CF#877 E. Danil and a Part-time Job(DFS序+线段树)

c语言串口编程详解 串口: 一种异步通信接口,通常用于低速数据传输。RS-232: 最常见的串口标准,定义了电气特性、连接器和信号线。UART: 通用异步收发传输器,是实现串口通信的硬件模块。通过以上内容,你应能够理解并实现C语言在Windows和Linux平台上的串口编程。希望这本手册能帮助你在实际项目中顺利进行串口通信开发。如果有任何疑问或需要进一步的帮助,请随时提问。 阅读详情
E. Danil and a Part-time Job
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Danil works in a rooted tree (undirected connected acyclic graph) with n vertices, vertex 1 is the root of the tree. There is a room in each vertex, light can be switched on or off in each room. Danil's duties include switching light in all rooms of the subtree of the vertex. It means that if light is switched on in some room of the subtree, he should switch it off. Otherwise, he should switch it on.

Unfortunately (or fortunately), Danil is very lazy. He knows that his boss is not going to personally check the work. Instead, he will send Danil tasks using Workforces personal messages.

There are two types of tasks:

  1. pow v describes a task to switch lights in the subtree of vertex v.
  2. get v describes a task to count the number of rooms in the subtree of v, in which the light is turned on. Danil should send the answer to his boss using Workforces messages.

A subtree of vertex v is a set of vertices for which the shortest path from them to the root passes through v. In particular, the vertex v is in the subtree of v.

Danil is not going to perform his duties. He asks you to write a program, which answers the boss instead of him.

Input

The first line contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree.

The second line contains n - 1 space-separated integers p2, p3, ..., pn (1 ≤ pi < i), where pi is the ancestor of vertex i.

The third line contains n space-separated integers t1, t2, ..., tn (0 ≤ ti ≤ 1), where ti is 1, if the light is turned on in vertex i and 0 otherwise.

The fourth line contains a single integer q (1 ≤ q ≤ 200 000) — the number of tasks.

The next q lines are get v or pow v (1 ≤ v ≤ n) — the tasks described above.

Output

For each task get v print the number of rooms in the subtree of v, in which the light is turned on.

Example
input
4
1 1 1
1 0 0 1
9
get 1
get 2
get 3
get 4
pow 1
get 1
get 2
get 3
get 4
output
2
0
0
1
2
1
1
0
Note
The tree before the task pow 1.

The tree after the task pow 1.

题意:给出一棵树,其节点为0或1,有两种操作

1:将一棵子树上所有节点的值反转

2:求一颗子树上1的个数

dfs序裸题

#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10;
int n, m;
int tot, tot1;
int head[N<<1], L[N], R[N];
struct xx{
    int to, nex;
} a[N<<1];
struct xxx{
    int l, r, v, tag;
} T[N<<2];

void Init(){
    tot = tot1 = 0;
    memset(head, -1, sizeof head);
}

void Add(int u, int v){
    a[++tot].to = v, a[tot].nex = head[u], head[u] = tot;
}

void Dfs(int k, int f){
    tot1++;
    L[k] = tot1;
    for(int i = head[k]; i != -1; i = a[i].nex){
        if(a[i].to == f) continue;
        Dfs(a[i].to, k);
    }
    R[k] = tot1;
}

void Pushdown(int k){
    if(!T[k].tag) return;
    T[k<<1].tag ^= 1;
    T[k<<1|1].tag ^= 1;
    T[k<<1].v = T[k<<1].r-T[k<<1].l+1-T[k<<1].v;
    T[k<<1|1].v = T[k<<1|1].r-T[k<<1|1].l+1-T[k<<1|1].v;
    T[k].tag = 0;
}

void Pushup(int k){
    T[k].v = T[k<<1].v+T[k<<1|1].v;
}

void Build(int l, int r, int k){
    T[k].l = l, T[k].r = r;
    T[k].v = T[k].tag = 0;
    if(l == r) return;
    int mid = (l+r)>>1;
    Build(l, mid, k<<1);
    Build(mid+1, r, k<<1|1);
    Pushup(k);
}

void Update(int l, int r, int k){
    if(l <= T[k].l && r >= T[k].r){
        T[k].tag ^= 1;
        T[k].v = T[k].r-T[k].l+1-T[k].v;
        return;
    }
    Pushdown(k);
    int mid = (T[k].l+T[k].r)>>1;
    if(r <= mid) Update(l, r, k<<1);
    else if(l > mid) Update(l, r, k<<1|1);
    else{
        Update(l, mid, k<<1);
        Update(mid+1, r, k<<1|1);
    }
    Pushup(k);
}

int ans;
void Query(int l, int r, int k){
    if(l <= T[k].l && r >= T[k].r){
        ans += T[k].v;
        return;
    }
    if(T[k].l == T[k].r) return;
    Pushdown(k);
    int mid = (T[k].l+T[k].r)>>1;
    if(r <= mid) Query(l, r, k<<1);
    else if(l > mid) Query(l, r, k<<1|1);
    else{
        Query(l, mid, k<<1);
        Query(mid+1, r, k<<1|1);
    }
}

int main(){
    while(scanf("%d", &n) == 1){
        Init();
        int x;
        char op[5];
        for(int i = 2; i <= n; i++){
            scanf("%d", &x);
            Add(x, i); Add(i, x);
        }
        Dfs(1, 0);
        Build(1, n, 1);
        printf("\n");
        for(int i = 1; i <= n; i++){
            scanf("%d", &x);
            if(x) Update(L[i], L[i], 1);
        }
        scanf("%d", &m);
        while(m--){
            scanf("%s%d", op, &x);
            if(op[0] == 'g'){
                ans = 0;
                Query(L[x], R[x], 1);
                printf("%d\n", ans);
            }
            else Update(L[x], R[x], 1);
        }
    }
}


面向 OData V4 的 Fiori elements 为何强制使用 draft、它到底是什么、以及是否能关闭 本文深入解析了SAP Fiori elements中draft handling的核心机制与必要性。关键点包括:1)draft机制通过中间版本实现复杂编辑的安全恢复与并发控制;2)Fiori elements for OData V4强制要求draft-enabled服务支持事务编辑;3)非draft服务仅适用于只读场景;4)替代方案需承担完全自定义开发的成本。文章基于SAP官方文档,系统阐述了draft在事务型应用中的基础性作用,为架构决策提供了明确指导。 阅读详情

相关推荐

ESP32S3学习——i2c,点亮0.96寸oled

ESP32S3学习——i2c,点亮0.96寸oled

HandBAI的博客 1万+

CodeForces 877E DFS序+线段树

CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身一共有几个灯是亮的。pow x,表示你需要改变x的子树和x本身上的灯的状态。 题解思路 这个题肯定是用DFS序了,为啥?因为树不好操作啊(我也不会啊),使用DFS序可以把树压成一维的一串...

weixin_30715523的博客 194

工程师必看:如何用MINITAB快速计算传感器寿命(附详细步骤)

本文为工程师提供了使用MINITAB软件快速计算传感器寿命的详细实战指南。文章重点阐述了如何通过数据预处理、回归分析拟合阿伦尼斯模型,并最终预测平均寿命与置信区间,帮助工程师高效处理加速寿命试验数据,提升可靠性分析工作的准确性与效率。

weixin_29296241的博客 248

Danil and a Part-time Job

Danil and a Part-time Job time limit per test2 seconds memory limit per test256 megabytes Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he is a light switcher. Danil works in a rooted tree (undirect

Amber的博客 681

NVIDIA DGX Spark实战指南:从开箱到部署200B参数大模型

本文详细介绍了NVIDIA DGX Spark桌面AI超级计算机的开箱体验与实战部署指南。文章深入解析了其Grace Blackwell架构与128GB统一内存的核心优势,并逐步演示了从系统初始化、运行200B参数大模型到进行QLoRA微调的全过程。该设备将高达1 PetaFLOP的AI算力浓缩于桌面,为开发者和研究团队提供了本地化、高性能的AI开发与部署一体化解决方案。

weixin_29274969的博客 2228

从Reset到Read:深入解析Rolling Shutter曝光流水线

本文深入解析Rolling Shutter(卷帘曝光)的工作原理,重点探讨Reset和Read信号在图像传感器中的关键作用。通过Reset-Read流水线机制,详细解释了曝光控制、时序调优及运动伪影补偿等核心技术,帮助读者理解卷帘曝光在高速摄影和动态场景中的应用。

weixin_42531925的博客 253

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 now he is a light switcher. Danil works in a rooted tree (undirected connected acyclic graph) with ...

满月照亮的路 347

ROS TF坐标转换实战:从零搭建机器人坐标系(附常见问题排查)

本文提供了一份ROS TF坐标转换的实战指南,详细讲解了如何从零为机器人搭建完整的坐标系,并深入解析了TF的原理。内容涵盖静态与动态坐标变换的发布、URDF模型使用、以及Rviz和rqt_tf_tree等可视化调试工具。文章还附带了常见问题排查表,帮助开发者快速定位并解决TF树断裂、数据错位等典型问题。

weixin_29268385的博客 203

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

E. Danil and a Part-time Jobtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputDanil decided to earn some money, so he had found a part-time job. The...

gyr 368

Dynamics 365 F&O 开发入门(一)-- 从零开始建表

从零开始一步一步教你在Dynamics 365 F&O中创建一个表

qq_48843110的博客 941

实战篇| app爬虫不用真机

之前写过一篇基于真机app爬虫《App爬虫实战篇-以华为真机手机爬取集换社的app为例》,如果大家有用真机爬虫的经历,会深刻认识到真的不方便,一是自己的手机,经常会被电话、微信等打断,二是闲置手机,面对小批量数据还能勉强,大批量爬虫,会导致成本指数级上升,并且控制起来相对麻烦。以下就是常见的模拟器。emulator -avd my_avd -no-snapshot-save # 启动时不保存 snapshot。emulator -avd my_avd -wipe-data # 清除数据(恢复到出厂)

youbingchen的博客 596

CodeForces877E 线段树lazy

我的code超市了 E. Danil and a Part-time Job time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Danil decided

u011535421的博客 366

Android一个APP里面最少有几个线程

Android应用启动时,默认会创建一个进程,该进程中最少包含负责处理用户交互、UI更新等核心操作,所有与界面相关的逻辑必须在此线程执行。若在此线程执行耗时操作(如网络请求),会导致界面卡顿甚至触发ANR(应用无响应)。当对象重写了finalize()方法时,该线程负责将这些对象放入队列并调用其finalize()方法,完成资源释放后再由GC回收。监控的执行效率。如果finalize()方法执行超时(默认10秒),此线程会触发虚拟机退出以防止应用长时间阻塞。

编程进化论 613

Codeforces877 E. Danil and a Part-time Job(dfs序+线段树+lazy标记)

https://codeforces.com/contest/877/problem/E E. Danil and a Part-time Job time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Danil ...

yaoct的博客 345

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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

题目地址 题意:给你一棵树,1为根节点,每个节点都有应该状态0或者1,你有两种操作,pow的操作是把该节点以及他的所有子树的每个节点进行自身异或的操作,get就是查询该节点以及他的所有子树的每个节点有多少个1。 思路:我们先通过dfs一边把所有的子树搜索出来,并标记好,然后我们通过这个给出的子树的范围对线段树进行更新,之后的操作就是根据之前求出的子树范围来更新和查询,线段树就是一个简单的区间更新

不要錯過才珍惜 979

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

codeforces 877-E

题目链接:codeforces 877-E 题目大意:一棵树上,有些灯亮着,有些灯暗的,我们每次可以查询某个节点的亮灯个数,或者改变某个子树的暗亮情况(暗变成亮,亮变成暗)。 比较简单的dfs序,然后用线段树区间亮的个数即可。 AC代码: #pragma GCC optimize(2) #include<bits/stdc++.h> //#define int long long...

青烟绕指柔的博客 2676

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

【滚动训练】CodeForces877E Danil and a Part-time Job (DFS序+线段树)

题意 给一颗树,树上每个节点都有一个开关,每次操作对一个节点及其子树上节点的开关翻转,询问以某个节点为根的子树上开着的开关数目。 题解 dfs序将树形结构转换成线性结构,然后用线段树+lazy标记维护即可。 需要注意的就是编号问题,因为原树中的编号和线段树维护的区间编号不是一一对应的。 dfs序的pre编号是原树编号到线段树编号的一个hash。 所以在建树初始化的时候有一下两种方法...

Pengwill's Blog 317

Codeforces 877E Danil and a Part-time Job【Dfs序+线段树】

E. Danil and a Part-time Job time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Danil decided to earn some mon

mengxiang000000 774

Codeforces Round #442 (Div. 2)-E-Danil and a Part-time Job(DFS序+线段树区间更新)

E. Danil and a Part-time Job time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Danil decided to earn some mon

信仰.的博客 596

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 now he is a light switcher. Danil works in a rooted tree (undirected connected acyclic graph) with ...

满月照亮的路 247

codeforces 877E. Danil and a Part-time Job (DFS序列+线段树)

DFS序列+线段树

zuzhiang的博客 457

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

题目: Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he is a light switcher. Danil works in a rooted tree (undirected connected acyclic graph) ...

weixin_44554979的博客 298

CodeForces877E Danil and a Part-time Job 线段树+DFS序

题意:给你一棵树,每个结点都有一个灯,有两种操作:一是拨动某个结点对应子树的所有开关(若原来是开的就关上,若原来是关的就打开)。二是询问某个结点的状态(开还是关)。 思路:首先是操作某一结点与其对应子树,所以妥妥的dfs序。 关于dfs序(序嘛,肯定是序列的意思,那么肯定是一种线性结构),个人理解是把所有结点揪出来,然后把树形结构变成一个线性结构。然后对子树的操作就变成了对dfs序中某一区间的操作...

枚举星星 265

Danil and a Part-time Job--线段树dfs序

题目链接:https://cn.vjudge.net/problem/CodeForces-877E 题目大意 有一棵以1位根节点的树,每个节点都有一盏灯,或亮或灭,m次操作,get x,表示求x以及其子树中,所有亮着的灯的数量,pow x,表示把x以及其子树灯的状态改变,即1变为0,0变为1。 分析 通过这个题学会了dfs序。dfs序的思想就是,从根节点开始dfs,记下每个节点第一次...

黄油^小饼干的博客 320

CF877E Danil and a Part-time Job

CF877E Danil and a Part-time Job DFS序神题 首先你得了解DFS序是什么 有谁知道吗? 好,那就有请小Y同学来给我们讲讲DFS序及其运用吧! ERR,有意思,那就举个例子吧! 傲!所以…它有那些特别之处呐? 看来神犇被黑洞吸走了。请自行前往密室 寻找剩下的性质 我们的题解开始了。 由于是子树修改、查询,只要运用性质一即可 这里要结合线段树使用 push...

weixin_43872370的博客 376

codeforces 877E Danil and a Part-time Job

目录 codeforces 877E Danil and a Part-time Job 题意: 题解: Code: codeforces 877E Danil and a Part-time Job 题目传送门 题意: 给出一个树,每一个点都有权值,权值为...

dieyi9889的博客 233
上一篇: CF#877 B. Nikita and string(思维,暴力)
下一篇: HDU - 2586 How far away ?(LCA裸题)
信鸦
博客等级 码龄10年 10粉丝 133原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值