Codeforces #442 Div2 E
#442 Div2 E
题意
给你一棵树,每个结点有开关(0表示关闭,1表示开启),两种操作:
- 反转一棵子树所有开关
- 询问一棵子树有多少开关是开着的
分析
先 DFS 把树上的结点映射到区间上,然后就是线段树区间更新、区间求和了。
code
#include<bits/stdc++.h>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef long long ll;
using namespace std;
const int MAXN = 2e5 + 10;
int n, cnt, sl[MAXN], sr[MAXN], a[MAXN], b[MAXN];
vector<int> G[MAXN];
void dfs(int u) {
sl[u] = ++cnt;
for(int v : G[u]) dfs(v);
sr[u] = cnt;
}
int s[MAXN << 2], lazy[MAXN << 2];
void pushUp(int rt) {
s[rt] = s[rt << 1] + s[rt << 1 | 1];
}
void pushDown(int rt, int len) {
if(lazy[rt]) {
lazy[rt << 1] ^= 1;
lazy[rt << 1 | 1] ^= 1;
s[rt << 1] = len - len / 2 - s[rt << 1];
s[rt << 1 | 1] = len / 2 - s[rt << 1 | 1];
lazy[rt] = 0;
}
}
void build(int l, int r, int rt) {
if(l == r) {
s[rt] = b[a[l]];
} else {
int m = l + r >> 1;
build(lson);
build(rson);
pushUp(rt);
}
}
void update(int L, int R, int l, int r, int rt) {
if(l >= L && r <= R) {
lazy[rt] ^= 1;
s[rt] = r - l + 1 - s[rt];
} else {
pushDown(rt, r - l + 1);
int m = l + r >> 1;
if(m >= L) update(L, R, lson);
if(m < R) update(L, R, rson);
pushUp(rt);
}
}
int query(int L, int R, int l, int r, int rt) {
if(l >= L && r <= R) return s[rt];
else {
pushDown(rt, r - l + 1);
int m = l + r >> 1;
int res = 0;
if(m >= L) res += query(L, R, lson);
if(m < R) res += query(L, R, rson);
pushUp(rt);
return res;
}
}
int main() {
scanf("%d", &n);
for(int i = 2; i <= n; i++) {
int x;
scanf("%d", &x);
G[x].push_back(i);
}
dfs(1);
for(int i = 1; i <= n; i++) scanf("%d", &b[i]);
for(int i = 1; i <= n; i++) a[sl[i]] = i;
build(1, n, 1);
int q;
scanf("%d", &q);
while(q--) {
char c[4];
int x;
scanf("%s%d", c, &x);
if(c[0] == 'g') printf("%d\n", query(sl[x], sr[x], 1, n, 1));
else update(sl[x], sr[x], 1, n, 1);
}
return 0;
}
Codeforces #442 Div2 E的更多相关文章
- Codeforces #442 Div2 F
#442 Div2 F 题意 给出一些包含两种类型(a, b)问题的问题册,每本问题册有一些题目,每次查询某一区间,问有多少子区间中 a 问题的数量等于 b 问题的数量加 \(k\) . 分析 令包含 ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- cf 442 div2 F. Ann and Books(莫队算法)
cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
随机推荐
- 浅谈javascript的原型及原型链
浅谈javascript的原型及原型链 这里,我们列出原型的几个概念,如下: prototype属性 [[prototype]] __proto__ prototype属性 只要创建了一个函数,就会为 ...
- 原生ajax方法封装
/** * @function ajax request * @fields ajaxName:请求名称,method:请求方法,headers:setRequestHeader自定义部分,url:接 ...
- java的URI和URL到底是什么
在我们做开发时,经常有URI和URL弄混的问题,如果当时直接看URI和URL的源码就不可能弄混.首先我总结一下URI和URL的关系:他们的关系是:URL是一种特殊的URI,是URI包括URL, 下面用 ...
- CMDB资产管理系统开发【day26】:02-数据写入待存区
一.资产自动回报数据及个更新流程图 二.表结构注释(NewAssetApprovalZone) class NewAssetApprovalZone(models.Model): "&quo ...
- barba.js 优化页面跳转用户体验
barba.js 原理就是在a页面中显示b页面的内容,样式为刷新,给用户以页面跳转后无刷新体验,注意样式命名,ab页面引用的样式和js要相同 可以在页面之间创建良好的转换,增强用户的体验. 减少HTT ...
- shell编程---变量赋值
echo $filen | awk -F. '{print $3}' 怎么把上边这行脚本产生的字串赋给一个变量啊,实际上会产生一个数, 这个变量用来存这个数.格式应该是怎么写的? a=`echo $ ...
- Eclipse Jetty调试时无法保存js文件
Jetty会使用内存映射文件来缓存静态文件,包括js,css文件. 在Windows下,使用内存映射文件会导致文件被锁定,所以当Jetty启动的时候无法在编辑器对js或者css文件进行编辑. 解决办法 ...
- HDU2066一个人的旅行---(多起点多终点最短路径)
http://acm.hdu.edu.cn/showproblem.php?pid=2066 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memo ...
- request.getParameterValues与request.getParameter的区别
一. 简单的对比 request.getParameter用的比较多,相对熟悉 request.getParameterValues(String name)是获得如checkbox类(名字相同, ...
- 之江学院第0届校赛 qwb与支教 (容斥公式)
description qwb同时也是是之江学院的志愿者,暑期要前往周边地区支教,为了提高小学生的数学水平.她把小学生排成一排,从左至右从1开始依次往上报数. 玩完一轮后,他发现这个游戏太简单了.于是 ...