思路:二维线段树求区域最值。值得注意的是在进行Q操作时可能H1>H2,A1>A2.所以要swap.
View Code
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 #define lson l,m,rt<<1 5 #define rson m+1,r,rt<<1|1 6 #define maxn1 1002 7 #define maxn2 102 8 struct node 9 { 10 int subsetree[maxn1*3]; 11 }setree[maxn2*3]; 12 void subbuild(int num,int l,int r,int rt) 13 { 14 setree[num].subsetree[rt]=-1; 15 if(l==r) 16 return; 17 int m=(l+r)>>1; 18 subbuild(num,lson); 19 subbuild(num,rson); 20 } 21 void build(int l,int r,int rt) 22 { 23 subbuild(rt,0,1000,1); 24 if(l==r) 25 return; 26 int m=(l+r)>>1; 27 build(lson); 28 build(rson); 29 } 30 void subupdate(int num,int l,int r,int rt,int l2,int c) 31 { 32 if(l==r){ 33 setree[num].subsetree[rt]=max(setree[num].subsetree[rt],c); 34 return; 35 } 36 int m=(l+r)>>1; 37 if(l2<=m) 38 subupdate(num,lson,l2,c); 39 else if(l2>m) 40 subupdate(num,rson,l2,c); 41 setree[num].subsetree[rt]=max(setree[num].subsetree[rt<<1],setree[num].subsetree[rt<<1|1]); 42 } 43 void update(int l,int r,int rt,int l1,int l2,int c) 44 { 45 subupdate(rt,0,1000,1,l2,c); 46 if(l==r) 47 return; 48 int m=(l+r)>>1; 49 if(l1<=m) 50 update(lson,l1,l2,c); 51 else if(l1>m) 52 update(rson,l1,l2,c); 53 } 54 int subquery(int num,int l,int r,int rt,int l2,int r2) 55 { 56 if(l2<=l&&r<=r2) 57 return setree[num].subsetree[rt]; 58 int m=(l+r)>>1; 59 int ans=-1; 60 if(l2<=m) 61 ans=max(ans,subquery(num,lson,l2,r2)); 62 if(r2>m) 63 ans=max(ans,subquery(num,rson,l2,r2)); 64 return ans; 65 } 66 int query(int l,int r,int rt,int l1,int r1,int l2,int r2) 67 { 68 if(l1<=l&&r<=r1) 69 return subquery(rt,0,1000,1,l2,r2); 70 int m=(l+r)>>1; 71 int ans=-1; 72 if(l1<=m) 73 ans=max(ans,query(lson,l1,r1,l2,r2)); 74 if(r1>m) 75 ans=max(ans,query(rson,l1,r1,l2,r2)); 76 return ans; 77 } 78 int main() 79 { 80 int n; 81 while(scanf("%d",&n)&&n){ 82 build(100,200,1); 83 while(n--){ 84 char s[5]; 85 scanf("%s",s); 86 if(s[0]=='I'){ 87 double a,l; 88 int l1; 89 scanf("%d%lf%lf",&l1,&a,&l); 90 int l2=int(a*10),c=int(l*10); 91 update(100,200,1,l1,l2,c); 92 } 93 else if(s[0]=='Q'){ 94 double l2,r2; 95 int l1,r1; 96 scanf("%d%d%lf%lf",&l1,&r1,&l2,&r2); 97 int ll2=int(10*l2),rr2=int(10*r2); 98 if(l1>r1)swap(l1,r1); 99 if(ll2>rr2)swap(ll2,rr2); 100 int ans=query(100,200,1,l1,r1,ll2,rr2); 101 if(ans==-1) 102 printf("-1\n"); 103 else 104 printf("%.1lf\n",ans/10.0); 105 } 106 } 107 } 108 return 0; 109 }