#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define pb push_back
const int N = 300005;
const int M = 90;
ull cnt[N], tree[N*M];
int L[N*M], R[N*M], ind[N];
vector<int> v[N];
int node_cnt = 0;
int build(int l, int r)
{
node_cnt++;
int node = node_cnt;
tree[node] = 0;
if(l == r)
return node;
int mid = (l+r)>>1;
L[node] = build(l, mid);
R[node] = build(mid+1, r);
return node;
}
void LazyPropagate(int node)
{
node_cnt++;
int left = node_cnt;
node_cnt++;
int right = node_cnt;
L[left] = L[L[node]];
R[left] = R[L[node]];
L[right] = L[R[node]];
R[right] = R[R[node]];
tree[left] = tree[node] + tree[L[node]];
tree[right] = tree[node] + tree[R[node]];
L[node] = left;
R[node] = right;
tree[node] = 0;
return;
}
int update(int pnode, int l, int r, int a, int b, ull val)
{
node_cnt++;
int node = node_cnt;
if(l == a && r == b)
{
tree[node] = tree[pnode] + val;
L[node] = L[pnode];
R[node] = R[pnode];
return node;
}
if(tree[pnode])
LazyPropagate(pnode);
int mid = (l+r)>>1;
L[node] = L[pnode];
R[node] = R[pnode];
if(b<=mid)
L[node] = update(L[pnode], l, mid, a, b, val);
else if(a>mid)
R[node] = update(R[pnode], mid+1, r, a, b, val);
else
{
L[node] = update(L[pnode], l, mid, a, mid, val);
R[node] = update(R[pnode], mid+1, r, mid+1, b, val);
}
return node;
}
ull query(int node, int l, int r, int idx)
{
if(l == r)
return tree[node];
if(tree[node])
LazyPropagate(node);
int mid = (l+r)>>1;
if(idx<=mid)
return query(L[node], l, mid, idx);
return query(R[node], mid+1, r, idx);
}
int main()
{
int n, m, k, l, r, q, mid, ow;
ull a;
scanf("%d %d", &n, &m);
for(int i = 1; i<=m; i++)
{
scanf("%d", &ow);
v[ow].pb(i);
}
for(int i = 1; i<=n; i++)
scanf("%llu", &cnt[i]);
ind[0] = build(1, m);
scanf("%d", &q);
for(int i = 1; i<=q; i++)
{
scanf("%d %d %llu", &l, &r, &a);
if(l > r)
{
ind[i] = update(ind[i-1], 1, m, l, m, a);
ind[i] = update(ind[i], 1, m, 1, r, a);
}
else
ind[i] = update(ind[i-1], 1, m, l, r, a);
}
ull total;
for(int i = 1; i<=n; i++)
{
l = 1;
r = q + 1;
while(l<r)
{
mid = (l+r)>>1;
total = 0;
for(int j = 0; j<v[i].size(); j++)
total += query(ind[mid], 1, m, v[i][j]);
if(total < cnt[i])
l = mid + 1;
else
r = mid;
}
if(l == (q + 1))
printf("NIE\n");
else
printf("%d\n", l);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKI2RlZmluZSB1bGwgdW5zaWduZWQgbG9uZyBsb25nCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiAKY29uc3QgaW50IE4gPSAzMDAwMDU7CmNvbnN0IGludCBNID0gOTA7CnVsbCBjbnRbTl0sIHRyZWVbTipNXTsKaW50IExbTipNXSwgUltOKk1dLCBpbmRbTl07CnZlY3RvcjxpbnQ+IHZbTl07CmludCBub2RlX2NudCA9IDA7CiAKaW50IGJ1aWxkKGludCBsLCBpbnQgcikKewoJbm9kZV9jbnQrKzsKCWludCBub2RlID0gbm9kZV9jbnQ7Cgl0cmVlW25vZGVdID0gMDsKIAoJaWYobCA9PSByKQoJCXJldHVybiBub2RlOwoJaW50IG1pZCA9IChsK3IpPj4xOwoJTFtub2RlXSA9IGJ1aWxkKGwsIG1pZCk7CglSW25vZGVdID0gYnVpbGQobWlkKzEsIHIpOwogCglyZXR1cm4gbm9kZTsKfQogCnZvaWQgTGF6eVByb3BhZ2F0ZShpbnQgbm9kZSkKewoJbm9kZV9jbnQrKzsKCWludCBsZWZ0ID0gbm9kZV9jbnQ7Cglub2RlX2NudCsrOwoJaW50IHJpZ2h0ID0gbm9kZV9jbnQ7CiAKCUxbbGVmdF0gPSBMW0xbbm9kZV1dOwoJUltsZWZ0XSA9IFJbTFtub2RlXV07CiAKCUxbcmlnaHRdID0gTFtSW25vZGVdXTsKCVJbcmlna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