题目链接:

大概是:修改点值,求子树节点为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. POI读word doc 03 文件的两种方法

    Apache poi的hwpf模块是专门用来对word doc文件进行读写操作的.在hwpf里面我们使用HWPFDocument来表示一个word doc文档.在HWPFDocument里面有这么几个 ...

  2. ios之UIToolBar

    toolbar除了可以和navigationController一起用之外,也可以独立用到view里.工具栏UIToolbar – 一般显示在底部,用于提供一组选项,让用户执行一些功能,而并非用于在完 ...

  3. 洛谷 P2921 在农场万圣节

    https://www.luogu.org/problemnew/show/P2921 开始感觉这题30行代码就可以搞定,还是太菜啦,还是乖乖地写了tarjan. 对图进行缩点,那么这个强联通分量中的 ...

  4. 【java】 field 和 variable 区别及相关术语解释

    Having said that, the remainder of this tutorial uses the following general guidelines when discussi ...

  5. python的web框架---Django项目

    Django项目之会议室预预订: 界面效果展示: 1.创建超级管理员,实现预定界面功能 2.预定界面: (一)基于pymysql设计数据表结构,理清前后端与用户交互逻辑.(用户表,会议室表,预定内容存 ...

  6. Python文件指针与Python函数

    文件内指针移动:f.seek() 强调:只有t模式下read(n),n代表字符的个数,除此以外都是以字节为单位. """ 文件内容:哈哈哈哈 呵呵呵呵 "&qu ...

  7. 水题:UVa253-Cube painting

    Cube painting We have a machine for painting cubes. It is supplied with three different colors: blue ...

  8. Linux下安装Oracle客户端

    1.创建用户名和组名 /usr/sbin/groupadd oinstall /usr/sbin/groupadd dba /usr/sbin/useradd -m -g oinstall -G db ...

  9. xfce-OpenVAS自动化安全风险评估指南

    1.  登录系统 在客户端,打开浏览器,在地址栏输入https://IP:9392/ 看到如下界面: 我们要信任此网站,点击继续浏览此网站,进入系统登录界面,如下图: 输入我提供的通用登录账号:wdl ...

  10. Linux基础命令详解-2

    本篇详解的命令有以下30个 1.rmdir 功能:删除一个空目录   2.telnet 功能:使用telnet协议连接到主机的指定端口 3.vim 功能:编辑器之神 vim三种模式的功能及其转换   ...