树状数组,与Turing Tree类似。

xr[i]表示从1到i的抑或,树状数组维护从1到i每个数只考虑一次的异或,结果为sum(r) ^ sum(l) ^ xr[r] ^ xr[l]

其中xr[r] ^ xr[l] 相当于l + 1到r出现奇数次的数的异或,sum(r) ^ sum(l)表示l + 1到r每个数只考虑一次的异或,则两者异或为出现偶数次的数的异或。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
const int N = 1000008, INF = 0x3F3F3F3F;
#define MS(a, num) memset(a, num, sizeof(a))
#define PB(A) push_back(A)
#define FOR(i, n) for(int i = 0; i < n; i++)
int a[N];
struct Que{
int l, r, i;
}que[N];
int C[N],n;
int ans[N];
int xr[N];
inline int lowbit(int x){
return x&-x;
}
void add(int x, int val){
for(int i=x;i<=n;i+=lowbit(i)){
C[i] ^= val;
}
}
int sum(int x){
int ret = 0;
for(int i=x;i>0;i-=lowbit(i)){
ret^=C[i];
}
return ret;
} bool cmp(const Que &a, const Que &b){
return a.r < b.r;
}
int main(){
int t, q;
scanf("%d", &n);
xr[0] = 0;
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
xr[i] = xr[i - 1] ^a[i];
}
scanf("%d", &q);
for(int i = 0; i < q; i++){
scanf("%d %d", &que[i].l , &que[i].r);
que[i].i = i;
}
sort(que, que + q, cmp);
MS(C, 0 );
map<int, int> vis;
int j = 0;
for(int i = 1; i <= n; i++){
if(vis.find(a[i]) != vis.end()){
int p = vis[a[i]];
add(p, a[i]);
}
vis[a[i]] = i;
add(i, a[i]);
while(j < q && que[j].r == i){
int l = que[j].l - 1, r = que[j].r;
ans[que[j].i] = sum(r) ^ sum(l) ^ xr[r] ^ xr[l];
j++;
}
}
for(int i = 0; i < q; i++){
printf("%d\n", ans[i]);
}
return 0;
}

  

Mishka and Interesting sum Codeforces Round #365 (树状数组)的更多相关文章

  1. codeforces 597C (树状数组+DP)

    题目链接:http://codeforces.com/contest/597/problem/C 思路:dp[i][j]表示长度为i,以j结尾的上升子序列,则有dp[i][j]= ∑dp[i-1][k ...

  2. Codeforces 597C. Subsequences (树状数组+dp)

    题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...

  3. CodeForces 371D Vessels(树状数组)

    树状数组,一个想法是当往p注水时,认为是其容量变小了,更新时二分枚举,注意一些优化. #include<cstdio> #include<iostream> #include& ...

  4. Leetcode 2——Range Sum Query - Mutable(树状数组实现)

    Problem: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...

  5. Petya and Array CodeForces - 1042D (树状数组)

    D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. hdu5592/BestCoder Round #65 树状数组寻找第K大

    ZYB's Premutation    Memory Limit: 131072/131072 K (Java/Others) 问题描述 ZYBZYB有一个排列PP,但他只记得PP中每个前缀区间的逆 ...

  7. Sereja and Brackets CodeForces - 380C (树状数组+离线)

    Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...

  8. 2019ICPC 上海网络赛 L. Digit sum(二维树状数组+区间求和)

    https://nanti.jisuanke.com/t/41422 题目大意: 给出n和b,求1到n,各数在b进制下各位数之和的总和. 直接暴力模拟,TLE.. 没想到是要打表...还是太菜了. # ...

  9. Codeforces 1096F(dp + 树状数组)

    题目链接 题意: 对于长度为$n$的排列,在已知一些位的前提下求逆序对的期望 思路: 将答案分为$3$部分 $1.$$-1$与$-1$之间对答案的贡献.由于逆序对考虑的是数字之间的大小关系,故假设$- ...

随机推荐

  1. gcc编译时对'xxxx'未定义的引用问题

    gcc编译时对’xxxx’未定义的引用问题 gcc编译时对’xxxx’未定义的引用问题 原因 解决办法 gcc 依赖顺序问题 在使用gcc编译的时候有时候会碰到这样的问题,编译为.o(obj) 文件没 ...

  2. UVALive 4949 Risk(二分网络流、SAP)

    n个区域,每个区域有我方军队a[i],a[i]==0的区域表示敌方区域,输入邻接矩阵.问经过一次调兵,使得我方边界处(与敌军区域邻接的区域)士兵的最小值最大.输出该最大值.调兵从i->j仅当a[ ...

  3. POJ 1830 开关问题 (高斯消元)

    题目链接 题意:中文题,和上篇博客POJ 1222是一类题. 题解:如果有解,解的个数便是2^(自由变元个数),因为每个变元都有两种选择. 代码: #include <iostream> ...

  4. 【XLL API 函数】xlAbort

    C API 中有 15个 Excel 回调函数只能使用 Excel4.Excel4v.Excel12.Excel12v 函数调用(或间接的使用框架函数 Excel 或 Excel12f 调用).也就是 ...

  5. oc弹出框显示提示消息

    - (void)ShowHUDTitle:(NSString *)title andDelay:(NSTimeInterval)delayTime { if (HUD) { [HUD removeFr ...

  6. python基础——匿名函数

    python基础——匿名函数 当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便.  在Python中,对匿名函数提供了有限支持.还是以map()函数为例,计算f(x)=x2时 ...

  7. 用Mysqlbinlog备份BinLog文件

    默认情况下, mysqlbinlog读取二进制文件[BinLog]并以文本的方式呈现[text format].mysqlbinlog可以直接地从本地读取Log,也可以读取远程的Log[--read- ...

  8. ASCII 非打印字符

    项目出了问题,因为AscII非打印字符的原因,后来找了一下啊ASCII的非打印字符,总共有31个,然后我们直接全部替换成问号了. 解决方式为先找到非打印字符,这是我从网上找的非打印字符表: 进制 十六 ...

  9. C 替换字符方法--1

    #include "stdafx.h" //linux 底下要去掉这一行 #include <stdio.h> #include<stdlib.h> #in ...

  10. CSS3实现文字抹开特效

    CSS: .column-title { color: #9b9b9b; text-shadow: 1px 1px #d4d4d4;}.column-title:hover { color: #5a5 ...