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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. KTU Programming Camp (Winter Training Day 1)

    A.B.C(By musashiheart) 0216个人赛前三道题解 E(By ggg) Gym - 100735E Restore H(by pipixia) Gym - 100735H

  6. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  7. 【重走Android之路】【Java面向对象基础(三)】面向对象思想

    [重走Android之路][基础篇(三)][Java面向对象基础]面向对象思想   1 面向对象的WWH   1.1 What--什么是面向对象         首先,要理解“对象”.在Thinkin ...

  8. 【ASP.NET Web API教程】5.3 发送HTML表单数据:文件上传与多部分MIME

    原文:[ASP.NET Web API教程]5.3 发送HTML表单数据:文件上传与多部分MIME 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面 ...

  9. 【ASP.NET Web API教程】3.3 通过WPF应用程序调用Web API(C#)

    原文:[ASP.NET Web API教程]3.3 通过WPF应用程序调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...

随机推荐

  1. SQL SERVER 视图优化经历

    系统中要求对HIS数据进行效益统计,因为HIS数据是需要第三方提供接口导入的,不清楚数据量大小,所以视图以业务为主未对其做性能优化(当时编写试图时就是几条简单的测试数据) 如今在项目接口实施完成后查看 ...

  2. python作业ATM(第五周)

    作业需求: 额度 15000或自定义. 实现购物商城,买东西加入 购物车,调用信用卡接口结账. 可以提现,手续费5%. 支持多账户登录. 支持账户间转账. 记录每月日常消费流水. 提供还款接口. AT ...

  3. 【比赛游记】THUSC2018酱油记

    day -1 早上4:30就要起来去飞机场…… 7点的飞机,10:30就到北京了. 北京的街景并没有我想像的漂亮……大概是因为我在四环外〒▽〒 晚上还有CF div3场,果断的去水了,因为太累就没有打 ...

  4. CentOS7.3下的一个iptables配置

    centos7.3默认使用的防火墙应该是firewall,而不是iptables.而我们xxmj服务器使用的是iptables防火墙.所以,在配置防火墙之前,我们需要先关闭firewall,安装ipt ...

  5. C#基础学习之FileStream

    FileStream和File的区别  后者比前者给内存带来压力大. FileStream可以操作字节也就是可以保存任何类型的文件. 1.FileStream读文件操作 //OpenOrCreate: ...

  6. 形参前的&&啥意思?

    C++2011标准的 右值引用 语法 去搜索“c++11右值引用” 右值引用,当传入临时对象时可以避免一次拷贝. 右值引用.举个例子 C/C++ code   ? 1 2 3 4 5 6 7 8 // ...

  7. makefile特殊符号介绍

    http://blog.chinaunix.net/uid-20564848-id-217918.html makefile下$(wildcard $^),$^,$@,$?,$<,$(@D),$ ...

  8. HTML网页自动跳转

    <meta http-equiv="refresh" content="3;URL=res.html">

  9. thinkphp框架if标签条件表达式

    eq 等于neq 不等于gt 大于egt 大于等于lt 小于elt 小于等于

  10. Unix IPC之pipe

    pipe创建函数: #include <unistd.h> /* Create a one-way communication channel (pipe). If successful, ...