我们可以整体二分这玩意。
#pragma GCC optimize("-Ofast") #include <bits/stdc++.h> #define N 600005 #define LL long long #define L(x) ((x)&(-x)) #define MID (l+r>>1) #define Tc template <class T> using namespace std; LL T[N];vector<int> o[N]; #define sight(x) ('0'<=x&&x<='9') inline void read(int &x){ static char c; static int b; for (b=1,c=getchar();!sight(c);c=getchar()) if (c=='-') b=-1; for (x=0;sight(c);c=getchar())x=x*10+c-48; x=x*b; } int n,m,k,a[N],w[N],l,r,p,cnt,ans[N],b[N],c[N]; struct Que{ int l1,r1,l2,r2,p;}Q[N]; inline void _add(int pos,int x){ while (pos<=m) T[pos]+=x,pos+=L(pos); } inline LL _que(int pos){ static LL ret; ret=0; while (pos) ret+=T[pos],pos-=L(pos); return ret; } inline void gen(int l,int r,int x){ cnt++; if (r>=l) Q[cnt].l1=l,Q[cnt].r1=r; else Q[cnt].l1=l,Q[cnt].r1=m,Q[cnt].l2=1,Q[cnt].r2=r; Q[cnt].p=x; } inline void play(int x,int y,int sign){ for (int i=x;i<=y;i++){ _add(Q[i].l1,Q[i].p*sign);_add(Q[i].r1+1,-Q[i].p*sign); if (Q[i].l2) _add(Q[i].l2,Q[i].p*sign),_add(Q[i].r2+1,-Q[i].p*sign); } } bool query(int x){ static LL now;now=0; for (int i=0;i<o[x].size();++i) { now+=_que(o[x][i]); if (now>=w[x]) return 1; } return 0; } void work(int l,int r,int ll,int rr){ // if (l==r) { for (int i=ll;i<=rr;i++) ans[a[i]]=l; return ; } if (l+10>r) { for (int t=l;t<r;t++) { play(t,t,1); for (int i=ll;i<=rr;i++) if (ans[a[i]]==0&&query(a[i])) ans[a[i]]=t; } for (int i=ll;i<=rr;i++) if (ans[a[i]]==0) ans[a[i]]=r; play(l,r-1,-1); return; } play(l,MID,1); int p=ll-1,q=rr; for (int i=ll;i<=rr;i++) if (query(a[i])) b[i]=1; else b[i]=0; for (int i=ll;i<=rr;i++) if (b[i]) p++,c[p]=a[i]; else c[q]=a[i],q--; for (int i=ll;i<=rr;i++) a[i]=c[i]; work(MID+1,r,p+1,rr); play(l,MID,-1); work(l,MID,ll,p); } Tc void write(T x){if (x<10) {putchar('0'+x); return;} write(x/10); putchar('0'+x%10);} Tc inline void writeln(T x){ if (x<0) putchar('-'),x*=-1; write(x); putchar(' '); } Tc inline void writel(T x){ if (x<0) putchar('-'),x*=-1; write(x); putchar(' '); } int xx; signed main() { read(n); read(m); for (int i=1;i<=m;i++) read(xx),o[xx].push_back(i); for (int i=1;i<=n;i++) read(w[i]); read(k); for (int i=1;i<=k;i++){ read(l),read(r),read(p); gen(l,r,p); } gen(1,n,1e9); for (int i=1;i<=n;i++) a[i]=i; work(1,k+1,1,n); for (int i=1;i<=n;i++) if (ans[i]==k+1) puts("NIE"); else writeln(ans[i]); return 0; }