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$之间对答案的贡献.由于逆序对考虑的是数字之间的大小关系,故假设$- ...
随机推荐
- 33 网络相关函数(一)——live555源码阅读(四)网络
33 网络相关函数(一)——live555源码阅读(四)网络 33 网络相关函数(一)——live555源码阅读(四)网络 简介 1)IsMulticastAddress多播(组播)地址判断函数 多播 ...
- python入门教程链接
python安装 选择 2.7及以上版本 linux: 一般都自带 windows: https://www.python.org/downloads/windows/ mac os: https:/ ...
- 1.6---旋转二维数组,旋转图像像素,旋转矩阵,90度(CC150)
import java.util.*; public class Transform { public int[][] transformImage(int[][] matrix, int n) { ...
- thinkphp save()方法没有数据,保存失败解决办法
thinkphp save()方法没有数据保存返回0,保存失败返回false 可以对返回值判断一下就好 $ret = $model->save($data); //var_dump($ret ...
- 转帖: 使用脚本删除程序(免除在[控制面板]->[添加或删除程序]中的手工操作)
1. 代码:VBS strComputer = "." '这个表示本地计算机 Set objWMIService = GetObject("winmgmts:" ...
- c#.net循环将DataGridView中的数据赋值到Excel中,并设置样式
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel ...
- Linux下如何移除同时在线的用户
Linux下移除同时在线的用户太多时,shell操作会变得比较卡,很多时候经常是直接关闭终端导致不正常退出,一般要等上一段时间才会退出,这个时候主动结束用户进程使用户下线是比较好的方式,方法如下: 使 ...
- 【ACM】hud1166 敌兵布阵(线段树)
经验: cout 特别慢 如果要求速度 全部用 printf !!! 在学习线段树 内容来自:http://www.cnblogs.com/shuaiwhu/archive/2012/04/22/24 ...
- objective-c字典
1 // 初始化一个空字典 2 // NSDictionary *dictionary = [[NSDictionary alloc] init]; 3 // ...
- stdafx.h的作用
// stdafx.h : include file for standard system include files,// or project specific include files th ...