【KTU Programming Camp (Day 3)】Queries
http://codeforces.com/gym/100739/problem/A
按位考虑,每一位建一个线段树。
求出前缀xor和,对前缀xor和建线段树。
线段树上维护区间内的0的个数和1的个数。
修改就修改p到最后的区间,进行区间取反。
回答询问时把总区间内0的个数和1的个数相乘即可。
时间复杂度\(O(n\log^2n)\)。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 100003;
int a[N], s[N], n, m;
struct node {
node *l, *r;
int sum0, sum1, mark;
node() {
sum0 = sum1 = mark = 0;
l = r = NULL;
}
void pushdown() {
if (mark) {
mark = 0;
if (l) {
l->mark ^= 1;
swap(l->sum0, l->sum1);
}
if (r) {
r->mark ^= 1;
swap(r->sum0, r->sum1);
}
}
}
void count_() {
sum0 = sum1 = 0;
if (l) {
sum0 += l->sum0;
sum1 += l->sum1;
}
if (r) {
sum0 += r->sum0;
sum1 += r->sum1;
}
}
} *rt[15];
node *build_tree(int l, int r, int x) {
node *t = new node;
if (l == r) {
if ((s[l] >> x) & 1) ++t->sum1;
else ++t->sum0;
return t;
}
int mid = ((l + r) >> 1);
t->l = build_tree(l, mid, x);
t->r = build_tree(mid + 1, r, x);
t->count_();
return t;
}
void reserve(node *t, int l, int r, int L, int R) {
if (L <= l && r <= R) {
t->mark ^= 1;
swap(t->sum0, t->sum1);
return;
}
int mid = ((l + r) >> 1);
t->pushdown();
if (mid >= L) reserve(t->l, l, mid, L, R);
if (mid < R) reserve(t->r, mid + 1, r, L, R);
t->count_();
}
int S0, S1;
void count(node *t, int l, int r, int L, int R) {
if (L <= l && r <= R) {
S0 += t->sum0;
S1 += t->sum1;
return;
}
t->pushdown();
int mid = ((l + r) >> 1);
if (mid >= L) count(t->l, l, mid, L, R);
if (mid < R) count(t->r, mid + 1, r, L, R);
}
int main() {
//freopen("a.in", "r", stdin);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i) s[i] = (a[i] ^ s[i - 1]);
for (int i = 0; i < 15; ++i)
rt[i] = build_tree(0, n, i);
int p, x, aa, bb, op;
while (m--) {
scanf("%d", &op);
if (op == 1) {
scanf("%d%d", &p, &x);
for (int i = 0; i < 15; ++i)
if (((a[p] >> i) & 1) != ((x >> i) & 1)) {
reserve(rt[i], 0, n, p, n);
a[p] ^= (1 << i);
}
} else {
scanf("%d%d", &aa, &bb);
int ans = 0;
for (int i = 0; i < 15; ++i) {
S0 = S1 = 0;
count(rt[i], 0, n, aa - 1, bb);
(ans += 1ll * S0 * S1 % 4001 * (1 << i) % 4001) %= 4001;
}
printf("%d\n", ans);
}
}
return 0;
}
【KTU Programming Camp (Day 3)】Queries的更多相关文章
- Codeforces Gym100735 I.Yet another A + B-Java大数 (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)
I.Yet another A + B You are given three numbers. Is there a way to replace variables A, B and C with ...
- Codeforces Gym100735 G.LCS Revised (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)
G.LCS Revised The longest common subsequence is a well known DP problem: given two strings A and B ...
- Codeforces Gym100735 E.Restore (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)
E - Restore Given a matrix A of size N * N. The rows are numbered from 0 to N-1, the columns are num ...
- Codeforces Gym100735 D.Triangle Formation (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)
日常训练题解 D.Triangle Formation You are given N wooden sticks. Your task is to determine how many triang ...
- KTU Programming Camp (Winter Training Day 1)
A.B.C(By musashiheart) 0216个人赛前三道题解 E(By ggg) Gym - 100735E Restore H(by pipixia) Gym - 100735H
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- 【重走Android之路】【Java面向对象基础(三)】面向对象思想
[重走Android之路][基础篇(三)][Java面向对象基础]面向对象思想 1 面向对象的WWH 1.1 What--什么是面向对象 首先,要理解“对象”.在Thinkin ...
- 【ASP.NET Web API教程】5.3 发送HTML表单数据:文件上传与多部分MIME
原文:[ASP.NET Web API教程]5.3 发送HTML表单数据:文件上传与多部分MIME 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面 ...
- 【ASP.NET Web API教程】3.3 通过WPF应用程序调用Web API(C#)
原文:[ASP.NET Web API教程]3.3 通过WPF应用程序调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...
随机推荐
- Linux服务-搭建NFS
任务目标:二进制安装nfs,作为共享存储挂载在三台web的网站根目录下,在任意一台web上修改的结果,其余两台都可以看到 首先来安装NFS服务,NFS顾名思义,就是极品飞车,哦不!是网络文件服务的意思 ...
- Linux基础操作-分区概念
开启Linux系统前添加一块大小为20G的SCSI硬盘 开启系统,右击桌面,打开终端 为新加的硬盘分区,一个主分区大小为10G,剩余空间给扩展分区,在扩展分区上划分两个逻辑分区,大小各5G 进入分区工 ...
- 【洛谷题解】P2303 [SDOi2012]Longge的问题
题目传送门:链接. 能自己推出正确的式子的感觉真的很好! 题意简述: 求\(\sum_{i=1}^{n}gcd(i,n)\).\(n\leq 2^{32}\). 题解: 我们开始化简式子: \(\su ...
- word文档下划线无法显示的解决方法
在编辑文档的时候经常会遇到下划线无法显示的情况,如图: 如果遇到不能在姓名后面加下划线的情况,我们该怎么做? 请看下面的图解: 1.首先点击左上角的office图标 2.点击右下角“word选项” 3 ...
- .net开源框架开源类库(整理)
源:http://www.cnblogs.com/chinanetwind/p/3715809.html 常用库 Json.NET https://github.com/JamesNK/Newtons ...
- Monkeyrunner的相关总结
1.1 monkeyrunner API 主要包括三个模块1.MonkeyRunner:这个类提供了用于连接monkeyrunner和设备或模拟器的方法,它还提供了用于创建用户界面显示提供了方法.2 ...
- day08作业
1.A.在类中的位置不同 成员变量:在类中方法外 局部变量:在方法定义中或者方法声明上 B.在内存中的位置不同 成员变量:在堆内存(成员变量属于对象,对象进堆内存) 局部变量:在栈内存(局部变量属于方 ...
- day06作业
一.方法 1.方法是完成特定功能的代码块. 修饰符 返回值类型 方法类型(参数类型 参数名1,参数类型 参数名2,...){ 方法体语句: return返回值: } 修饰符:目前就用publi ...
- php-fpm和cgi,并发响应的理解以及高并发和多线程的关系
首先搞清楚php-fpm与cgi的关系 cgi cgi是一个web server与cgi程序(这里可以理解为是php解释器)之间进行数据传输的协议,保证了传递的是标准数据. php-cgi php-c ...
- hibernate cascade
默认:none Cascade 属性值: none:在保存.删除修改对象的时候,不考虑其附属物的操作 save-update:在保存.更新当前对象时,级联保存.更新附属物. delete:在删除当前对 ...