思路:整体二分
提交:4次
错因:树状数组开的$int$
题解:
二分操作序列,将仅用$[l,md]$即可满足要求的国家递归到左半边,将仅用$[l,md]$不能满足要求的国家,把他们的要求去掉左半边的贡献,递归到右半边。
具体来说,开一个以空间站为下标的树状数组(把环展成链),区间加单点求和转化为差分和前缀和,依次加入$[l,md]$中的所有操作区间;
然后每个国家枚举自己的所有空间站,计算贡献,判断前$[l,md]$是否满足,来决定向左右递归的方向。
#include<iostream> #include<cstdio> using namespace std; #define ull unsigned long long #define ll long long #define R register ll #define pause (for(R i=1;i<=10000000000;++i)) #define In freopen("NOIPAK++.in","r",stdin) #define Out freopen("out.out","w",stdout) namespace Fread { static char B[1<<15],*S=B,*D=B; #ifndef JACK #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++) #endif inline ll g() { R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix; if(ch==EOF) return EOF; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix; } inline bool isempty(const char& ch) {return (ch<=36||ch>=127);} inline void gs(char* s) { register char ch; while(isempty(ch=getchar())); do *s++=ch; while(!isempty(ch=getchar())); } } using Fread::g; using Fread::gs; namespace Luitaryi { const int N=3e+5+10; int n,m,k,lim,ct; struct node {int fir,rk; ll w;}a[N],tmp[N]; #define fir(u) a[u].fir int vr[N],nxt[N],LL[N],RR[N],W[N],ans[N]; ll c[N<<1]; inline void adde(int u,int v) {vr[++ct]=v,nxt[ct]=fir(u),fir(u)=ct;} inline int lbt(int x) {return x&-x;} inline void add(int pos,int inc) {for(;pos<=lim;pos+=lbt(pos)) c[pos]+=inc;} inline ll query(int pos) { R ret=0; for(;pos;pos-=lbt(pos)) ret+=c[pos]; return ret; } inline void solve(int l,int r,int s,int t) { if(l==r) {for(R i=s;i<=t;++i) ans[a[i].rk]=l; return ;} R md=l+r>>1,f=s-1,b=t+1; for(R i=l;i<=md;++i) add(LL[i],W[i]),add(RR[i]+1,-W[i]); for(R u=s;u<=t;++u) { R cnt=0; for(R i=fir(u),lim=a[u].w;i&&cnt<lim;i=nxt[i]) { R v=vr[i]; cnt+=query(v)+query(v+m); } if(cnt>=a[u].w) tmp[++f]=a[u]; else tmp[--b]=a[u],tmp[b].w-=cnt; } for(R i=l;i<=md;++i) add(LL[i],-W[i]),add(RR[i]+1,W[i]); for(R i=s;i<=f;++i) a[i]=tmp[i]; for(R i=t;i>=b;--i) a[i]=tmp[i]; solve(l,md,s,f); solve(md+1,r,b,t); } inline void main() { n=g(),m=g(),lim=m<<1; for(R i=1,x;i<=m;++i) x=g(),adde(x,i); for(R i=1;i<=n;++i) a[i].w=g(),a[i].rk=i; k=g(); for(R i=1;i<=k;++i) LL[i]=g(),RR[i]=g(),W[i]=g(),LL[i]>RR[i]?RR[i]+=m:0; solve(1,k+1,1,n); for(R i=1;i<=n;++i) ans[i]==k+1?printf("NIE\n"):printf("%d\n",ans[i]); } } signed main() { Luitaryi::main(); return 0; }
2019.07.14