The art of "luogu P2574"xor--line tree

Source: Internet
Author: User

# Ideas for solving problems

This problem is not difficult, but forgive me the beginning of the silly idea, a moment to reveal to everyone.

Tell me how to do it first.

Obviously, for $0$ and $1$, there are only two variations of XOR.

    • $0$ will become $1$,$1$ and will become $0$.
    • Different or even several times, $0$ and $1$ are unchanged.

That only needs to be changed a few times when the mark is next passed.

OK, let's talk about my stupid operation. I did not give the sum of the child node an XOR when I passed the tag, but only the tag, and did not update the sum value of the parent node when backtracking.

Then I did not have the success of the example, but goose this is not stupid, and changed to do it, right, but, the silly to come, I changed the time did not follow the above to change, but in the update, along the way to the node hit a flag flag, pretend that this period needs to change.

hahaha haha, then I succeeded in getting a good score of T four points.

What a ghost, I actually forgot to update the parent node, sure enough, I am too much food.

I decided to put this code on.

# Attach Code

Put on my dumb-nuts. Error code

#include <iostream>#include<cstring>#include<cstdio>using namespacestd;intN, M;Const intMAXN = 2e5+3;structNode {intL, R, Sum, T, tag, flag;} TREE[MAXN <<2];structTree {#defineLson (k << 1)#defineRson ((k << 1) | 1)Template<typename t> InlinevoidRead (T &x) {x=0; T f =1;Charc =GetChar ();  while(C <'0'|| C >'9') {if(c = ='-') F =-1; c =GetChar ();}  while(c <='9'&& C >='0') {x = x*Ten+ C'0'; c =GetChar ();} X*=F; }    voidBuildintKintllintRR) {TREE[K].L= ll, TREE[K].R =RR; Tree[k].flag= TREE[K].T =0; if(TREE[K].L = =TREE[K].R) {scanf ("%1d", &tree[k].sum); return ; }        intMid = (tree[k].l + TREE[K].R) >>1;        Build (Lson, TREE[K].L, mid); Build (Rson, mid+1, TREE[K].R); Tree[k].sum= Tree[lson].sum +tree[rson].sum; }    voidPush_down (intk) {tree[lson].t+=Tree[k].tag; TREE[RSON].T+=Tree[k].tag; Tree[lson].tag+=Tree[k].tag; Tree[rson].tag+=Tree[k].tag; Tree[k].tag=0; }    voidUpdateintKintLintR) {if(TREE[K].L >= l && TREE[K].R <=R) {tree[k].t++; Tree[k].tag++; return; } Tree[k].flag=1; if(Tree[k].tag) Push_down (k); intMid = (tree[k].l + TREE[K].R) >>1; if(L <=mid) Update (Lson, L, R); if(R >mid) Update (Rson, L, R); }    intQueryintKintLintR) {intAns =0; if(TREE[K].L >= L && tree[k].r <= r && Tree[k].flag = =0) {            if(tree[k].t%2==1)return(tree[k].r-tree[k].l+1)-tree[k].sum; Else returntree[k].sum; }        if(Tree[k].tag) Push_down (k); intMid = (tree[k].l + TREE[K].R) >>1; if(L <= mid) ans + =query (Lson, L, R); if(R > mid) ans + =query (Rson, L, R); returnans;        } Tree () {read (n), read (m); Build (1,1, N); intopt, l, R;  for(intI=1; i<=m; i++{Read (opt), read (L), read (R); if(opt = =0) Update (1, L, R); Elseprintf"%d\n", Query (1, L, R)); }}}t;intMain () {return 0;}

Okay, let's take a look at the correct code.

#include <iostream>#include<cstring>#include<cstdio>using namespacestd;intN, M;Const intMAXN = 2e5+3;structNode {intL, R, Sum, T, tag, flag;} TREE[MAXN <<2];structTree {#defineLson (k << 1)#defineRson ((k << 1) | 1)Template<typename t> InlinevoidRead (T &x) {x=0; T f =1;Charc =GetChar ();  while(C <'0'|| C >'9') {if(c = ='-') F =-1; c =GetChar ();}  while(c <='9'&& C >='0') {x = x*Ten+ C'0'; c =GetChar ();} X*=F; }    voidBuildintKintllintRR) {TREE[K].L= ll, TREE[K].R =RR; Tree[k].flag= TREE[K].T =0; if(TREE[K].L = =TREE[K].R) {scanf ("%1d", &tree[k].sum); return ; }        intMid = (tree[k].l + TREE[K].R) >>1;        Build (Lson, TREE[K].L, mid); Build (Rson, mid+1, TREE[K].R); Tree[k].sum= Tree[lson].sum +tree[rson].sum; }    voidPush_down (intk) {if(Tree[k].tag%2==1) Tree[lson].sum= (tree[lson].r-tree[lson].l+1)-Tree[lson].sum, Tree[rson].sum= (tree[rson].r-tree[rson].l+1)-tree[rson].sum; Tree[lson].tag+=Tree[k].tag; Tree[rson].tag+=Tree[k].tag; Tree[k].tag=0; }    voidUpdateintKintLintR) {if(TREE[K].L >= l && TREE[K].R <=R) {Tree[k].tag++; Tree[k].sum= (tree[k].r-tree[k].l+1)-tree[k].sum; return; }        if(Tree[k].tag) Push_down (k); intMid = (tree[k].l + TREE[K].R) >>1; if(L <=mid) Update (Lson, L, R); if(R >mid) Update (Rson, L, R); Tree[k].sum= Tree[lson].sum +tree[rson].sum; }    intQueryintKintLintR) {intAns =0; if(TREE[K].L >= l && TREE[K].R <=R)returntree[k].sum; if(Tree[k].tag) Push_down (k); intMid = (tree[k].l + TREE[K].R) >>1; if(L <= mid) ans + =query (Lson, L, R); if(R > mid) ans + =query (Rson, L, R); returnans;        } Tree () {read (n), read (m); Build (1,1, N); intopt, l, R;  for(intI=1; i<=m; i++{Read (opt), read (L), read (R); if(opt = =0) Update (1, L, R); Elseprintf"%d\n", Query (1, L, R)); }}}t;intMain () {return 0;}

The art of "luogu P2574"xor--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.