[CF#365 (Div. 2) Mishka and Interesting sum] 线段树离线处理区间不同数
来源:互联网 发布:ubuntu如何卸载jdk 编辑:程序博客网 时间:2024/05/16 09:30
[CF#365 (Div. 2) Mishka and Interesting sum] 线段树离线处理区间不同数
题目链接:[CF#365 (Div. 2) Mishka and Interesting sum]
题意描述:给定N个数a1, a2, ..., an , M次查询。每次查询一个区间[l, r] 中出现偶数次的数字的异或和。
解题思路: 如果求出现奇数次数字的异或和, 那么利用异或的性质,直接一个前缀异或和就分分钟搞定。出现偶数次数字的异或和可以这么考虑:
首先呢,还是求出前缀异或和。
然后呢, 求出区间不同数的异或和,即区间中每个数字不管出现多少次, 都做一次处理,求出区间异或和。嘿嘿嘿。给[hdu3333 Turing Tree] 思路一样。详情请转到我的上一篇博客[hdu3333 Turing Tree] 线段树离线 处理区间不同数。
然后呢,求区间[L, R] 的答案就是区间中所有数字的异或和(可以用之前的前缀异或和求出) 再异或 区间不同数的异或和。
#include <map>#include <cmath>#include <queue>#include <vector>#include <cstdio>#include <string>#include <cstring>#include <iomanip>#include <iostream>#include <algorithm>using namespace std;//#pragma comment(linker, "/STACK:1024000000,1024000000")#define FIN freopen("input.txt","r",stdin)#define FOUT freopen("output.txt","w",stdout)#define fst first#define snd second#define lson l, mid, rt << 1#define rson mid + 1, r, rt << 1 | 1typedef __int64 LL;//typedef long long LL;const int MAXN = 1000000 + 5;const int MAXM = 1000000 + 5;struct Node { int L, R, ID; Node () {} Node (int L, int R, int ID) : L (L), R (R), ID (ID) {} bool operator < (const Node& e) const { if (R == e.R) return L < e.L; return R < e.R; }} inp[MAXM];int N, M;int A[MAXN], pre[MAXN], seg[MAXN << 2];int res[MAXM];map<int, int> pos;inline void pushUp (const int& rt) { seg[rt] = seg[rt << 1] ^ seg[rt << 1 | 1];}void build (int l, int r, int rt) { memset (seg, 0x00, sizeof (seg) );}LL query (int L, int R, int l, int r, int rt) { if (L <= l && r <= R) { return seg[rt]; } int mid = (l + r) >> 1; LL ret = 0LL; if (L <= mid) ret ^= query (L, R, lson); if (R > mid) ret ^= query (L, R, rson); return ret;}void update (int p, int v, int l, int r, int rt) { if (l == r) { seg[rt] = v; return; } int mid = (l + r) >> 1; if (p <= mid) update (p, v, lson); else update (p, v, rson); pushUp (rt);}int main() {#ifndef ONLINE_JUDGE FIN;#endif // ONLINE_JUDGE int L, R, ID; while (~scanf ("%d", &N) ) { memset (pre, 0, sizeof (pre) ); for (int i = 1; i <= N; i++) { scanf ("%d", &A[i]); pre[i] = A[i] ^ pre[i - 1]; } scanf ("%d", &M); for (int i = 1; i <= M; i++) { scanf ("%d %d", &inp[i].L, &inp[i].R); inp[i].ID = i; } sort (inp + 1, inp + M + 1); build (1, N, 1); pos.clear(); for (int i = 1, lst = 1, p; i <= M; i++) { L = inp[i].L, R = inp[i].R, ID = inp[i].ID; while (lst <= R) { p = pos[A[lst]]; if (p) { update (p, 0, 1, N, 1); } update (lst, A[lst], 1, N, 1); pos[A[lst]] = lst; lst ++; } res[ID] = pre[R] ^ pre[L - 1] ^ query (L, R, 1, N, 1); } for (int i = 1; i <= M; i++) { printf ("%d\n", res[i]); } } return 0;}
1 0
- [CF#365 (Div. 2) Mishka and Interesting sum] 线段树离线处理区间不同数
- CF#365-D. Mishka and Interesting sum-树状数组/线段树离线处理区间查询
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum(离线线段树)
- 【Codeforces Round 365 (Div 2)D】【离线询问 树状数组 前驱思想】Mishka and Interesting sum 区间内出现次数偶数的数的异或和
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum(离线树状数组)
- Codeforces Round #365 (Div. 2) D Mishka and Interesting sum (离线树状数组)
- Codeforces Round #365 (Div. 2)D. Mishka and Interesting sum
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum
- Codeforces Round #365 (Div. 2)D - Mishka and Interesting sum
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum
- Codeforces Round #365 (Div. 2) [D] Mishka and Interesting sum
- Codeforces Round #365 (Div. 2)D. Mishka and Interesting sum
- codeforces D. Mishka and Interesting sum 求区间内不同数的异或值
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线操作,树状数组,last[value],异或和
- CF 703D Mishka and Interesting sum
- [CF 703D]Mishka and Interesting sum
- Codeforces 703D Mishka and Interesting sum (树状数组求区间内不同的数的异或和)
- Codeforces Round #365 (Div. 2) Problem D.Mishka and Interesting sum 解题报告
- 盒模型再回顾:外边距折叠与怪异盒模型
- C++空类编译器自动生成的6个成员函数
- Android 点击按钮实现控件显示隐藏
- 用Python在训练好的log文件中提取出数据并画图
- 001
- [CF#365 (Div. 2) Mishka and Interesting sum] 线段树离线处理区间不同数
- 前台如何调用MD5加密
- C# 通过IHttpModule来实现Url Rewrite,且Session有效
- Android 反编译与混淆技术(上)
- Python的time模块datetime模块
- Redis之——jedis高版本的JedisPoolConfig没有maxActive和maxWait
- 350.leetcode Intersection of Two Arrays II(easy)[数组 归并排序]
- HDU1083+POJ1469 (匈牙利算法+最大二分匹配)
- 001