#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. 【POJ 1201 Intervals】

    Time Limit: 2000MSMeamory Limit: 65536K Total Submissions: 27949Accepted: 10764 Description You are ...

  2. Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) B

    B. Little Artem and Grasshopper time limit per test 2 seconds memory limit per test 256 megabytes in ...

  3. mysql__视图

    视图 1.什么是视图 视图是一种虚拟存在的表,对于使用视图的用户来说基本上是透明的.视图并不是在数据库中实际存在的,行和列数据来自定义视图的查询中使用的表,并且是在使用视图时动态生成的 视图相对于普通 ...

  4. Eclipse开发环境配置,打磨Eclipse,安装插件(适用3.4,3.5,3.6,3.7)

    转载自:http://elf8848.iteye.com/blog/354035 打磨Eclipse -- 磨刀不误砍柴工 -------------------------------------- ...

  5. windows 系统下git 的使用

    前言: 最新版本的git for windows也是有界面的,不再是以前的纯命令行操作,但是我习惯了乌龟,所以感觉还是直接用乌龟比较方便点~~ 前提,已安装以下: git for windows,未安 ...

  6. 帮助小伙伴写的组装xml字符串类

    import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.uti ...

  7. org.apache.http.conn.HttpHostConnectException: Connection to xxx refused.

    if you are using emulator to run your app for local server. mention the local ip as 10.0.2.2 and hav ...

  8. 【BZOJ2663】灵魂宝石 [二分]

    灵魂宝石 Time Limit: 5 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description “作为你们本体的灵魂,为了能够更好的 ...

  9. bzoj 1706: [usaco2007 Nov]relays 奶牛接力跑——倍增floyd

    Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项目.至于进行接力跑的地点 自然是在牧场中现有的T(2 <= T < ...

  10. 洛谷 PT2 First Step (ファーストステップ)

    题目背景 知らないことばかりなにもかもが(どうしたらいいの?) 一切的一切 尽是充满了未知数(该如何是好) それでも期待で足が軽いよ(ジャンプだ!) 但我仍因满怀期待而步伐轻盈(起跳吧!) 温度差なん ...