Codeforces 242e. XOR on segment

Source: Internet
Author: User
There are two kinds of operations to give a sequence. One is to calculate the sum of L to R, the other is to make all the numbers of L to R and X do the exclusive or operation. Practice: Obviously, direct violence is impossible (but when this question was just launched, many people used violent water, and then strengthened data, I think of a line segment tree again .. But this is not simple. What should I do with an exception or operation? Or a bitwise operation. If the J-th of X is 1, the J-th of each number from L to R is reversed, (0 ^ 1 = ^ 1 = 0). If it is 0, it remains unchanged. Since it is a bit operation, can each bit be separately maintained as a line segment tree? It seems okay! An operation is equivalent to an interval operation. Do you only need to reverse some bits from L to R? If it is a reverse operation or something, it is not okay to add the lazy tag. The sum operation can also calculate the number of 1 s on each bit from L to R to calculate the final sum. Well, let's take a look. First, determine the number of Line Segment trees to be created. The number range is 10 ^ 6. That is to say, we only need to create 20 line segment trees at most, then write the bulid, update, and query functions of the Line Segment tree. Two values must be maintained on each node. One is the number of values in the interval (For summation), and the other is whether the interval is reversed (lazy tag ).
Code:
# Include <iostream >#include <cstdio >#include <algorithm> # define n 100010 # define L (x) <1 # define R (x) (X) <1 | 1 using namespace STD; struct node {int L, R, CNT, lazy; node (long LL, long RR, long C, long la) {L = ll, r = RR, CNT = C, lazy = La;} node () {L = r = CNT = lazy = 0 ;}} P [20] [N * 4]; long a [n] [20], num [N], two [22]; long bulid (long ID, long long l, long R, long Na) {If (L = r) {P [Na] [ID] = node (L, R, A [l] [Na], 0); return a [l] [Na];} long mid = (L + r)/2; long long cur = bulid (L (ID), L, mid, Na) + bulid (R (ID), Mid + 1, R, Na ); P [Na] [ID] = node (L, R, cur, 0); Return cur;} void revers (long ID, long l, long R, long long Na) {If (P [Na] [ID]. L = L & P [Na] [ID]. R = r) {P [Na] [ID]. lazy = 1-P [Na] [ID]. lazy; P [Na] [ID]. CNT = P [Na] [ID]. r-p [Na] [ID]. L + 1-P [Na] [ID]. CNT; return;} If (P [Na] [ID]. lazy) // place the lazy tag {P [Na] [ID]. lazy = 0; P [Na] [L (ID)]. lazy = 1-P [Na] [L (ID)]. lazy; P [Na] [R (ID)]. lazy = 1-P [Na] [R (ID)]. lazy; P [Na] [L (ID)]. CNT = P [Na] [L (ID)]. r-p [Na] [L (ID)]. L + 1-P [Na] [L (ID)]. CNT; P [Na] [R (ID)]. CNT = P [Na] [R (ID)]. r-p [Na] [R (ID)]. L + 1-P [Na] [R (ID)]. CNT;} long mid = (P [Na] [ID]. L + P [Na] [ID]. r)/2; If (mid <L) revers (R (ID), L, R, Na); else if (mid> = r) revers (L (ID ), l, R, Na); else revers (L (ID), L, mid, NA), revers (R (ID), Mid + 1, R, Na ); P [Na] [ID]. CNT = P [Na] [L (ID)]. CNT + P [Na] [R (ID)]. CNT;} long query (long ID, long l, long R, long Na) {If (P [Na] [ID]. L = L & P [Na] [ID]. R = r) return P [Na] [ID]. CNT; If (P [Na] [ID]. lazy) {P [Na] [ID]. lazy = 0; P [Na] [L (ID)]. lazy = 1-P [Na] [L (ID)]. lazy; P [Na] [R (ID)]. lazy = 1-P [Na] [R (ID)]. lazy; P [Na] [L (ID)]. CNT = P [Na] [L (ID)]. r-p [Na] [L (ID)]. L + 1-P [Na] [L (ID)]. CNT; P [Na] [R (ID)]. CNT = P [Na] [R (ID)]. r-p [Na] [R (ID)]. L + 1-P [Na] [R (ID)]. CNT;} long mid = (P [Na] [ID]. L + P [Na] [ID]. r)/2; If (mid> = r) return query (L (ID), L, R, Na); else if (mid <L) return query (R (ID), L, R, Na); else return query (L (ID), L, mid, Na) + query (R (ID ), mid + 1, R, Na) ;}int main () {two [0] = 1; for (long I = 1; I <21; I ++) two [I] = two [I-1] * 2; long n; scanf ("% i64d", & N); For (long I = 1; I <= N; I ++) {scanf ("% i64d", & num [I]); long J = 0; while (Num [I]> 0) {A [I] [J ++] = num [I] & 1; num [I] >>=1 ;}} for (long I = 0; I <20; I ++) Bulid (1, 1, n, I); long m; scanf ("% i64d", & M); While (M --) {long ty; scanf ("% i64d", & ty); If (TY = 1) {long a, B; scanf ("% i64d % i64d ", & A, & B); long cur = 0; For (long I = 0; I <20; I ++) cur + = query (1, A, B, i) * Two [I]; cout <cur <Endl;} else {long a, B, X; scanf ("% i64d % i64d % i64d ", & A, & B, & X); For (long I = 0; I <20 & X; I ++, x> = 1) {If (X & 1) revers (1, A, B, I) ;}} return 0 ;}


Codeforces 242e. XOR on segment

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.