zoukankan      html  css  js  c++  java
  • Codeforces 384E-线段树+dfs序

    如果这题只传到儿子不继续向下就是裸的dfs序+线段树,继续往下传的还改变正负号,我们可以根据它的层数来确定正负号

      1 #include<bits/stdc++.h>
      2 
      3 #define inf 0x3f3f3f3f
      4 
      5 #define lson (id<<1)
      6 
      7 #define rson ((id<<1)|1)
      8 
      9 #define mid ((l+r)>>1)
     10 
     11 const int maxn=200000;
     12 
     13 using namespace std;
     14 
     15 int t;
     16 
     17 int p;
     18 
     19 int n,m;
     20 
     21 int u,v;
     22 
     23 int x,val;
     24 
     25 int dep[maxn+10];
     26 
     27 int a[maxn+10];
     28 
     29 int pos[maxn+10];
     30 
     31 int q[maxn+10];
     32 
     33 int son[maxn+10];
     34 
     35 int tree[maxn*4+10];
     36 
     37 int sum[maxn*4+10];
     38 
     39 int lazy[maxn*4+10];
     40 
     41 vector<int> G[maxn+10];
     42 
     43 void push_up(int id){
     44    tree[id]=tree[lson]+tree[rson];
     45 }
     46 
     47 void push_down(int id,int l,int r){
     48         if(lazy[id]){
     49                 lazy[lson]+=lazy[id];
     50                 lazy[rson]+=lazy[id];
     51                 tree[lson]+=(mid-l+1)*lazy[id];
     52                 tree[rson]+=(r-mid)*lazy[id];
     53                 lazy[id]=0;
     54         }
     55         return ;
     56 }
     57 
     58 void build(int id,int l,int r){
     59       if(l==r){
     60         sum[id]=a[pos[l]];
     61         return ;
     62       }
     63       build(lson,l,mid);
     64       build(rson,mid+1,r);
     65       push_up(id);
     66       return ;
     67 }
     68 
     69 void update(int id,int l,int r,int x,int y,int val){
     70         if(l==x&&r==y){
     71                 tree[id]+=val*(r-l+1);
     72                 lazy[id]+=val;
     73                 return ;
     74         }
     75         push_down(id,l,r);
     76         if(x>mid){
     77                 update(rson,mid+1,r,x,y,val);
     78         } else if(y<=mid){
     79                 update(lson,l,mid,x,y,val);
     80         } else {
     81                 update(lson,l,mid,x,mid,val);
     82                 update(rson,mid+1,r,mid+1,y,val);
     83         }
     84 }
     85 
     86 int fi(int id,int l,int r,int x){
     87         if(l==r){
     88                 int temp=dep[pos[l]]&1;
     89                 if(!temp) temp=-1;
     90                 return sum[id]+lazy[id]*temp;
     91         }
     92         push_down(id,l,r);
     93         if(x<=mid){
     94                 return fi(lson,l,mid,x);
     95         } else {
     96                 return fi(rson,mid+1,r,x);
     97         }
     98 }
     99 
    100 void dfs(int x,int fa,int d){
    101         dep[x]=d;
    102         pos[++p]=x;
    103         q[x]=p;
    104         for(size_t i=0;i<G[x].size();i++){
    105                 if(G[x][i]==fa) continue;
    106                 dfs(G[x][i],x,d+1);
    107                 son[x]++;
    108                 son[x]+=son[G[x][i]];
    109         }
    110 }
    111 
    112 int main()
    113 {
    114     scanf("%d%d",&n,&m);
    115     for(int i=1;i<=n;i++){
    116         scanf("%d",&a[i]);
    117     }
    118     for(int i=1;i<n;i++){
    119         scanf("%d%d",&u,&v);
    120         G[u].push_back(v);
    121         G[v].push_back(u);
    122     }
    123     dfs(1,0,1);
    124     build(1,1,n);
    125     for(int i=1;i<=m;i++){
    126         scanf("%d",&t);
    127         if(t==1){
    128         scanf("%d%d",&x,&val);
    129         if(dep[x]&1)
    130         update(1,1,n,q[x],q[x]+son[x],val);
    131         else update(1,1,n,q[x],q[x]+son[x],-val);
    132         } else {
    133         scanf("%d",&x);
    134         int ans=fi(1,1,n,q[x]);
    135         printf("%d
    ",ans);
    136         }
    137     }
    138     return 0;
    139 }
    View Code
  • 相关阅读:
    [bbk2908]第4集 Chapter 03 介绍RAC的体系结构
    [bbk3011]第8集 Chapter 05 介绍RAC安装过程概述
    [bbk3100]第7集 Chapter 04 介绍RAC中CVU工具的使用
    [bbk2907]第3集 Chapter 02 RAC的安装过程中需要注意的要点
    [bbk2905]第1集 Chapter 01 介绍RAC概述
    [bbk2906]第2集 Chapter 02 介绍RAC概述
    RAC之CRS架构简介
    NOIP普及组2017比赛总结
    struct和typedef
    KMP详解(转)
  • 原文地址:https://www.cnblogs.com/GeniusYang/p/6049990.html
Copyright © 2011-2022 走看看