Mishka and Interesting sum Codeforces Round #365 (树状数组)
树状数组,与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 (树状数组)的更多相关文章
- codeforces 597C (树状数组+DP)
题目链接:http://codeforces.com/contest/597/problem/C 思路:dp[i][j]表示长度为i,以j结尾的上升子序列,则有dp[i][j]= ∑dp[i-1][k ...
- Codeforces 597C. Subsequences (树状数组+dp)
题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...
- CodeForces 371D Vessels(树状数组)
树状数组,一个想法是当往p注水时,认为是其容量变小了,更新时二分枚举,注意一些优化. #include<cstdio> #include<iostream> #include& ...
- 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), ...
- Petya and Array CodeForces - 1042D (树状数组)
D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- hdu5592/BestCoder Round #65 树状数组寻找第K大
ZYB's Premutation Memory Limit: 131072/131072 K (Java/Others) 问题描述 ZYBZYB有一个排列PP,但他只记得PP中每个前缀区间的逆 ...
- Sereja and Brackets CodeForces - 380C (树状数组+离线)
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...
- 2019ICPC 上海网络赛 L. Digit sum(二维树状数组+区间求和)
https://nanti.jisuanke.com/t/41422 题目大意: 给出n和b,求1到n,各数在b进制下各位数之和的总和. 直接暴力模拟,TLE.. 没想到是要打表...还是太菜了. # ...
- Codeforces 1096F(dp + 树状数组)
题目链接 题意: 对于长度为$n$的排列,在已知一些位的前提下求逆序对的期望 思路: 将答案分为$3$部分 $1.$$-1$与$-1$之间对答案的贡献.由于逆序对考虑的是数字之间的大小关系,故假设$- ...
随机推荐
- 项目:DoubleFaceCamera
代码在github: https://github.com/Viyu/DoubleFacedCamera 这个app的想法是:用手机的前置和后置摄像头同时拍摄画面合成输出一张图,把拍摄者和被拍摄者的画 ...
- osc的git服务器简单使用。
1.创建本地仓库 mkdir dir cd dir git init 初始化仓库 2.创建远程仓库 进入远程仓库去创建 3.关联本地仓库与远程仓库 git remote add osc git::.. ...
- django的中间件
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA2EAAAGUCAIAAAAzrr3rAAAgAElEQVR4nOy9d5wcx3kmzAXl80m6zy
- OLA音频变速算法的仿真与剖析
前段时间,在尝试音乐节拍数的提取时,终于有了突破性的进展,效果基本上比市面上的许多商业软件还要好,在作节拍数检测时,高频信息作用不大, 通过重采样减小运算量.重采样让我想起了在学校里面做的变速变调算法 ...
- KVC 与 KVO 理解
KVC 与 KVO 是 Objective C 的关键概念,个人认为必须理解的东西,下面是实例讲解. Key-Value Coding (KVC) KVC,即是指 NSKeyValueCoding,一 ...
- 【Java】嵌套For循环性能优化案例
参考资料:http://cgs1999.iteye.com/blog/1596671 1 案例描述 某日,在JavaEye上看到一道面试题,题目是这样的:请对以下的代码进行优化 for (int i ...
- HTTP 格式
HTTP请求报文和HTTP响应报文 HTTP报文是面向文本的,报文中的每一个字段都是一些ASCII码串,各个字段的长度是不确定的.HTTP有两类报文:请求报文和响应报文. HTTP请求报文 一个HTT ...
- java 调用webservice的各种方法总结
java 调用webservice的各种方法总结 几种流行的开源WebService框架Axis1,Axis2,Xfire,CXF,JWS比较 方法一:创建基于JAX-WS的webservice(包括 ...
- freemodbus-v1.5.0 源码分析
注:转载请注明出处 http://www.cnblogs.com/wujing-hubei/p/5935142.html FreeModbus协议栈作为从机,等待主机传送的数据,当从机接收到一帧完 ...
- Android 自动化测试 常用的命令----随时更新
常用命令分为三类,如下: 1. android android sdk : 打开SDK管理器. android avd : 打开虚拟设备管理器. android --help : 查看帮助信息. 2. ...