UVA 12436-rip Van Winkle ' s Code (line tree)

Source: Internet
Author: User

Topic Link: UVa 12436-rip Van Winkle ' s Code

Main topic: Four kinds of operation, see the topic.

Problem-solving ideas: that is, with a line tree to maintain a arithmetic progression, because a linear plus a linear or a arithmetic progression, so for each node record area

The value of the left side of the room, i.e. the first item, and the tolerance. Because there is an S operation, it is necessary to open a tag to record whether the interval value is the same.

#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;typedef Long LongllConst intMAXN =250100;#Define Lson (x) ((x) <<1)#Define Rson (x) (((x) <<1) |intLC[MAXN <<2], RC[MAXN <<2], V[MAXN <<2];ll ND[MAXN <<2], AD[MAXN <<2], S[MAXN <<2];voidPushup (intu);voidPushdown (intu);inline intLengthintu) {returnRc[u]-Lc[u] +1;}inline voidChange (intU, ll a) {V[u] =1; Ad[u] =0;    Nd[u] = A; S[u] = A * length (u);}inline voidMaintain (intU, ll A, ll D) {if(V[u] && lc[u]! = Rc[u])        {pushdown (U);    Pushup (U); } V[u] =0;    Nd[u] + = A;    Ad[u] + = D;    ll n = length (u); S[u] + = A * n + (((n1) * N)/2) * D;}inline voidPushup (intu) {S[u] = S[lson (u)] + S[rson (u)];}inline voidPushdown (intu) {if(V[u])        {Change (Lson (U), Nd[u]);        Change (Rson (U), Nd[u]); V[u] = Nd[u] =0; }Else if(Nd[u] | | ad[u])        {Maintain (Lson (U), Nd[u], ad[u]);        Maintain (Rson (U), Nd[u] + length (Lson (u)) * Ad[u], ad[u]); Nd[u] = Ad[u] =0; }}voidBuild (intUintLintR) {Lc[u] = l;    Rc[u] = r; Nd[u] = Ad[u] = S[u] =0;if(L = = r)return;intMid = (L + r)/2;    Build (Lson (U), L, mid); Build (Rson (U), Mid +1, R); Pushup (u);}voidModifyintUintLintR, ll A, ll D) {if(l <= Lc[u] && Rc[u] <= R) {Maintain (U, a + D * (Lc[u]-L), D);return; } pushdown (U);intMID = (Lc[u] + rc[u])/2;if(L <= mid) Modify (Lson (U), L, R, a, d);if(R > Mid) Modify (Rson (U), L, R, a, d); Pushup (u);}void Set(intUintLintR, ll a) {if(l <= Lc[u] && Rc[u] <= R) {Change (U, a);return; } pushdown (U);intMID = (Lc[u] + rc[u])/2;if(L <= Mid)Set(Lson (U), L, R, a);if(R > Mid)Set(Rson (U), L, R, a); Pushup (u);} ll query (intUintLintR) {if(l <= Lc[u] && Rc[u] <= R)returnS[u];    Pushdown (U); LL ret =0;intMID = (Lc[u] + rc[u])/2;if(L <= mid) ret + = query (Lson (U), L, R);if(R > Mid) ret + = query (Rson (U), L, R); Pushdown (U);returnRET;}intNintMain () { while(~scanf("%d", &n)) {Charop[5];intL, R, X; Build1,1,250000); while(n--) {scanf("%s%d%d", OP, &l, &r);if(op[0] ==' A ') Modify (1, L, R,1,1);Else if(op[0] ==' B ') Modify (1, L, R, R-l +1, -1);Else if(op[0] ==' C ') {scanf("%d", &x);Set(1, L, R, X); }Else                printf("%lld\n", Query (1, L, R)); }    }return 0;}

UVA 12436-rip Van Winkle ' s Code (line 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.