标签:
Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> #define true ture #define false flase using namespace std; #define ll long long int scan() { int res = 0 , ch ; while( !( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - ‘0‘ ; while( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) res = res * 10 + ( ch - ‘0‘ ) ; return res ; } int w; struct is { int l,r; int num; }tree[200010*3]; void build_tree(int l,int r,int pos) { tree[pos].l=l; tree[pos].r=r; if(l==r) { tree[pos].num=w; //scanf("%lld",&tree[pos].num); return; } int mid=(l+r)/2; build_tree(l,mid,pos*2); build_tree(mid+1,r,pos*2+1); tree[pos].num=max(tree[pos*2].num,tree[pos*2+1].num); } void update(int l,int r,int change,int pos) { if(tree[pos].l==l&&tree[pos].r==r) { tree[pos].num+=change; return; } int mid=(tree[pos].l+tree[pos].r)/2; if(r<=mid) update(l,r,change,pos*2); else if(l>mid) update(l,r,change,pos*2+1); else { update(l,mid,change,pos*2); update(mid+1,r,change,pos*2+1); } tree[pos].num=max(tree[pos*2].num,tree[pos*2+1].num); } int query(int l,int r,int pos) { //cout<<l<<" "<<r<<" "<<pos<<endl; if(tree[pos].l==l&&tree[pos].r==r) return tree[pos].num; int mid=(tree[pos].l+tree[pos].r)/2; if(l>mid) return query(l,r,pos*2+1); else if(r<=mid) return query(l,r,pos*2); else return max(query(l,mid,pos*2),query(mid+1,r,pos*2+1)); } int main() { int x,q,i,t; while(~scanf("%d%d%d",&x,&w,&q)) { if(x>q) x=q; build_tree(1,x,1); while(q--) { int fi; scanf("%d",&fi); int st=1; int en=x; int mid=(st+en)>>1; if(query(st,en,1)<fi) printf("-1\n"); else { while(st<en) { mid=(st+en)>>1; int ans=query(st,mid,1); if(ans>=fi) en=mid; else st=mid+1; } printf("%d\n",st); update(st,st,-fi,1); } } } return 0; }
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5400533.html