1 #include<cstdio>
2
3 #include<cstdlib>
4
5 #include<cmath>
6
7 #include<cstring>
8
9 #include<algorithm>
10
11 #include<iostream>
12
13 #include<vector>
14
15 #include<map>
16
17 #include<set>
18
19 #include<queue>
20
21 #include<string>
22
23 #define inf 1100000000
24
25 #define maxn 500000+100
26
27 #define maxm 500+100
28
29 #define eps 1e-10
30
31 #define ll long long
32
33 #define pa pair<int,int>
34
35 #define for0(i,n) for(int i=0;i<=(n);i++)
36
37 #define for1(i,n) for(int i=1;i<=(n);i++)
38
39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
40
41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
42
43 #define mod 1000000007
44
45 using namespace std;
46
47 inline int read()
48
49 {
50
51 int x=0,f=1;char ch=getchar();
52
53 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
54
55 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
56
57 return x*f;
58
59 }
60 int n,m,k,ans[maxn],head[maxn],next[maxn];
61 ll s[maxn];
62 struct rec1{int l,r;ll w;}b[maxn];
63 struct rec2{int pos;ll ned,cur;}a[maxn],q[maxn];
64 inline void change(int x,ll w)
65 {
66 for(;x<=m;x+=x&(-x))s[x]+=w;
67 }
68 inline ll sum(int x)
69 {
70 ll t=0;
71 for(;x;x-=x&(-x))t+=s[x];
72 return t;
73 }
74 inline void update(int l,int r,ll w)
75 {
76 change(l,w);change(r+1,-w);
77 }
78 void solve(int l,int r,int h,int t)
79 {
80 if(h>t)return;
81 if(l==r)
82 {
83 for2(i,h,t)ans[a[i].pos]=l;
84 return;
85 }
86 int mid=(l+r)>>1,t1=h-1,t2=t+1;
87 for2(i,l,mid)
88 if(b[i].l<=b[i].r)update(b[i].l,b[i].r,b[i].w);else update(b[i].l,m,b[i].w),update(1,b[i].r,b[i].w);
89 for2(i,h,t)
90 {
91 ll tmp=0;
92 for(int j=head[a[i].pos];j;j=next[j])tmp+=sum(j);
93 if(a[i].cur+tmp>=a[i].ned)q[++t1]=a[i];else a[i].cur+=tmp,q[--t2]=a[i];
94 }
95 for2(i,h,t)a[i]=q[i];
96 for2(i,l,mid)
97 if(b[i].l<=b[i].r)update(b[i].l,b[i].r,-b[i].w);else update(b[i].l,m,-b[i].w),update(1,b[i].r,-b[i].w);
98 solve(l,mid,h,t1);
99 solve(mid+1,r,t2,t);
100 }
101
102 int main()
103
104 {
105
106 freopen("input.txt","r",stdin);
107
108 freopen("output.txt","w",stdout);
109
110 n=read();m=read();
111 for1(i,m)
112 {
113 int x=read();
114 next[i]=head[x];head[x]=i;
115 }
116 for1(i,n)a[i].ned=read(),a[i].pos=i;
117 k=read();
118 for1(i,k)b[i].l=read(),b[i].r=read(),b[i].w=read();
119 b[++k].l=1;b[k].r=m;b[k].w=inf;
120 solve(1,k,1,n);
121 for1(i,n)
122 if(ans[i]==k||ans[i]==0)printf("NIE\n");else printf("%d\n",ans[i]);
123
124 return 0;
125
126 }