#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. jsp电子商务购物车之四 数据库存储篇

    为了方便用户下次登录,仍然可以看到自己的购物车内容,所以,需要在数据库存储相应的购物车项目,本处增加购物车项表;uid和bid是复合主键. package com.cart.entity; //购物车 ...

  2. 【NOIP模拟赛】藏宝图 最小生成树

    性质:我们把最小生成树建出来,如果其距离符合那么就是对的,不符合就是错的 因为这是个n^2的图所以不能Kruskal只能Prim #include <cstdio> #include &l ...

  3. ubuntu下使用sudo 出现unable to resolve host 解决方法

    Linux 环境, 假设这台机器名字叫dev(机器的hostname), 每次执行sudo 就出现这个警告讯息:sudo: unable to resolve host dev虽然sudo 还是可以正 ...

  4. 第五届华中区程序设计邀请赛暨武汉大学第十四届校赛 网络预选赛 A

    Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB   Total Submit: 564  Accepted: ...

  5. Java Error: Failed to validate certificate. The application will not be executed

    Hi, last week a customer had the problem that he wants to connect to the administration interface of ...

  6. canvas知识01

    本文转自:http://www.cnblogs.com/jsdarkhorse/archive/2012/06/29/2568451.html 更多参考:http://www.cnblogs.com/ ...

  7. L2-001. 紧急救援---(Dijkstra,记录路径)

    https://www.patest.cn/contests/gplt/L2-001 L2-001. 紧急救援 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 ...

  8. loj6043 「雅礼集训 2017 Day7」蛐蛐国的修墙方案

    传送门:https://loj.ac/problem/6043 [题解] 我们考虑这是个置换,所以一定形成了很多不相交的环. 对于每个环,我们只能选一段.不选.选一段.不选这样交替下去. 显然只有偶环 ...

  9. HDU 1798 Tell me the area (数学)

    题目链接 Problem Description     There are two circles in the plane (shown in the below picture), there ...

  10. bzoj 1088 DP

    我们可以用w[i][s]来表示到第i位的方案,s代表第i位和第i+1位是否有雷的二进制串,那么我们就可以根据每一位的雷的数量转移了. /******************************** ...