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 |
路过的大牛帮忙看下,如何优化···暴力1400MS+,二维线段树死活TLE,优化了一下午还是TLE.暴力代码在HDU上跑800多MS,二维线段树在HDU上面交是1200MS+,所以这边跑的话预估时间是2500MS左右。实在是不知道怎么优化了,先贴TLE的码放着,以后有空再回来继续优化 #include <stdio.h> #include <math.h> #include <algorithm> #include <map> #include <string> #include <string.h> #include <queue> #include <set> using namespace std; const int N=39; const int BGCOLOR=1<<20; int mpy[40],mpx[40],ans[BGCOLOR]; pair<int,int> a[40]; int Y,X,X1,X2,Yidx,Xidx,mask; struct rectangles { pair<int,int> p1,p2; }rec[20]; struct node { int color1,has1,color2[2*N-1],has2[2*N-1]; }tree[2*N-1],*p; int inline getidx(int s,int t) { return s+t|s!=t; } bool inline cmp(const pair<double,int> &a,const pair<double,int> &b) { return a.first<b.first; } void OperateX(int l,int r,int s,int t) { int nowidx=getidx(l,r); p->has2[nowidx]|=mask; if(l==s&&r==t) { p->color2[nowidx]|=mask; return; } int mid=l+r>>1; if(p->color2[nowidx]&BGCOLOR) { int lch=getidx(l,mid),rch=getidx(mid+1,r); p->color2[lch]=p->color2[rch]=BGCOLOR; p->color2[nowidx]-=BGCOLOR; p->has2[lch]=p->has2[rch]=0; } if(s<=mid) { if(t<=mid) OperateX(l,mid,s,t); else { OperateX(l,mid,s,mid); OperateX(mid+1,r,mid+1,t); } } else OperateX(mid+1,r,s,t); } void OperateY(int l,int r,int s,int t) { int nowidx=getidx(l,r); tree[nowidx].has1|=mask; if(l==s&&r==t) { if(tree[nowidx].color1>0) { if(tree[nowidx].color1==BGCOLOR) tree[nowidx].color1=mask; else tree[nowidx].color1|=mask; p=&tree[nowidx]; OperateX(0,X-1,X1,X2); return; } } int mid=l+r>>1; if(tree[nowidx].color1>0) { int lch=getidx(l,mid),rch=getidx(mid+1,r); if(tree[nowidx].color1!=BGCOLOR) { memcpy(tree[lch].color2,tree[nowidx].color2,2*X-1<<2); memcpy(tree[rch].color2,tree[nowidx].color2,2*X-1<<2); memcpy(tree[lch].has2,tree[nowidx].has2,2*X-1<<2); memcpy(tree[rch].has2,tree[nowidx].has2,2*X-1<<2); tree[lch].has1=tree[rch].has1=tree[nowidx].has1; } else { tree[lch].color2[Xidx]=tree[rch].color2[Xidx]=BGCOLOR; tree[lch].has1=tree[rch].has1=tree[lch].has2[Xidx]=tree[rch].has2[Xidx]=0; } tree[lch].color1=tree[rch].color1=tree[nowidx].color1; tree[nowidx].color1=0; } if(s<=mid) { if(t<=mid) OperateY(l,mid,s,t); else { OperateY(l,mid,s,mid); OperateY(mid+1,r,mid+1,t); } } else OperateY(mid+1,r,s,t); } int QueryX(int l,int r) { int nowidx=getidx(l,r); if(!(p->has2[nowidx]&mask)) return 0; if(p->color2[nowidx]&mask) return mpx[r+1]-mpx[l]; int mid=l+r>>1; return QueryX(l,mid)+QueryX(mid+1,r); } int QueryY(int l,int r) { int nowidx=getidx(l,r); if(!(tree[nowidx].has1&mask)) return 0; if(tree[nowidx].color1>0) { p=&tree[nowidx]; return (mpy[r+1]-mpy[l])*QueryX(0,X-1); } int mid=l+r>>1; return QueryY(l,mid)+QueryY(mid+1,r); } int main() { int n,m,cas=0,tmp; while(scanf("%d %d",&n,&m)&&n) { for(int i=0;i<n;i++) { scanf("%d %d %d %d",&a[i].first,&mpy[i],&a[i+n].first,&mpy[i+n]); a[i].second=i+1; a[i+n].second=-i-1; } sort(a,a+2*n,cmp); X=1; mpx[0]=a[0].first; for(int i=0;i<n<<1;i++) { if(a[i].first!=mpx[X-1]) mpx[X++]=a[i].first; if(a[i].second>0) rec[a[i].second-1].p1.first=X-1; else rec[-a[i].second-1].p2.first=X-2; } for(int i=0;i<n;i++) { a[i].first=mpy[i]; a[i+n].first=mpy[i+n]; a[i].second=i+1; a[i+n].second=-i-1; } sort(a,a+2*n,cmp); Y=1; mpy[0]=a[0].first; for(int i=0;i<n<<1;i++) { if(a[i].first!=mpy[Y-1]) mpy[Y++]=a[i].first; if(a[i].second>0) rec[a[i].second-1].p1.second=Y-1; else rec[-a[i].second-1].p2.second=Y-2; } X--; Y--; Yidx=getidx(0,Y-1); tree[Yidx].color1=BGCOLOR; tree[Yidx].has1=0; Xidx=getidx(0,X-1); tree[Yidx].color2[Xidx]=BGCOLOR; tree[Yidx].has2[Xidx]=0; mask=1; for(int i=0;i<n;i++) { X1=rec[i].p1.first; X2=rec[i].p2.first; OperateY(0,Y-1,rec[i].p1.second,rec[i].p2.second); mask<<=1; } memset(ans,0,1<<n+2); printf("Case %d:\n",++cas); for(int i=1;i<=m;i++) { scanf("%d",&n); mask=0; while(n--) { scanf("%d",&tmp); mask|=1<<tmp-1; } if(!ans[mask]) ans[mask]=QueryY(0,Y-1); printf("Query %d: %d\n",i,ans[mask]); } putchar('\n'); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator