树状数组,与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. 5.1---二进制数插入(CC150)

    public class Solution { public static int binInsert(int n, int m, int i, int j) { // write code here ...

  2. div 自动满屏

    通常通过jq来做,类似这样: $('#navigation').css({ height: $(window).innerHeight() }); css3后,只需要用 下面这段样式即可 #navig ...

  3. bootstrap框架----像素

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 页面遮罩层,并且阻止页面body滚动。bootstrap模态框原理

    实现思路: 1.需要有一个层将body遮住,放在body上方. 2.修改body的overflow属性值为:hidden 废话不多说了,将关键代码贴出来了,兼容火狐,谷歌,ie 遮罩层的样式代码,红色 ...

  5. python之errno

    http://www.cnblogs.com/Security-Darren/p/4168392.html errno.EWOULDBLOCK 操作将会阻塞

  6. Effective C++ -----条款41:了解隐式接口和编译期多态

    classes和templates都支持接口(interface)和多态(polymorphism). 对classes而言接口是显式的(explicit),以函数签名为中心.多态则是通过virtua ...

  7. HDU 4862 Jump(最小K路径覆盖)

    输入一个n×m网格图,每个结点的值为0-9,可以从任意点出发不超过k次,走完每个点且仅访问每个结点一次,问最终的能量最大值.不可全部走完的情况输出-1. 初始能量为0. 而结点(x,y)可以跳跃到结点 ...

  8. codeforces 591A. Wizards' Duel 解题报告

    题目链接:http://codeforces.com/problemset/problem/591/A 题目意思:其实看下面这幅图就知道题意了,就是Harry 和 He-Who-Must-Not-Be ...

  9. C#关于new的用法

    1.运算符就是在实例化一个类的时候(运算符的用法) A a=new A(); 2.new 约束指定泛型类声明中的任何类型参数都必须有公共无参数构造函数.当泛型类创建类型的新实例时,将此约束应用于类型参 ...

  10. Android 中沉浸式状态栏实现

    Android 中沉浸式状态栏实现方式如下 计算状态栏高度及调用沉浸式状态栏的相关API方法 package com.example.status; import android.annotation ...