题目链接:

大概是:修改点值,求子树节点为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. ios调试技巧

    一.概述1.掌握调试技巧,调试技术最基本,最重要的调试手段包括:单步跟踪,断点,变量观察等.单步跟踪(Step)所谓单步跟踪是指一行一行地执行程序,每执行一行语句后就停下来等待指示,这样你就能够仔细了 ...

  2. http post get 同步异步

    下面首先介绍一下一些基本的概念---同步请求,异步请求,GET请求,POST请求. 1.同步请求从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操作.也 ...

  3. Promise中的next 另一个用法

    const chainAsync = fns => { let curr = 0 ; const next = (...args) => fns[curr++](next,...args) ...

  4. vue父子传值的具体应用

    最近我负责的项目已经迭代到第四版了,我作为一个没啥经验的小菜鸟也成长了很多. 在这一版开发开始之前,我老大就要求我在开发过程中尽量实现组件化,因此,我也遇到了很多问题,但基本都解决了,所以趁周末把这些 ...

  5. substring substr slice 区别

    1. substring(start,end)  返回指定索引区间的字串,不改变原字符串 start 必需,开始位置的索引,一个非负的整数 end  可选,结束位置的索引(不包括其本身),如果未设置, ...

  6. UVA-1220-Party at Hali-Bula && UVA-1218-Perfect Service(树形DP)

    UVA-1220-Party at Hali-Bula 题意: 一个公司员工要举行聚会,要求任意一个人不能和他的直接上司同时到场,一个员工只有一个支系上司,现在求最多有多少人到场,并且方案是否唯一(紫 ...

  7. 【OS_Linux】Linux中虚拟机的三种上网方式——桥接、NAT、Host-only

    1.桥接 桥接方便做实验,配置ip方便.可以和局域网中的其他机器进行通信,也可以和公网进行通信.缺点是会占用主机所在局域网的一个ip. 2. NAT NAT模式下虚拟机可以和主机进行通信,可以上网,而 ...

  8. CSS3的-字体 @font-face

    http://www.w3cplus.com/content/css3-font-face http://www.w3cplus.com/css3/web-icon-with-font-face ht ...

  9. subprocess模块windows系统命令和linux系统命令

    windows系统 查看所有进程 tasklist 查找指定进程 tasklist | findstr pycharm 程序名称 PID(大写) 数量 大小 python exe 2640 conso ...

  10. (转) iOS程序国际化

    IOS程序国际化  本文转自http://www.cnblogs.com/zhidao-chen/archive/2012/07/08/2581977.html 1.1 新建一个Single View ...