题目链接:

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

DFS序后,BIT 询问,修改

  1.  1 #include<bits/stdc++.h>
  2.  2 
  3.  3 using namespace std;
  4.  4 typedef long long ll;
  5.  5 
  6.  6 #define N 223456
  7.  7 ll val[N];
  8.  8 int l[N],r[N];
  9.  9 int t=;
  10.  int f[N];
  11.  int lowbit(int x)
  12.  {
  13.      return x&-x;
  14.  }
  15.  void update(int x,int v)
  16.  {
  17.      while (x<N)
  18.      {
  19.          f[x]+=v;
  20.          x+=lowbit(x);
  21.      }
  22.  }
  23.  ll query(int x)
  24.  {
  25.      ll s=;
  26.      while (x)
  27.      {
  28.          s+=f[x];
  29.          x-=lowbit(x);
  30.      }
  31.      return s;
  32.  }
  33.  vector<int>mp[N];
  34.  void dfs(int u,int pre)
  35.  {
  36.      l[u]=t++;
  37.      for (int i=;i<mp[u].size();i++)
  38.      {
  39.          int v=mp[u][i];
  40.          if (v==pre) continue;
  41.          dfs(v,u);
  42.      }
  43.      r[u]=t-;
  44.  }
  45.  
  46.  
  47.  int main()
  48.  {
  49.      int n,Q;
  50.      scanf("%d%d",&n,&Q);
  51.      for (int i=;i<n;i++)
  52.      {
  53.          int x,y;
  54.          scanf("%d%d",&x,&y);
  55.          mp[x].push_back(y);
  56.          mp[y].push_back(x);
  57.      }
  58.      dfs(,);
  59.      for (int i=;i<=n;i++)
  60.      {
  61.          scanf("%d",&val[i]);
  62.          if (val[i]==) update(l[i],);
  63.      }
  64.      while (Q--)
  65.      {
  66.          char s[];
  67.          scanf("%s",s);
  68.          if (s[]=='U')
  69.          {
  70.              int u,v;
  71.              scanf("%d%d",&u,&v);
  72.              if (val[u]==) update(l[u],-);
  73.              val[u]+=v;
  74.              if (val[u]==) update(l[u],);
  75.          }else
  76.          {
  77.              int x;
  78.              scanf("%d",&x);
  79.              int ans=query(r[x])-query(l[x]-);
  80.              printf("%d\n",ans);
  81.          }
  82.      }
  83.      return ;
  84.  }

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. 初识 Hibernate

    Hibernate 框架 1.1   什么是框架? 框架是一个提供了可重用的公共结构半成品. 2.1   关于Hibernate Hibernate是数据持久层的一个轻量级框架.数据持久层的框架有很多 ...

  2. Dubbo服务的搭建

    dubbo框架主要作用是基于RPC的远程调用服务管理,但是注册中心是用的zookeeper,搭建dubbo,首先要安装zookeeper,配置zookeeper... 实现功能如图所示:(存在2个系统 ...

  3. UINavgationController

    UINavigationBar和UINavigationItem是iOS开发中常用的控件.   1.设置导航栏标题 self.title = @"iOS开发:iOSDevTip"; ...

  4. docker系列之安装配置

    由于docker支持内核3.8以上的,所以我们要安装centos7系统,这个系统的内核是3.10,来支持docker使用环境 一.用UItralSo制作u盘启动 打开找到要做u盘启动的镜像 二.安装C ...

  5. 一次下载多个文件的解决思路-JS

    一次下载多个文件的解决思路(iframe) - Eric 真实经历 最近开发项目需要做文件下载,想想挺简单的,之前也做过,后台提供下载接口,前端使用window.location.href就行了呗.不 ...

  6. Template--模板

    模板引擎的支持 配置 模板引擎配置为TEMPLATES设置.这是一个配置列表,每个引擎一个,默认值为空.这是settings.py生成的,通过startproject命令定义了一个更有用的值: TEM ...

  7. MIP求解方法总结

    *本文主要记录和分享学习到的知识,算不上原创 *参考文献见链接 本文主要简述了求解MIP问题的两大类(精确求解和近似求解),或者更细致地,三大类方法(精确算法,ε-近似算法和启发式算法).由于暂时不太 ...

  8. 推荐SQL Server Management Studio以及Visual Studio下的免费的插件 ApexSQL Complete

    SQL Server 并没有代码格式化的工具,对于处理他人编写的长SQL需要手工的格式化是一件麻烦的事情. 推荐SQL Server Management Studio以及Visual Studio下 ...

  9. Mac 生成public_key

    1.首先查看是否已经生成过public_key 打开终端查看是否已经存在SSH密钥:cd ~/.ssh 输出:No such file or directory表示没有 2.生成public_key ...

  10. hdu 4251 The Famous ICPC Team Again划分树入门题

    The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...