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$之间对答案的贡献.由于逆序对考虑的是数字之间的大小关系,故假设$- ...
随机推荐
- 一个按比特位拷贝数据的函数copybits
一个按比特位拷贝数据的函数 没有进行特别的优化.其实还可以在拷贝源开始位置和目标开始位置是2的整数倍位置的时候进行优化. 说明 这个函数用于从src数组首地址跳过sbb个字节,又跳过ssb个比特位,拷 ...
- struts2回显指定的错误信息
<s:fielderror /> 显示全部的 错误消息(用addFieldError方法添加的 ) <s:fielderror> <s:pa ...
- WinForm多线程编程简单Demo
需要搭建一个可以监控报告生成的CS(WinForm)工具,即CS不断Run,执行获取数据生成报告,经过研究和实践,选择了使用"WinForm多线程编程"的解决方案.当然参考了园中相 ...
- js数组转置
<script type="text/javascript"> var arr=[[1,2,3],[4,5,6],[7,8,9],[17,18,19]]; ...
- java视频转码博客
一下为找到的资料地址 http://lichen.blog.51cto.com/697816/162124 http://www.cnblogs.com/live365wang/archive/201 ...
- SprignMVC+myBatis整合
转载自:http://lydia-fly.iteye.com/blog/2153076 学习本节内容请先看"MyBatis的基本应用".地址:http://lydia-fly.it ...
- xcode 自定义Eclipse里边常用的快捷键
之前在用Eclipse写Java的 时候,有几个常用的快捷键,比如删除当前行,在当前行下面插入空行,向上/下移动当前行等等,到了Xcode里怎么也找不到这些快捷键,一直觉得 Xcode自带的快捷键不够 ...
- mysql常用函数整理
一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 ) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 SELECT CEIL( ...
- storyboard pushViewController 的时候,新的界面黑屏
storyboard 创建的一级界面需要通过代码跳转到另一 storyboard 创建的界面的时候,通常我们会这样 其实 alloc init 相当于重新创建一个界面,所以我们 push 进入之后会发 ...
- [Android Pro] app_process command in Android
reference to : http://blog.csdn.net/wangkaiblog/article/details/46050587 本来以为存放在/systen/bin/下的monkey ...