FZU-2105 Digits Count (two tagged as segment update)

Source: Internet
Author: User

The main topic: to the number of n 0~15, there are 3 kinds of update operations, 1 kinds of inquiry operation. 3 Kinds of update operations are: 1, let a closed interval of all the numbers and a 0~15 between the number of logic and operation, 2, let a closed interval of all the numbers and a 0~15 between the number of logic or operation; 3, let all the number of a closed interval and a 0~15 between the number of the XOR operation. One type of inquiry is to ask for the sum of all the numbers in a closed interval.

Title Analysis: All the input numbers are between 0~15, each of the binary can establish a line tree, build four trees. There are actually only two of the 3 update operations, namely interval and interval inversion. With two lazy tags, when the interval is updated, to clear the existing reversal markers, so that the mark under the two tags under the sequence of transmission.

The code is as follows:

# include<iostream># include<cstdio># include<cstring># include<algorithm>using namespace std;# define LL long longconst int n=1000000;int n,m;int a[n+5];int tr[4][n*4+5];int lazy[4][n*4+5];int lazy_xor[4][N*4+5 ];char com[5];void pushdown (int id,int rt,int l,int r) {int mid=l+ (r-l)/2;if (lazy[id][rt]!=-1) {lazy_xor[id][rt<< 1]=lazy_xor[id][rt<<1|1]=0;lazy[id][rt<<1]=lazy[id][rt<<1|1]=lazy[id][rt];tr[id][rt<<1 ]=lazy[id][rt]* (mid-l+1); tr[id][rt<<1|1]=lazy[id][rt]* (r-mid); lazy[id][rt]=-1;} int &q=lazy_xor[id][rt];if (q==1) {lazy_xor[id][rt<<1]^=1;lazy_xor[id][rt<<1|1]^=1;tr[id][rt< <1]=mid-l+1-tr[id][rt<<1];tr[id][rt<<1|1]=r-mid-tr[id][rt<<1|1];q=0;}} void pushup (int id,int RT) {tr[id][rt]=tr[id][rt<<1]+tr[id][rt<<1|1];} void build (int id,int rt,int l,int r) {lazy[id][rt]=-1;lazy_xor[id][rt]=0;if (l==r) {if (a[r]& (1<<id)) >0) Tr[id][rt]=1;else tr[id][rt]=0;} Else{int mid=l+ (r-l)/2;Build (Id,rt<<1,l,mid); build (Id,rt<<1|1,mid+1,r);p ushup (ID,RT);}} void update (int id,int rt,int l,int r,int l,int r,int x) {if (l<=l&&r<=r) {if (x==2) {if (lazy[id][rt]!=-1) Lazy[id][rt]^=1;else lazy_xor[id][rt]^=1;tr[id][rt]= (r-l+1)-tr[id][rt];} else{lazy[id][rt]=x;lazy_xor[id][rt]=0;tr[id][rt]=x* (r-l+1);}} Else{pushdown (id,rt,l,r), int mid=l+ (r-l)/2;if (l<=mid) Update (ID,RT&LT;&LT;1,L,MID,L,R,X), if (r>mid) update ( id,rt<<1|1,mid+1,r,l,r,x);p ushup (ID,RT);}} LL query (int id,int rt,int l,int r,int l,int R) {if (l<=l&&r<=r) return TR[ID][RT];p ushdown (id,rt,l,r); int mid=l+ (r-l)/2; LL res=0;if (L<=mid) res+=query (id,rt<<1,l,mid,l,r); if (R>mid) res+=query (id,rt<<1|1,mid+1,r,l,r) ; return res;} int main () {//freopen ("In.txt", "R", stdin), int t;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m); for (int I=0;i<n;++i) scanf ("%d", A+i), for (int i=0;i<4;++i) build (i,1,0,n-1); int O,b,c;while (m--) {scanf ("%s", com); if ( com[0]== ' S ') {scanf ("%d%d", & b,&c); int ans=0;for (int i=0;i<4;++i) {ans+=query (i,1,0,n-1,b,c) * (1<<i);} printf ("%d\n", ans);} ELSE{SCANF ("%d%d%d", &o,&b,&c), if (com[0]== ' X ') {for (int i=0;i<4;++i) if (o& (1<<i)) Update (i , 1,0,n-1,b,c,2);} else if (com[0]== ' O ') {for (int i=0;i<4;++i) if (o& (1<<i)) update (i,1,0,n-1,b,c,1);} else if (com[0]== ' A ') {for (int i=0;i<4;++i) if (! ( o& (1<<i))) update (i,1,0,n-1,b,c,0);}}} return 0;}

  

FZU-2105 Digits Count (two tagged as segment update)

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.