[题目链接]

https://codeforces.com/contest/877/problem/E

[算法]

首先求出这棵树的DFS序

一棵子树的DFS序为连续的一段 , 根据这个性质 , 用线段树维护区间中1的个数即可

时间复杂度 : O(NlogN)

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + ;
typedef long long ll;
typedef long double ld; struct edge
{
int to , nxt;
} e[MAXN << ]; int n , q , timer , tot;
int val[MAXN] , l[MAXN] , r[MAXN] , dfn[MAXN] , size[MAXN] , head[MAXN] , loc[MAXN] , par[MAXN]; struct Segment_Tree
{
struct Node
{
int l , r , cnt;
bool tag;
} a[MAXN << ];
inline void update(int index)
{
a[index].cnt = a[index << ].cnt + a[index << | ].cnt;
}
inline void build(int index , int l , int r)
{
a[index].l = l , a[index].r = r;
a[index].tag = false;
if (l == r)
{
a[index].cnt = (val[loc[l]] == );
return;
}
int mid = (l + r) >> ;
build(index << , l , mid);
build(index << | , mid + , r);
update(index);
}
inline void pushdown(int index)
{
int l = a[index].l , r = a[index].r;
int mid = (l + r) >> ;
if (a[index].tag)
{
a[index << ].cnt = (mid - l + ) - a[index << ].cnt;
a[index << | ].cnt = (r - mid) - a[index << | ].cnt;
a[index << ].tag ^= ;
a[index << | ].tag ^= ;
a[index].tag = false;
}
}
inline void modify(int index , int l , int r)
{
if (a[index].l == l && a[index].r == r)
{
a[index].cnt = (r - l + ) - a[index].cnt;
a[index].tag ^= ;
return;
}
pushdown(index);
int mid = (a[index].l + a[index].r) >> ;
if (mid >= r) modify(index << , l , r);
else if (mid + <= l) modify(index << | , l , r);
else
{
modify(index << , l , mid);
modify(index << | , mid + , r);
}
update(index);
}
inline int query(int index , int l , int r)
{
if (a[index].l == l && a[index].r == r)
return a[index].cnt;
pushdown(index);
int mid = (a[index].l + a[index].r) >> ;
if (mid >= r) return query(index << , l , r);
else if (mid + <= l) return query(index << | , l , r);
else return query(index << , l , mid) + query(index << | , mid + , r);
}
} SGT; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u , int v)
{
++tot;
e[tot] = (edge){v , head[u]};
head[u] = tot;
}
inline void dfs(int u)
{
l[u] = dfn[u] = ++timer;
loc[timer] = u;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == par[u]) continue;
dfs(v);
}
r[u] = timer;
} int main()
{ scanf("%d" , &n);
for (int i = ; i <= n; i++)
{
scanf("%d" , &par[i]);
addedge(par[i] , i);
}
for (int i = ; i <= n; i++) scanf("%d" , &val[i]);
dfs();
SGT.build( , , n);
scanf("%d" , &q);
while (q--)
{
char type[];
scanf("%s" , type);
if (type[] == 'p')
{
int u;
scanf("%d" , &u);
SGT.modify( , l[u] , r[u]);
} else
{
int u;
scanf("%d" , &u);
printf("%d\n" , SGT.query( , l[u] , r[u]));
}
} return ; }

[Codeforces 877E] Danil and a Part-time Job的更多相关文章

  1. Codeforces 877E - Danil and a Part-time Job(dfs序+线段树)

    877E - Danil and a Part-time Job 思路:dfs序+线段树 dfs序:http://blog.csdn.net/qq_24489717/article/details/5 ...

  2. CodeForces 877E Danil and a Part-time Job(dfs序+线段树)

    Danil decided to earn some money, so he had found a part-time job. The interview have went well, so ...

  3. Codeforces 877E Danil and a Part-time Job(dfs序 + 线段树)

    题目链接   Danil and a Part-time Job 题意    给出一系列询问或者修改操作 $pow$ $x$表示把以$x$为根的子树的所有结点的状态取反($0$变$1$,$1$变$0$ ...

  4. Codeforces 877E - Danil and a Part-time Job 线段树+dfs序

    给一个有根树,1e5个节点,每个节点有权值0/.1,1e5操作:1.将一个点的子树上所有点权值取反2.查询一个点的子树的权值和   题解: 先深搜整颗树,用dfs序建立每个点对应的区间,等于把树拍扁成 ...

  5. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  6. codeforces 877e

    E. Danil and a Part-time Job time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. Codeforces Round #877 (Div. 2) E. Danil and a Part-time Job

    E. Danil and a Part-time Job 题目链接:http://codeforces.com/contest/877/problem/E time limit per test2 s ...

  8. Codeforces Round #442 (Div. 2) Danil and a Part-time Job

    http://codeforces.com/contest/877/problem/E 真的菜的不行,自己敲一个模板,到处都是问题.哎 #include <bits/stdc++.h> u ...

  9. codeforces 877 E. Danil and a Part-time Job(线段树(dfs序))

    题目链接:http://codeforces.com/contest/877/problem/E 题解:显然一看就感觉要么树链剖分要么线段树+dfs序,题目要求的操作显然用线段树+dfs序就可以实现. ...

随机推荐

  1. Pixhawk之姿态解算篇(1)_入门篇(DCM Nomalize)

    一.开篇 慢慢的.慢慢的.慢慢的就快要到飞控的主要部分了,飞控飞控就是所谓的飞行控制呗,一个是姿态解算一个是姿态控制,解算是解算,控制是控制,各自负责各自的任务.我也不懂.还在学习中~~~~ 近期看姿 ...

  2. VC 读取服务器上的文件(HTTP方式) [转]

    CString GetStringFromUrl(LPCTSTR pszUrl){    CString str ;    HINTERNET hSession = ::InternetOpen( _ ...

  3. Majority Number

    题目描写叙述 链接地址 解法 算法解释 题目描写叙述 Given an array of integers, the majority number is the number that occurs ...

  4. Solidworks安装完成提示failed to load slderresu.dll怎么办

    安装完成出现下面的一系列错误提示   进入到语言包,重新安装中文语言包即可   可以正常打开和运行了                  

  5. HDMI速率计算

    我们在采集HDMI口的数据时,首先肯定要计算它的速率是多少.怎么计算这个速率,本文要跟大家分享的便是这个事情. HDMI口有三个TM-DS(Time Minimized Differential Si ...

  6. 笔记03 MVVM 开发的几种模式(WPF)

    转自http://www.cnblogs.com/buptzym/p/3220910.html 在WPF系(包括SL,WP或者Win8)应用开发中,MVVM是个老生常谈的问题.初学者可能不会有感觉,但 ...

  7. 看完此文,妈妈还会担心你docker入不了门?

    本文在个人技术博客不同步发布,详情可猛戳 亦可扫描屏幕右侧二维码关注个人公众号,公众号内有个人联系方式,等你来撩...   上周对象突然心血来潮说想养个小宠物,我问想养啥她又说随便,你看着办!!!这我 ...

  8. HSSFWorkbook

    public ActionResult excelPrint() { HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件 HSSFShe ...

  9. iOS开发中的单元测试(三)——URLManager中的测试用例解析

    本文转载至 http://www.cocoachina.com/cms/plus/view.php?aid=8088   此前,我们在<iOS开发中的单元测试(一)&(二)>中介绍 ...

  10. 10个必需的iOS开发工具和资源

    本文转载至 http://mobile.51cto.com/iphone-418166.htm 界面总不是一件很容易事,尤其是iPhone/iPad的界面,做过iOS开发的程序员,一定会感到开发iPh ...