#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define pb push_back
const int N = 300005;
const int M = 200;
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+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIHVsbCB1bnNpZ25lZCBsb25nIGxvbmcKI2RlZmluZSBwYiBwdXNoX2JhY2sKCmNvbnN0IGludCBOID0gMzAwMDA1Owpjb25zdCBpbnQgTSA9IDIwMDsKdWxsIGNudFtOXSwgdHJlZVtOKk1dOwppbnQgTFtOKk1dLCBSW04qTV0sIGluZFtOXTsKdmVjdG9yPGludD4gdltOXTsKaW50IG5vZGVfY250ID0gMDsKCmludCBidWlsZChpbnQgbCwgaW50IHIpCnsKCW5vZGVfY250Kys7CglpbnQgbm9kZSA9IG5vZGVfY250OwoJdHJlZVtub2RlXSA9IDA7CgoJaWYobCA9PSByKQoJCXJldHVybiBub2RlOwoJaW50IG1pZCA9IChsK3IpPj4xOwoJTFtub2RlXSA9IGJ1aWxkKGwsIG1pZCk7CglSW25vZGVdID0gYnVpbGQobWlkKzEsIHIpOwoKCXJldHVybiBub2RlOwp9Cgp2b2lkIExhenlQcm9wYWdhdGUoaW50IG5vZGUpCnsKCW5vZGVfY250Kys7CglpbnQgbGVmdCA9IG5vZGVfY250OwoJbm9kZV9jbnQrKzsKCWludCByaWdodCA9IG5vZGVfY250OwoKCUxbbGVmdF0gPSBMW0xbbm9kZV1dOwoJUltsZWZ0XSA9IFJbTFtub2RlXV07CgoJTFtyaWdodF0gPSBMW1Jbbm9kZV1dOwoJUltyaWdodF0gPSBSW1Jbbm9kZV1dO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