#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define PII pair<int,int>
#define all(c) c.begin(),c.end()
#define sz(c) (int)c.size()
#define clr(c) c.clear()
#define pb push_back
#define mp make_pair
#define cin(x) scanf("%d",&x)
#define MOD 1000000007
#define EPS 1E-10
const int maxn = 300010;
vector<int> owns[maxn]; // who owns whom
int owner[maxn]; // given array
int reqd[maxn]; // required samples
vector<int> tocheck[maxn]; // the parallel binary search
int L[maxn], R[maxn]; // left and right.
int ql[maxn],qr[maxn],qa[maxn];
// segtree
int segtree[1000000];
int lazy[1000000];
void buildTree(int l,int r,int pos)
{
if(l == r)
{
segtree[pos] = 0;
lazy[pos] = 0;
}
else
{
int mid = (l + r) >> 1;
buildTree(l,mid,2*pos);
buildTree(mid+1,r,2*pos+1);
segtree[pos] = segtree[2*pos] + segtree[2*pos+1];
lazy[pos] = 0;
}
}
void lazyu(int l,int r,int pos)
{
if(lazy[pos] == 0) return;
segtree[pos] = (int)min(1000000000LL, segtree[pos] + (r - l + 1LL) * lazy[pos]);
if(l != r)
{
lazy[2*pos] += lazy[pos];
lazy[2*pos + 1] += lazy[pos];
lazy[2*pos] = min(lazy[2*pos],1000000000);
lazy[2*pos+1] = min(lazy[2*pos+1],1000000000);
}
lazy[pos] = 0;
}
void update(int lQ,int rQ,int val,int l,int r,int pos)
{
lazyu(l,r,pos);
if(l > r or l > rQ or r < lQ or lQ > rQ) return ;
if(l >= lQ && r <= rQ)
{
segtree[pos] = (int)min(1000000000LL, segtree[pos] + (r - l + 1LL) * val);
if(l != r)
{
lazy[2 * pos] += val;
lazy[2 * pos + 1] += val;
lazy[2*pos] = min(lazy[2*pos],1000000000);
lazy[2*pos+1] = min(lazy[2*pos+1],1000000000);
}
return ;
}
int mid = (l + r) >> 1;
update(lQ,rQ,val,l,mid,2*pos);
update(lQ,rQ,val,mid+1,r,2*pos+1);
segtree[pos] = segtree[2*pos] + segtree[2*pos+1];
segtree[pos] = min(segtree[pos],1000000000);
}
int query(int lQ,int rQ,int l,int r,int pos)
{
lazyu(l,r,pos);
if(l > r or lQ > rQ or l > rQ or r < lQ) return 0;
else if(l >= lQ && r <= rQ) return segtree[pos];
int mid = (l + r) >> 1;
int ret = query(lQ,rQ,l,mid,2*pos) + query(lQ,rQ,mid+1,r,2*pos+1);
return (ret >= 1000000000) ? 1000000000 : ret;
}
// end of segtree
int n,m,k;
void cleanup()
{
for(int i = 1; i <= k + 1; i++)
clr(tocheck[i]);
for(int i = 1; i <= n; i++)
if(L[i] != R[i])
tocheck[ (L[i] + R[i])/2 ].pb(i);
}
int main()
{
cin(n);
cin(m);
for(int i = 1; i <= m; i++)
{
cin(owner[i]);
owns[owner[i]].pb(i);
}
for(int i = 1; i <= n; i++)
cin(reqd[i]);
cin(k);
for(int i = 1; i <= n; i++)
{
L[i] = 1;
R[i] = k + 1;
}
for(int i = 1; i <= k; i++)
{
cin(ql[i]);
cin(qr[i]);
cin(qa[i]);
}
int changed = 1;
while(changed)
{
changed = 0;
cleanup();
buildTree(1,m,1);
for(int q = 1; q <= k; q++)
{
// apply updates and then use tocheck
if(ql[q] > qr[q])
{
update(ql[q],m,qa[q],1,m,1);
update(1,qr[q],qa[q],1,m,1);
}
else
update(ql[q],qr[q],qa[q],1,m,1);
for(auto id: tocheck[q])
{
changed = 1;
int sum = 0;
for(auto sector: owns[id])
{
sum += query(sector,sector,1,m,1);
if(sum >= reqd[id]) break;
}
if(sum >= reqd[id])
R[id] = q;
else
L[id] = q + 1;
}
}
}
for(int i = 1; i <= n; i++)
{
assert(L[i] == R[i]);
if(L[i] > k)
printf("NIE\n");
else
printf("%d\n",L[i]);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBMTDsKCiNkZWZpbmUgUElJICAgICAgIHBhaXI8aW50LGludD4KI2RlZmluZSBhbGwoYykgICAgYy5iZWdpbigpLGMuZW5kKCkKI2RlZmluZ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