#442 Div2 E

题意

给你一棵树,每个结点有开关(0表示关闭,1表示开启),两种操作:

  1. 反转一棵子树所有开关
  2. 询问一棵子树有多少开关是开着的

分析

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

  1. Codeforces #442 Div2 F

    #442 Div2 F 题意 给出一些包含两种类型(a, b)问题的问题册,每本问题册有一些题目,每次查询某一区间,问有多少子区间中 a 问题的数量等于 b 问题的数量加 \(k\) . 分析 令包含 ...

  2. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  3. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  4. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  5. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  6. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

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

  8. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  9. Codeforces #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

随机推荐

  1. 2018牛客多校第一场 B.Symmetric Matrix

    题意: 构造一个n*n的矩阵,使得Ai,i = 0,Ai,j = Aj,i,Ai,1+Ai,2+...+Ai,n = 2.求种类数. 题解: 把构造的矩阵当成邻接矩阵考虑. 那么所有点的度数都为2,且 ...

  2. C++——OOP面向对象理解

    从Rob Pike 的 Google+上的一个推看到了一篇叫<Understanding Object Oriented Programming>的文章,我先把这篇文章简述一下,然后再说说 ...

  3. PHP报错Cannot adopt OID in UCD-SNMP-MIB、 LM-SENSORS-MIB

    Cannot adopt OID in UCD-SNMP-MIB: Cannot adopt OID in LM-SENSORS-MIB: lmTempSensorsValue 运行PHP遇到这些错误 ...

  4. (转)什么是JSON+如何处理JSON字符串

    仍然是crifan的好文: http://www.crifan.com/summary_what_is_json_and_how_to_process_json_string/ . . . .

  5. MySQL rpm 版本安装

     准备: [root@localhost moudles]# ls MySQL-client-5.6.36-1.linux_glibc2.5.x86_64.rpm MySQL-server-5.6.3 ...

  6. Spring学习-- IOC 容器中 bean 的生命周期

    Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...

  7. 【BZOJ】5010: [Fjoi2017]矩阵填数

    [算法]离散化+容斥原理 [题意]给定大矩阵,可以每格都可以任意填1~m,给定n个子矩阵,要求满足子矩阵内的最大值为vi,求方案数. n<=10,h,w<=1w. [题解] 此题重点之一在 ...

  8. Node.js 编码转换

    Node.js自带的toString()方法不支持gbk,因此中文转换的时候需要加载第三方库,推荐以下两个编码转换库,iconv-lite和encoding.       iconv, iconv-l ...

  9. usaco 2000 contest 滑雪

    2013-09-11 10:22 [题目大意]给定N个点的高度和M条相连的路线(单向),从最高点向下走, 到无法走时为一条路径,求不同的路径数,(一节点不同就叫不同) [输入样例] 4 5   (N, ...

  10. Guice 注入(@Inject注解)

    带有@Inject注解的类 使用 injector.getInstance初始化 http://blog.csdn.net/java_le/article/details/24851251 Googl ...