UVA 12647 Balloon
这是一个线段树的题目;
我记得一个月前在cf上也做过一个类似的题目;
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define maxn 100010 5 #define bon 1000010 6 using namespace std; 7 int cover[bon<<2]; 8 int f[maxn<<2],ans[maxn<<2]; 9 struct line 10 { 11 int x1,y1,x2,y2; 12 bool operator<(const line &t)const 13 { 14 return y1<t.y1; 15 } 16 } li[maxn<<2]; 17 18 void pushdown(int rt) 19 { 20 if(cover[rt]>=0) 21 { 22 cover[rt<<1]=cover[rt<<1|1]=cover[rt]; 23 cover[rt]=-1; 24 } 25 } 26 27 int query(int x,int l,int r,int rt) 28 { 29 if(cover[rt]>=0) return cover[rt]; 30 int mid=(l+r)>>1; 31 pushdown(rt); 32 if(x<=mid)return query(x,l,mid,rt<<1); 33 else return query(x,mid+1,r,rt<<1|1); 34 } 35 36 void update(int L,int R,int i,int l,int r,int rt) 37 { 38 if(L<=l&&R>=r) 39 { 40 cover[rt]=i; 41 return; 42 } 43 int mid=(l+r)>>1; 44 pushdown(rt); 45 if(L<=mid)update(L,R,i,l,mid,rt<<1); 46 if(R>mid)update(L,R,i,mid+1,r,rt<<1|1); 47 } 48 int n,q,xx; 49 void solve(int x) 50 { 51 int k=query(x,0,bon,1); 52 if(li[k].y1!=li[k].y2)x=ans[k]; 53 if(f[k]==n)printf("%d\n",x); 54 else printf("%d %d\n",x,li[f[k]].y1); 55 } 56 57 int main() 58 { 59 while(scanf("%d%d",&n,&q)!=EOF) 60 { 61 memset(cover,-1,sizeof cover); 62 for(int i=0; i<n; i++) 63 { 64 scanf("%d%d%d%d",&li[i].x1,&li[i].y1,&li[i].x2,&li[i].y2); 65 if(li[i].y1<li[i].y2) 66 { 67 swap(li[i].x1,li[i].x2); 68 swap(li[i].y1,li[i].y2); 69 } 70 } 71 li[n].x1=0,li[n].x2=bon,li[n].y1=bon+1,li[n].y2=bon+1; 72 sort(li,li+n+1); 73 for(int i=n; i>=0; i--) 74 { 75 if(li[i].y1==li[i].y2) f[i]=i; 76 else 77 { 78 int k=query(li[i].x1,0,bon,1); 79 f[i]=f[k]; 80 if(li[k].y1==li[k].y2) ans[i]=li[i].x1; 81 else ans[i]=ans[k]; 82 } 83 int l=li[i].x1,r=li[i].x2; 84 if(l>r)swap(l,r); 85 update(l,r,i,0,bon,1); 86 } 87 while(q--) 88 { 89 scanf("%d",&xx); 90 solve(xx); 91 } 92 } 93 return 0; 94 }