hdu 1823
这题真恶心,用M究竟多大没有说,我的代码测试了好几组数据都对了,但是就是wa,后来改成c++就ac了,O__O"…,二维线段树不话可说,
#include <iostream> #include <cstdio> using namespace std; struct Sub { int l,r; double love; }; struct Tree { int l,r; Sub sub[4000]; }tree[800]; void init(int root,int rt,int l,int r) { tree[root].sub[rt].love=-1; tree[root].sub[rt].l=l; tree[root].sub[rt].r=r; if(l==r) return; //cout<<l<<" "<<r<<endl; int m=(l+r)>>1; init(root,rt<<1,l,m); init(root,rt<<1|1,m+1,r); } void build(int rt,int l,int r,int ll,int rr) { tree[rt].l=l,tree[rt].r=r; init(rt,1,ll,rr); if(l==r) return; //cout<<"bug"<<endl; int m=(l+r)/2; build(rt<<1,l,m,ll,rr); build(rt<<1|1,m+1,r,ll,rr); } void insert(int root,int rt,int act,double love) { tree[root].sub[rt].love=max(tree[root].sub[rt].love,love); if(tree[root].sub[rt].l==tree[root].sub[rt].r) { //cout<<root<<" "<<rt<<endl; return; } int m=(tree[root].sub[rt].l+tree[root].sub[rt].r)>>1; if(act<=m) insert(root,rt<<1,act,love); else insert(root,rt<<1|1,act,love); tree[root].sub[rt].love=max(tree[root].sub[rt<<1].love,tree[root].sub[rt<<1|1].love); //cout<<tree[root].sub[rt].love<<endl; } void update(int rt,int height,int act,double love) { insert(rt,1,act,love); if(tree[rt].l==tree[rt].r) return; int m=(tree[rt].l+tree[rt].r)>>1; if(height<=m) update(rt<<1,height,act,love); else update(rt<<1|1,height,act,love); } double answer(int root,int rt,int l,int r) { if(l<=tree[root].sub[rt].l&&r>=tree[root].sub[rt].r) { return tree[root].sub[rt].love; } int m=(tree[root].sub[rt].l+tree[root].sub[rt].r)>>1; double ret1=-1,ret2=-1; if(l<=m) ret1=answer(root,rt<<1,l,r); if(r>m) ret2=answer(root,rt<<1|1,l,r); return max(ret1,ret2); } double query(int rt,int l,int r,int ll,int rr) { if(l<=tree[rt].l&&r>=tree[rt].r) { return answer(rt,1,ll,rr); } int m=(tree[rt].l+tree[rt].r)>>1; double ret1=-1,ret2=-1; //cout<<l<<" "<<m<<endl; if(l<=m) ret1=query(rt<<1,l,r,ll,rr); if(r>m) ret2=query(rt<<1|1,l,r,ll,rr); return max(ret1,ret2); } int main() { int m,height; double act,love; char ord[5]; while(scanf("%d",&m)&&m) { build(1,100,200,0,1000); while(m--) { scanf("%s",ord); if(ord[0]=='I') { scanf("%d %lf %lf",&height,&act,&love); update(1,height,(int)(act*10),love); } else { // cout<<tree[1].sub[1450].love<<endl; int hl,hr; double al,ar; scanf("%d %d %lf %lf",&hl,&hr,&al,&ar); if(hl>hr) swap(hl,hr); if(al>ar) swap(al,ar); double ans=query(1,hl,hr,(int)(al*10),(int)(ar*10)); if(ans==-1) printf("-1\n"); else printf("%.1lf\n",ans); } } } return 0; }