Bzoj 1513 POI2006 Tet-tetris 3D Two-dimensional line segment tree

Source: Internet
Author: User

Tag:bzoj   bzoj1513    segment Tree     tree nesting tree    

Topic: Given a matrix, the initial element at each position is 0, select a sub-rectangle each time, the value within the sub-rectangle is modified to the maximum value in this sub-rectangle + h , Find the maximum value in all positions at the end

We need to maintain a data structure that supports updating the values of the sub-rectangles and querying the maximum values of the sub-rectangles

It seems that the two-dimensional line tree is ready?

But yy a bit we will find two unresolved problems:
1. The tag's next pass
2. Uploading of information

In fact...

The first one is very good! Not to pass not to be good!
tags are permanent, without the next pass, as long as the query on the line of the tree path of each point is asked once!

So what's the second one?

The second one is very good! Do not upload no, just fine!
Since the modification only increases the value of the element, the answer to the question will be affected if any one of the locations in the interval has been updated.
So just modify each point on the path of the segment tree to modify it.

So we maintain two markers: a mark and B mark.
The a tag is precisely overwritten at each corresponding node on the line tree at the time of the modification, and the query needs to be queried for all nodes on the path to the root of all the corresponding nodes.
The b tag is exactly the opposite

That's all.

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define M 1010using namespace STD;intN,m,q,ans;structsegtree{Segtree *ls,*rs;intVal,mark;void*operator New(size_t) {StaticSegtree *mempool,*c;if(C==mempool) mempool= (c=Newsegtree[1<< the])+(1<< the); c->ls=c->rs=0x0; c->val=c->mark=0;returnC + +; }voidUpdate (intVal) { This->val=max ( This->val,val);    Mark=max (Mark,val); }voidPush_up () {Val=max (ls->val,rs->val); }voidPush_down () {if(!ls) ls=NewSegtree;if(!RS) rs=NewSegtree;if(Mark)            {ls->update (Mark);            Rs->update (Mark); mark=0; }    }voidUpdate (intXintYintLintRintVal) {intMid=x+y>>1;if(X==L&AMP;&AMP;Y==R) {Update (val);return; } push_down ();if(R<=mid) ls->update (x,mid,l,r,val);Else if(L>mid) Rs->update (mid+1, Y,l,r,val);ElseLs->update (X,mid,l,mid,val), Rs->update (mid+1, y,mid+1, R,val);    Push_up (); }intQuery (intXintYintLintR) {intMid=x+y>>1;if(X==L&AMP;&AMP;Y==R)returnVal Push_down ();if(R<=mid)returnLs->query (X,MID,L,R);if(L>mid)returnRs->query (mid+1, y,l,r);returnMax (Ls->query (X,mid,l,mid), Rs->query (mid+1, y,mid+1, R)); }};structabcd{ABCD *ls,*rs; Segtree *a,*b;void*operator New(size_t) {StaticABCD mempool[m<<1],*c=mempool; C->a=NewSegtree; c->b=NewSegtree;returnC + +; }voidBuild_tree (intXintY) {intMid=x+y>>1;if(x==y)return; (ls=NewABCD)->build_tree (X,mid); (rs=NewABCD)->build_tree (mid+1, y); }voidUpdate (intXintYintL1,intR1,intL2,intR2,intVal) {intMid=x+y>>1; B->update (1, M,l2,r2,val);if(X==L1&AMP;&AMP;Y==R1) {A->update (1, M,l2,r2,val);return; }if(R1<=mid) ls->update (x,mid,l1,r1,l2,r2,val);Else if(L1>mid) Rs->update (mid+1, Y,l1,r1,l2,r2,val);ElseLs->update (X,mid,l1,mid,l2,r2,val), Rs->update (mid+1, y,mid+1, R1,l2,r2,val); }intQuery (intXintYintL1,intR1,intL2,intR2) {intMid=x+y>>1;intRe=a->query (1, M,L2,R2);if(X==L1&AMP;&AMP;Y==R1)returnMax (Re, B->query (1, M,L2,R2));if(R1<=mid)returnMax (Re, Ls->query (X,MID,L1,R1,L2,R2));if(L1>mid)returnMax (Re, rs->query (mid+1, Y,L1,R1,L2,R2));returnMax (Max (Ls->query (X,MID,L1,MID,L2,R2), Rs->query (mid+1, y,mid+1, R1,L2,R2)), re); }}*tree=NewAbcdintMain () {intI,x1,y1,x2,y2,h;Cin>>n>>m>>q; Tree->build_tree (1, n); for(i=1; i<=q;i++) {scanf("%d%d%d%d%d", &x2,&y2,&h,&x1,&y1); x2+=x1;y2+=y1;x1++;y1++;intHeight=tree->query (1, n,x1,x2,y1,y2);        Ans=max (ANS,HEIGHT+H); Tree->update (1, n,x1,x2,y1,y2,height+h); }cout<<ans<<endl;return 0;}

Bzoj 1513 POI2006 Tet-tetris 3D Two-dimensional line segment tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.