#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. 2017 Multi-University Training Contest - Team 4 phone call(树+lca+并查集)

    题解: (并查集处理往上跳的时候,一定要先让u,v往上跳到并查集的祖先,不然会wa掉) 代码如下: #include <iostream> #include <algorithm&g ...

  2. [bzoj] 1257 余数之和sum || 数论

    原题 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + - + k mod n的值,其中k mod i表示k除以i的余数. \(\sum^n_{i=1} ...

  3. 门户系统整合sso cookie共享及显示用户信息

    1.1 门户系统整合sso 在门户系统点击登录连接跳转到登录页面.登录成功后,跳转到门户系统的首页,在门户系统中需要从cookie中 把token取出来.所以必须在登录成功后把token写入cooki ...

  4. SCOI2005 互不侵犯 [状压dp]

    题目传送门 题目大意:有n*n个格子,你需要放置k个国王使得它们无法互相攻击,每个国王的攻击范围为上下左走,左上右上左下右下,共8个格子,求最多的方法数 看到题目,是不是一下子就想到了玉米田那道题,如 ...

  5. 用静态工厂的方法实例化bean

    //代码如下: package com.timo.domain; public class ClientService { //use static factory method create bea ...

  6. PHP正则匹配与替换的简单例子

    PHP正则匹配与替换的简单例子,含一个匹配获取加租字体例子和一个匹配替换超链接的例子. 1.查找匹配 <b> 与 </b> 标签的内容: <?php $str = &qu ...

  7. The base command for the Docker CLI.

    Description The base command for the Docker CLI. Child commands Command Description docker attach At ...

  8. idea 将工程项目打包成war

    1.File--Project structure ---- Artifacts ----- + -----web Application :Archive ---for ' **:war explo ...

  9. 【Foreign】石子游戏 [博弈论]

    石子游戏 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output 输出T行,表示每组的答案. Sample Input 3 ...

  10. 9张图让你明白什么叫做"一坨屎"一样的iOS垃圾代码

    前言:这是一个两万余行的商业项目,但代码质量却不敢恭维!     //本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3489939.html  ...