【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系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...
随机推荐
- js如何查看元素类型
<script type="text/javascript"> //定义变量temp var temp = Object.prototype.toString.appl ...
- shell系统检测->
系统状态检测脚本练习 1-> 查看磁盘状态 思路:查看磁盘/当前使用状态,如果使用率超过80%则报警发邮件 1.获取磁盘当前使用的值 df -h|grep /$ 2.从获取到的值中提取出,对应的 ...
- mysql取以当前时间为中心的任意时间段的时间戳
例如:取当前时间后一年的时间戳 SELECT UNIX_TIMESTAMP(date_sub(curdate(),interval -1 YEAR)) SELECT UNIX_TIMESTAMP(da ...
- 安卓ios各版本及分辨率占比
Google Play 安装统计数据 只有安卓的 https://developer.android.com/about/dashboards/index.html?hl=zh-cn 腾讯移动分析 安 ...
- java基础69 JavaScript产生伪验证码(网页知识)
1.伪验证码 <!doctype html> //软件版本:DW2018版 <html> <head> <meta charset="utf-8&q ...
- Centos之帮助命令
帮助命令man (manual) 比如我们可以看下man命令的解释 [root@localhost ~]# man man MAN(1) ...
- MP3 Fuzz学习
这篇文章主要是学习一波MP3格式fuzz的知识.目录如下 0x0.MP3格式的构成 0x0.MP3格式的构成 MP3是一种通俗叫法,学名叫MPEG1 Layer-3.MP3是三段式的结构,依次由ID3 ...
- CCF CSP 201403-2 窗口
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201403-2 窗口 问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标 ...
- 【洛谷】P4643 【模板】动态dp
题解 在冬令营上听到冬眠的东西,现在都是板子了猫锟真的是好毒瘤啊(雾) (立个flag,我去thusc之前要把WC2018T1乱搞过去= =) 好的,我们可以参考猫锟的动态动态dp的课件,然后你发现你 ...
- Ajax的text/plain、application/x-www-form-urlencoded和application/json
Ajax的text/plain.application/x-www-form-urlencoded和application/json HTTP请求中,如果是get请求,那么表单参数以name=valu ...