首页 > 其他分享 >Codeforces Round #442 (Div. 2)E. Danil and a Part-time Job 线段树+lazytag

Codeforces Round #442 (Div. 2)E. Danil and a Part-time Job 线段树+lazytag

时间:2023-02-14 12:56:01浏览次数:31  
标签:rt int 442 Codeforces dfs Job ls step build

题意:一颗有根树,树上每一个节点有一个灯,要支持两种操作

第一种操作是统计一颗子树内开着的灯个数。

第二种操作是将一个子树内的所有灯状态改变(开灯->关灯,关灯->开灯)。

解:

经典处理方法是先把树通过dfs序拍成区间,预处理出每个结点u管理的左右端点

然后变成区间改变01状态,求和问题

01状态用lazy维护一下

#include<bits/stdc++.h>
#define ls (rt<<1)
#define rs (rt<<1|1)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int maxn=2e5+5;
typedef long long ll;
int n,dfn[maxn],w[maxn],L[maxn],R[maxn];
void debug(int x){ cout<<": "<<x<<endl;}
struct seg{
    int l,r,len,sum,lazy;
    //
}t[maxn<<2];
void push_up(int rt){
    //
    t[rt].sum=t[ls].sum+t[rs].sum;
}
void push_down(int rt){
    //
    if(!t[rt].lazy) return;
    t[ls].lazy^=t[rt].lazy;
    t[ls].sum=t[ls].len-t[ls].sum;
    t[rs].lazy^=t[rt].lazy;
    t[rs].sum=t[rs].len-t[rs].sum;
    t[rt].lazy=0;// lazy==0 表示没有操作了 
}
void build(int rt,int l,int r){
    t[rt].l=l;t[rt].r=r;
    t[rt].len=r-l+1;
    t[rt].lazy==0;
    if(l==r){
       t[rt].sum=w[dfn[l]];
       return;
    }
    int mid=(l+r)>>1;
    build(ls,l,mid);build(rs,mid+1,r);
    push_up(rt);
}
void update(int rt,int l,int r){
    //cout<<rt<<" "<<l<<" "<<r<<endl;
    if(l<=t[rt].l&&t[rt].r<=r){
        // update
        t[rt].lazy^=1;
        t[rt].sum=t[rt].len-t[rt].sum;
        return;  
    }
    push_down(rt);
    if(t[ls].r>=l) update(ls,l,r);
    if(t[rs].l<=r) update(rs,l,r);
    push_up(rt);
}
int ask(int rt,int l,int r){
    //cout<<rt<<" "<<l<<" "<<r<<endl;
    if(l<=t[rt].l&&t[rt].r<=r){
        return t[rt].sum;//
    }
    push_down(rt);
    int ans=0;
    if(t[ls].r>=l) ans=(ans+ask(ls,l,r));//
    if(t[rs].l<=r) ans=(ans+ask(rs,l,r));//
    push_up(rt);
    return ans;
} 
int step;
vector<int>e[maxn];
void dfs(int u,int fa){
    ++step;
    dfn[step]=u;
    L[u]=step;
    for(int i=0;i<e[u].size();i++){
        int to=e[u][i];
        if(to==fa) continue;
        dfs(to,u);
    }
    R[u]=step;
    //cout<<u<<" "<<L[u]<<" "<<R[u]<<endl;
}
int main(){
    //freopen("lys.in","r",stdin);
    fastio;
    cin>>n;
    for(int i=2;i<=n;i++) {
        int x;cin>>x;
        e[x].push_back(i);
    }
    for(int i=1;i<=n;i++) cin>>w[i];
    dfs(1,0);
    build(1,1,n);
    int q;cin>>q;
    for(int i=1;i<=q;i++){
        string s;cin>>s;
        int v;cin>>v;
        //cout<<s<<" "<<v<<endl;
        if(s[0]=='p'){
            update(1,L[v],R[v]);
        }
        else {
            cout<<ask(1,L[v],R[v])<<endl;
        }
    }
}

一发过还是很爽的:)

标签:rt,int,442,Codeforces,dfs,Job,ls,step,build
From: https://www.cnblogs.com/liyishui2003/p/17119223.html

相关文章

  • 创建cronjob用户和命名空间
    创建命名空间#注意命名规则kubectlcreatenamespacecyj-scheduleyaml创建用户createaccount.yamlapiVersion:v1kind:ServiceAccountmetadata:name:s......
  • Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 c
    FSouvenirs将询问离线,对原数组离散化,然后用权值线段树维护区间对应权值最大值,\(a_i\)的权值为\(i\),再用树状数组维护区间两数绝对值差的最小值。复杂度为\(\mathcal{......
  • GitLab CICD Day 04 - 新增 Pipeline Job
    编写.gitlab-ci.ymlhelloworld:#Jobtags:-shell#Gitlab-runnerbefore_script:-echo"脚本执行前的任务"scrip......
  • Codeforces Round #852 (Div. 2)(C,D)
    CodeforcesRound#852(Div.2)(C,D)B这个题大意是给你一个\(x\),\(y\),\(x\)是极大值(\(a_i>a_{i+1},a_i>a_{i+1}\))的和,\(y\)是极小值(\(a_i<a_{i+1},a_i<a_{i+1}\))的......
  • Codeforces Round #852 (Div. 2)
    F.Rebrending题目抽象为现有长度为\(n\)的数组\(a_1,a_2,\cdots,a_n\),\(m\)次询问,每次询问\(\min\limits_{l\lei,j\ler,i\neqj}|a_i-a_j|\)\((1\lel<r\len)......
  • #0033. Educational Codeforces Round 3
    609A贪心优先选大的USBflashdrive609B先处理每个genre的书有多少本,然后直接枚举每次购买的是哪两种genre然后乘法原理即刻609C手下考虑balanced的长啥样。假设这n......
  • #0032. Educational Codeforces Round 2
    600A简单题但有个坑点在于会有空字符串600B一道可以用来实验upper_bound的题600C挺有趣的一道题。首先考虑怎样的字符串可以通过permutation变成palindrome:条件是至......
  • Codeforces Round #852 (Div. 2)
    A.YetAnotherPromotion题意:买土豆,一种卖a元一公斤,买m公斤送一公斤;一种卖b元一公斤。求买n公斤土豆最少花多少钱。解:完全没有思考,把a*n,b*n,买尽可能多m倍数的土豆剩......
  • Codeforces Round #851 (Div. 2)-F. XOR, Tree, and Queries-树、异或、并查集
    题目:https://codeforces.com/contest/1788/problem/F题解:(首先他和线性基没什么瓜系)想这个问题大概可以分成几个部分:1、很自然地考虑记\(p_x\)表示从根节点走到x路径......
  • 【Flink】详解JobGraph
    ​ 【Flink】详解JobGraph大家好,我们的gzh是朝阳三只大明白,满满全是干货,分享近期的学习知识以及个人总结(包括读研和IT),跪求一波关注,希望和大家一起努力、进步!!概述JobGraph......