Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贪心+线段树 为啥WA了?想问问大家为啥WA了 不是很理解 #include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> #define dbg(x) cout<<#x<<" = "<< (x)<< endl using namespace std; const int MAX_N = 51024; int vis[MAX_N<<2]; int s[MAX_N<<2],col[MAX_N<<2]; void up(int p){ s[p] = s[p*2] + s[p*2+1]; } void down(int p,int l,int r){ if(col[p]){ int mid = (l+r)>>1; col[p*2]+=col[p]; col[p*2+1]+=col[p]; s[p*2]+=col[p]*(l-mid+1); s[p*2+1]+=col[p]*(r-mid); col[p] = 0; } } void build (int p,int l,int r){ col[p]=0; if(l==r){ s[p] = 0; return; } int mid = (l+r)>>1; build(p*2,l,mid); build(p*2+1,mid+1,r); up(p); } void change(int p,int l,int r,int x,int y,int c){ if(x<=l&&r<=y){ col[p]+=c; s[p]+=c*(r-l+1); return; } down(p,l,r); int mid = (l+r)>>1; if(x<=mid) change(p*2,l,mid,x,y,c); if(y>mid) change(p*2+1,mid+1,r,x,y,c); up(p); } int query(int p,int l,int r,int x){ if(l==r){ return s[p]; } down(p,l,r); int mid = (l+r)>>1,res; if(x<=mid) res = query(p*2,l,mid,x); else res = query(p*2+1,mid+1,r,x); return res; } struct node { int x; int y; int num; bool operator < (const node &other)const{ if(y==other.y) return x>other.x; return y<other.y; } }arr[MAX_N]; int main(){ s[0] = 0; int n; while(~scanf("%d",&n)){ memset(vis,0,sizeof(vis)); int maxx=-1; for(int i= 1;i<=n;i++){ scanf("%d%d%d",&arr[i].x,&arr[i].y,&arr[i].num); arr[i].x++; arr[i].y++; } sort(arr+1,arr+1+n); build(1,1,arr[n].y); for(int i=0;i<arr[1].num;i++){ vis[arr[1].y-arr[1].num+1+i]=1; change(1,1,arr[n].y,arr[1].y-arr[1].num+1+i,arr[n].y,1); } int cnt=arr[1].num; for(int i =2;i<=n;i++){ int numx = query(1,1,arr[n].y,arr[i].x-1); int numy = query(1,1,arr[n].y,arr[i].y); //dbg(numy); //dbg(numx); if((arr[i].num-(numy-numx))<0) arr[i].num=0; else arr[i].num-=(numy-numx); //dbg(arr[i].num); cnt+=arr[i].num; //dbg(cnt); if(!arr[i].num) continue; else { int cnt_now = 0; for(int k = 0;k<arr[i].num;k++){ while(vis[arr[i].y-arr[i].num+1+k-cnt_now]==1) cnt_now++; change(1,1,arr[n].y,arr[i].y-arr[i].num+1+k-cnt_now,arr[n].y,1); vis[arr[i].y-arr[i].num+1+k-cnt_now]=1; //dbg(arr[i].y-arr[i].num+1+k-cnt_now); } } } printf("%d\n",cnt); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator