Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 207|回复: 0

[默认分类] CodeForces 877E Danil and a Part-time Job(dfs序+线段树)

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-5-10 10:06:38 | 显示全部楼层 |阅读模式
    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:
    pow v describes a task to switch lights in the subtree of vertex v.
    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
    inputCopy
    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
    outputCopy
    2
    0
    0
    1
    2
    1
    1
    0
    Note

    The tree before the task pow 1.

      

    The tree after the task pow 1.
      
    题意:给出一棵树初始的节点值,给出两种操作,一种为子树异或1,另一种为统计子树中一的个数
      
    题解:  可以用dfs序+线段树解决,异或标记可以通过tag[x]^=1来进行传递,每次更改相当于将区间内所有的1改为0,所有的0改为1,这样相当于把sum改为size-sum,可以O(1)实现
      
    代码如下:

    1. [b]#include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm>
    2. #define lson root<<1
    3. #define rson root<<1|1
    4. using namespace std; struct node { int l,r,lazy,sum; } tr[800080]; vector<int> g[200020]; int id[200020],size[200020],c[200020],w[200020],tot; void push_up(int root) { tr[root].sum=tr[lson].sum+tr[rson].sum; } void push_down(int root) { int mid=(tr[root].l+tr[root].r)>>1; tr[lson].sum=(mid-tr[root].l+1)-tr[lson].sum; tr[lson].lazy=1^tr[lson].lazy; tr[rson].sum=(tr[root].r-mid)-tr[rson].sum; tr[rson].lazy=1^tr[rson].lazy; tr[root].lazy=0; } void build(int root,int l,int r) { if(l==r) { tr[root].l=l; tr[root].r=r; tr[root].sum=w[l]; return ; } tr[root].l=l; tr[root].r=r; int mid=(l+r)>>1; build(lson,l,mid); build(rson,mid+1,r); push_up(root); } void update(int root,int l,int r,int val) { if(tr[root].l==l&&tr[root].r==r) { tr[root].sum=(tr[root].r-tr[root].l+1)-tr[root].sum; tr[root].lazy=tr[root].lazy^1; return ; } if(tr[root].lazy) { push_down(root); } int mid=(tr[root].l+tr[root].r)>>1; if(l>mid) { update(rson,l,r,val); } else { if(mid>=r) { update(lson,l,r,val); } else { update(lson,l,mid,val); update(rson,mid+1,r,val); } } push_up(root); } int query(int root,int l,int r) { if(tr[root].l==l&&tr[root].r==r) { return tr[root].sum; } if(tr[root].lazy) { push_down(root); } int mid=(tr[root].l+tr[root].r)>>1; if(mid<l) { return query(rson,l,r); } else { if(mid>=r) { return query(lson,l,r); } else { return query(lson,l,mid)+query(rson,mid+1,r); } } } void dfs(int now,int f) { id[now]=++tot; w[tot]=c[now]; size[now]=1; for(int i=0; i<g[now].size(); i++) { if(g[now][i]==f) { continue; } dfs(g[now][i],now); size[now]+=size[g[now][i]]; } } void sub_update(int u,int val) { update(1,id[u],id[u]+size[u]-1,val); } int sub_query(int u) { return query(1,id[u],id[u]+size[u]-1); } int main() { int n,m; scanf("%d",&n); for(int i=2; i<=n; i++) { int to; scanf("%d",&to); g[to].push_back(i); g[i].push_back(to); } for(int i=1; i<=n; i++) { scanf("%d",&c[i]); } dfs(1,0); build(1,1,n); scanf("%d",&m); char s[10]; int val; for(int i=1; i<=m; i++) { scanf("\n%s %d",s,&val); if(s[0]=="g") { printf("%d\n",sub_query(val)); } else { sub_update(val,1); } } }[/b]
    复制代码


    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-30 07:55 , Processed in 0.412901 second(s), 37 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表