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. 判断最小生成树是否为一(krustra)

    题目链接:https://vjudge.net/contest/66965#problem/K 具体思路: 首先跑一遍最短路算法,然后将使用到的边标记一下,同时使用一个数组记录每一个权值出现的次数,如 ...

  2. ubuntu 14.04安装JDK

    As a workaround, you can install OpenJDK 8 from a PPA repository: 1. Open terminal from the Dash or ...

  3. MySQL防范SQL注入风险

    MySQL防范SQL注入风险 0.导读 在MySQL里,如何识别并且避免发生SQL注入风险 1.关于SQL注入 互联网很危险,信息及数据安全很重要,SQL注入是最常见的入侵手段之一,其技术门槛低.成本 ...

  4. aarch64_n2

    nodejs-is-dotfile-1.0.2-2.fc26.noarch.rpm 2017-02-12 00:27 9.5K fedora Mirroring Project nodejs-is-e ...

  5. linux文件管理 -> 系统压缩打包

    如果希望windows和Linux互相能使用的压缩工具, 建议.zip格式 压缩的好处主要有: 节省磁盘空间占用率 节省网络传输带宽消耗 网络传输更加快捷 Linux系统常见的后缀名所对应的压缩工具 ...

  6. Ubuntu下使用Nginx+uWSGI+Flask(初体验)

    Ubuntu 18.04,Nginx 1.14.0, uWSGI 2.0.17.1,Flask, 前言 Windows不支持uWSGI!为了上线自己的项目,只能选择Linux. 自己前面开发了一个Fl ...

  7. Python版飞机大战

    前面学了java用java写了飞机大战这次学完python基础后写了个python版的飞机大战,有兴趣的可以看下. 父类是飞行物类是所有对象的父类,setting里面是需要加载的图片,你可以换称自己的 ...

  8. Filebeat入门

    一.安装filebeat 简介 Beats 是安装在服务器上的数据中转代理. Beats 可以将数据直接传输到 Elasticsearch 或传输到 Logstash . Beats 有多种类型,可以 ...

  9. CVE-2012-0003 Microsoft Windows Media Player ‘winmm.dll’ MIDI文件解析远程代码执行漏洞 分析

    [CNNVD]Microsoft Windows Media Player ‘winmm.dll’ MIDI文件解析远程代码执行漏洞(CNNVD-201201-110)    Microsoft Wi ...

  10. PowerTool x64驱动模块逆向分析(持续更新)

    比赛打完了,来继续搞了,因为那个主动防御正在写,所以想找找思路正好想到可以来逆向一下PT的驱动模块看看pt大大是怎么写的程序. PT x64版本的驱动模块是这个kEvP64.sys. 0x0 先来看看 ...