题意:给定一个1e6长度的值域1e9的数组。每次给定询问,询问区间内出现偶数次的数的异或和。

题解:首先很显然,每一次询问的答案,等于这个区间所有不同元素异或和异或上区间异或和。(因为出现偶数次的对区间异或和贡献为0,此时剩下的是出现奇数次的数,在取个补集即为答案)

区间异或和前缀和就好了,那问题转化为求区间不同元素异或和。由于这个东西区间合并很困难,所以在线算法是比较不优雅的。那我们考虑离线算法。我们按询问的右端点为第一关键字排序,然后处理到目前这个右端点位置的last数组,last数组定义为每个数最后出现的位置,然后给每个值对应的last附上它的值,这样我们一个区间求异或和可以得到区间不同元素异或和。树状数组一发就好了。

 #include<bits/stdc++.h>
using namespace std;
#define N 1000000
#define LL long long
#define lowbit(x) ((x) & -(x)) inline LL read() {
LL x = , f = ; char a = getchar();
while(a < '' || a > '') { if(a == '-') f = -; a = getchar(); }
while(a >= '' && a <= '') x = x * + a - '', a = getchar();
return x * f;
} int n, a[N + ], ans[N + ], sum[N + ], Q;
int fen[N + ];
map<int, int> last; struct query {
int id, l, r;
bool operator < (const query & w) const {
return r < w.r;
}
} q[N + ]; inline void add(int pos, int val) { if(!pos) return; for(int i = pos; i <= n; i += lowbit(i)) fen[i] ^= val; } inline int querysum(int l, int r) {
int ret = ;
for(int i = r; i; i -= lowbit(i)) ret ^= fen[i];
for(int i = l - ; i; i -= lowbit(i)) ret ^= fen[i];
return ret;
} int main() {
n = read();
for(int i = ; i <= n; i++) a[i] = read();
for(int i = ; i <= n; i++) sum[i] = sum[i - ] ^ a[i];
Q = read();
for(int i = ; i <= Q; i++) q[i].id = i, q[i].l = read(), q[i].r = read();
sort(q + , q + + Q);
for(int pos = , i = ; i <= Q; i++) {
for(; pos <= q[i].r; pos++) {
add(last[a[pos]], a[pos]);
last[a[pos]] = pos;
add(pos, a[pos]);
}
ans[q[i].id] = querysum(q[i].l, q[i].r) ^ sum[q[i].l-] ^ sum[q[i].r];
}
for(int i = ; i <= Q; i++) printf("%d\n", ans[i]);
return ;
}

最新文章

  1. Step by Step 配置使用HTTPS的ASP.NET Web应用
  2. Android ImageView高度根据图片比例自适应
  3. jquery——彩色投票进度条
  4. Hack技术
  5. 类函数和对象函数 PHP
  6. AXIOM解析XML 详细原理
  7. 汇编Ring 3下实现 HOOK API
  8. nginx负载均衡和反向代理有什么区别
  9. Hierarchy Viewer工具使用
  10. 3.sublime vue 语法高亮插件安装
  11. 机器学习基石11-Linear Models for Classification
  12. iOS 控制器的生命周期(UIController)
  13. python time 和 datetime模块
  14. 权限管理-ACL
  15. seller【2】Mock数据(接口访问配置)
  16. route
  17. a.每个 HTML 文件里开头都有个&lt;!DOCTYPE&gt;
  18. CF GYM 101196 G That’s One Hanoi-ed Teacher
  19. Altium Designer 小记
  20. Python --Redis Hash操作

热门文章

  1. jmeter操作myql数据库
  2. SSH配置免秘钥登录
  3. hoj 2543 (费用流 拆边)
  4. C# Static修饰符的作用
  5. 亚马逊订单api重构 api异常入库 在php中执行python
  6. &lt;2013 12 17&gt; 雅思写作、口语相关
  7. python多线程安全local()
  8. 我的Android进阶之旅------>修改Android签名证书keystore的密码、别名alias以及别名密码
  9. Activity重要函数
  10. make编译二