Time Limit: 10000/1000 MS
(Java/Others) Memory Limit: 32768/32768 K
(Java/Others)
Total Submission(s): 4897 Accepted
Submission(s): 1219
二维线段树,读入数据时少写了一个&,一直以为是建树或者是更新时出了问题,调试了几个小时!!!!
数据不大,每次插入和询问都是O(lgn*lgn),算是水题.
1 //140MS 6012K 2970 B C++ 2 #include<stdio.h> 3 #define NH 105 4 #define NA 1005 5 struct sub_node{ 6 int l; 7 int r; 8 int max; 9 }; 10 struct node{ 11 sub_node st[4*NA]; 12 int l; 13 int r; 14 }tree[4*NH]; 15 inline int Max(int a,int b) 16 { 17 return a>b?a:b; 18 } 19 void sub_build(int l,int r,int rt,int prt) 20 { 21 tree[prt].st[rt].l=l; 22 tree[prt].st[rt].r=r; 23 tree[prt].st[rt].max=-1; 24 if(l==r) return; 25 int mid=(l+r)>>1; 26 sub_build(l,mid,2*rt,prt); 27 sub_build(mid+1,r,2*rt+1,prt); 28 } 29 void build(int l,int r,int prt,int sl,int sr) 30 { 31 tree[prt].l=l; 32 tree[prt].r=r; 33 sub_build(sl,sr,1,prt); 34 if(l==r) return; 35 int mid=(l+r)>>1; 36 build(l,mid,2*prt,sl,sr); 37 build(mid+1,r,2*prt+1,sl,sr); 38 } 39 void sub_update(int A,int L,int rt,int prt) 40 { 41 if(tree[prt].st[rt].l==tree[prt].st[rt].r){ 42 tree[prt].st[rt].max=Max(tree[prt].st[rt].max,L); 43 return; 44 } 45 int mid=(tree[prt].st[rt].l+tree[prt].st[rt].r)>>1; 46 if(A<=mid) sub_update(A,L,2*rt,prt); 47 else sub_update(A,L,2*rt+1,prt); 48 tree[prt].st[rt].max=Max(tree[prt].st[2*rt].max,tree[prt].st[2*rt+1].max); 49 } 50 void update(int H,int A,int L,int prt) 51 { 52 sub_update(A,L,1,prt); 53 if(tree[prt].l==tree[prt].r) return; 54 int mid=(tree[prt].l+tree[prt].r)>>1; 55 if(H<=mid) update(H,A,L,2*prt); 56 else update(H,A,L,2*prt+1); 57 } 58 int sub_query(int A1,int A2,int rt,int prt) 59 { 60 if(A1<=tree[prt].st[rt].l && A2>=tree[prt].st[rt].r) 61 return tree[prt].st[rt].max; 62 if(A2<=tree[prt].st[2*rt].r) return sub_query(A1,A2,2*rt,prt); 63 else if(A1>=tree[prt].st[2*rt+1].l) return sub_query(A1,A2,2*rt+1,prt); 64 else return Max(sub_query(A1,tree[prt].st[2*rt].r,2*rt,prt),sub_query(tree[prt].st[2*rt+1].l,A2,2*rt+1,prt)); 65 } 66 int query(int H1,int H2,int A1,int A2,int prt) 67 { 68 if(H1<=tree[prt].l && H2>=tree[prt].r){ 69 return sub_query(A1,A2,1,prt); 70 } 71 if(H2<=tree[2*prt].r) return query(H1,H2,A1,A2,2*prt); 72 else if(H1>=tree[2*prt+1].l) return query(H1,H2,A1,A2,2*prt+1); 73 else{ 74 return Max(query(H1,tree[2*prt].r,A1,A2,2*prt),query(tree[2*prt+1].l,H2,A1,A2,2*prt+1)); 75 } 76 } 77 int main(void) 78 { 79 int n,H1,H2; 80 double A1,A2,L; 81 char op; 82 while(scanf("%d%*c",&n),n) 83 { 84 build(0,100,1,0,1000); 85 while(n--){ 86 scanf("%c",&op); 87 if(op==‘I‘){ 88 scanf("%d%lf%lf%*c",&H1,&A1,&L); 89 update(H1-100,(int)(A1*10),(int)(L*10),1); 90 }else{ 91 scanf("%d%d%lf%lf%*c",&H1,&H2,&A1,&A2); 92 if(H1>H2){ 93 int temp=H1;H1=H2;H2=temp; 94 } 95 if(A1>A2){ 96 double temp=A1;A1=A2;A2=temp; 97 } 98 int ans=query(H1-100,H2-100,(int)(A1*10),(int)(A2*10),1); 99 if(ans==-1) puts("-1"); 100 else printf("%.1lf\n",ans/10.0); 101 } 102 } 103 } 104 return 0; 105 }
hdu 1823 Luck and Love (二维线段树),布布扣,bubuko.com
hdu 1823 Luck and Love (二维线段树)
原文:http://www.cnblogs.com/GO-NO-1/p/3679237.html