题目链接:

大概是:修改点值,求子树节点为0有多少个,

DFS序后,BIT 询问,修改

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 typedef long long ll;
 5 
 6 #define N 223456
 7 ll val[N];
 8 int l[N],r[N];
 9 int t=;
 int f[N];
 int lowbit(int x)
 {
     return x&-x;
 }
 void update(int x,int v)
 {
     while (x<N)
     {
         f[x]+=v;
         x+=lowbit(x);
     }
 }
 ll query(int x)
 {
     ll s=;
     while (x)
     {
         s+=f[x];
         x-=lowbit(x);
     }
     return s;
 }
 vector<int>mp[N];
 void dfs(int u,int pre)
 {
     l[u]=t++;
     for (int i=;i<mp[u].size();i++)
     {
         int v=mp[u][i];
         if (v==pre) continue;
         dfs(v,u);
     }
     r[u]=t-;
 }
 
 
 int main()
 {
     int n,Q;
     scanf("%d%d",&n,&Q);
     for (int i=;i<n;i++)
     {
         int x,y;
         scanf("%d%d",&x,&y);
         mp[x].push_back(y);
         mp[y].push_back(x);
     }
     dfs(,);
     for (int i=;i<=n;i++)
     {
         scanf("%d",&val[i]);
         if (val[i]==) update(l[i],);
     }
     while (Q--)
     {
         char s[];
         scanf("%s",s);
         if (s[]=='U')
         {
             int u,v;
             scanf("%d%d",&u,&v);
             if (val[u]==) update(l[u],-);
             val[u]+=v;
             if (val[u]==) update(l[u],);
         }else
         {
             int x;
             scanf("%d",&x);
             int ans=query(r[x])-query(l[x]-);
             printf("%d\n",ans);
         }
     }
     return ;
 }

codechef Tree and Queries Solved的更多相关文章

  1. [Codeforces Round #221 (Div. 1)][D. Tree and Queries]

    题目链接:375D - Tree and Queries 题目大意:给你一个有n个点的树,每个点都有其对应的颜色,给出m次询问(v,k),问v的子树中有多少种颜色至少出现k次 题解:先对所有的询问进行 ...

  2. Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)

    题目链接  Tree and Queries 题目大意  给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...

  3. CodeForces 375D Tree and Queries 莫队||DFS序

    Tree and Queries 题意:有一颗以1号节点为根的树,每一个节点有一个自己的颜色,求出节点v的子数上颜色出现次数>=k的颜色种类. 题解:使用莫队处理这个问题,将树转变成DFS序区间 ...

  4. codeforces 375D:Tree and Queries

    Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  5. CF375D Tree and Queries

    题意翻译 给出一棵 n 个结点的树,每个结点有一个颜色 c i . 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种.树的根节点是1. 感谢@elijahqi 提供的翻译 ...

  6. CodeForces 376F Tree and Queries(假·树上莫队)

    You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...

  7. Codeforces 375 D Tree and Queries

    Discription You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  8. CodeForces - 375D Tree and Queries (莫队+dfs序+树状数组)

    You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...

  9. CodeChef DISTNUM2 Easy Queries 节点数组线段树

    Description You are given an array A consisting of N positive integers. You have to answer Q queries ...

随机推荐

  1. Spring框架bean的注解管理方法之一 使用注解生成对象

    首先在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression 下载地址: https://pan.baidu.com/s/1qXLH ...

  2. ECMAScript5 [].reduce()

    ECMAScript 5 的2个归并数组的方法,reduce() reduceRight() 两个方法都会迭代数组的所有项,然后构建一个最终返回的值. 两个参数:   1.函数,一个在每一项上调用的函 ...

  3. docker资源汇总

    https://github.com/hangyan/docker-resources/blob/master/README_zh.md   https://github.com/lightning- ...

  4. vue建项目并使用

    今天来回顾下vue项目的建立和使用,好久不用感觉不会用了. 下面两个都要全局安装 首先安装git,地址  https://gitforwindows.org/ 安装node, 地址 https://n ...

  5. ubuntu 安装 php7.2

    sudo apt-get install software-properties-common python-software-properties sudo add-apt-repository p ...

  6. 数据结构( Pyhon 语言描述 ) — —第9章:列表

    概念 列表是一个线性的集合,允许用户在任意位置插入.删除.访问和替换元素 使用列表 基于索引的操作 基本操作 数组与列表的区别 数组是一种具体的数据结构,拥有基于单个的物理内存块的一种特定的,不变的实 ...

  7. Python3与SQLServer、Oracle、MySql的连接方法

    环境: python3.4 64bit pycharm2018社区版 64bit Oracle 11 64bit SQLServer· Mysql 其中三种不同的数据库安装在不同的服务器上,通过局域网 ...

  8. I2C驱动框架(二)

    参考:I2C子系统之I2C bus初始化——I2C_init() 在linux内核启动的时候最先执行的和I2C子系统相关的函数应该是driver/i2c/i2c-core.c文件中的i2c_init( ...

  9. Bin Paking Problem:简单的构造性算法

    *本文主要记录和分享学习到的知识,算不上原创 *参考文献见链接 目录 BL和BLF算法 BF算法 HR算法 PH算法

  10. EFCore CodeFirst模型迁移生成数据库备注(mysql)

    重写Mysql下sql脚本生成器 using Framework.NetCore.Extensions; using Framework.NetCore.Models; using Microsoft ...