Codeforces 617 E. XOR and favorite number (Mo team algorithm)

Source: Internet
Author: User

Title Link: Http://codeforces.com/problemset/problem/617/E

Title:

Here you A1 A2 A3 An number, M inquiry: in [L, R] inside how much [L, R] makes Al xor al+1 xor AR is k.

The following:

in the subject only interval query has no interval modification, and the amount of data is small (10w   ), so it can be solved in an offline way.

Using the MO team algorithm to solve, you need O (1) Modification [L, r+1], [L, R-1], [L+1, R], [L-1, R].

Detailed MO team can learn a bit Baidu.

Let's talk about how to modify it through O (1).

1) We need to ask for the prefix XOR.  

2) [L, R], [L, R+1]: Here Add a ar+1, you can pass the prefix xor T, so we need to find the number of T ^ k H (because h^t=k-> h=t^k).

If H appears before, for example (all prefix XOR) 1 2 1[added]. At this point the XOR is 1, we when K is 0, then the number of 0^1 = 1 is 1.

What is the meaning of the 1 number here? If we want to think of 1 [2 1] This paragraph of XOR, should not be T3 (1) XOR T1 (1) = 0 = k;

[L, R], [L, R-1]: Here is the deletion of an AR, Num[tar]--, we ask for the number of k^ TaR.

1 2 1 1[Delete], k = 0, 0^1 = 1, this has 2 1. Here 2 means 1 [2 1 1] (T4 xor T1 = 0) and 1 2 1 [1] (T4 xor T3 = 0), since the AR ending interval is subtracted from the AR.

[L, R], [L-1, R]: This is to add one more aL-1. In [L-1, R] This is looking for [L-1, R (R<r)] to make XOR K, so we need k^ ((L-1)-1).

[L, R], [L+1, R]: Here is the removal of AL. is to find the XOR K in [L, R (R<r)]. T[R]^T[L-1] = k.

Num[0] = 1. Here is the prefix that should be different for the beginning or 0.

 

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <string>5#include <algorithm>6#include <cmath>7#include <vector>8#include <queue>9#include <stack>Ten#include <Set> One using namespacestd; AtypedefLong LongLL; - #defineMS (A, b) memset (A, B, sizeof (a)) - #definePB Push_back the #defineMP Make_pair - Const intINF =0x7fffffff; - Const intINF =0x3f3f3f3f; - Const intMoD = 1e9+7; + Const intMAXN =100000+Ten; - intA[MAXN]; +LL num[3000010]; A LL X[MAXN]; at LL ANS[MAXN]; - struct Point - { -     intL, R, id; - }NODE[MAXN]; - intunit, N, M, K; in voidinit () { -MS (A,0); toMS (ANS,0); +MS (NUM,0); -MS (X,0); the } * BOOLCMP (Point x1, point x2) $ {Panax Notoginseng     if(X1.l/unit! = x2.l/unit) { -         returnX1.l/unit < x2.l/Unit; the     } +     Else A         returnX1.R <X2.R; the } + voidWork () - { $      for(inti =1; i<=n;i++) $X[i] = x[i-1]^A[i]; -  -     intL, R; theLL temp =0; -L =1, R =0;Wuyinum[0]++; the      for(inti =0; i<m;i++){ -          while(r<NODE[I].R) { Wur++; -temp+=num[k^X[r]]; Aboutnum[x[r]]++; $         } -          while(r>NODE[I].R) { -num[x[r]]--; -temp-=num[x[r]^K]; Ar--; +         } the          while(l>NODE[I].L) { -l--; $temp+=num[k^x[l-1]]; thenum[x[l-1]]++; the         } the          while(l<NODE[I].L) { thenum[x[l-1]]--; -temp-=num[x[l-1]^K]; inl++; the         } theAns[node[i].id] =temp; About     } the } the voidsolve () { thescanf"%d%d%d", &n, &m, &k); +Unit = (int) sqrt (n); -      for(inti =1; i<=n;i++) scanf ("%d", &a[i]); the      for(inti =0; i<m;i++){BayiNode[i].id =i; thescanf"%d%d", &AMP;NODE[I].L, &NODE[I].R); the     } -Sort (node, node+m, CMP); - Work (); the      for(inti =0; i<m;i++){ theprintf"%lld\n", Ans[i]); the     } the } - intMain () { the #ifdef LOCAL theFreopen ("Input.txt","R", stdin); the //freopen ("Output.txt", "w", stdout);94 #endif the //ios::sync_with_stdio (0); the //cin.tie (0); the 98 init (); About solve (); - 101     return 0;102}
View Code

Codeforces 617 E. XOR and favorite number (Mo team algorithm)

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.