zhrt的数据结构课

这个题目我觉得是一个有一点点思维的dfs+线段树

虽然说看起来可以用树链剖分写,但是这个题目时间卡了树剖

因为之前用树剖一直在写这个,所以一直想的是区间更新,想dfs+线段树,有点点没想明白

后来才知道可以把这个区间更新转化成单点更新,就是查一个结点的子树,如果子树有可以到根节点的,那么这个结点肯定也可以到根节点。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <stack>
#include <map>
#include <string>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = 4e5 + 10;
int sum[maxn * 4]; void push_up(int id)
{
sum[id] = sum[id << 1 | 1] + sum[id << 1];
} void build(int id,int l,int r)
{
if(l==r)
{
sum[id] = 0;
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
push_up(id);
} void update(int id,int l,int r,int pos,int val)
{
if(l==r)
{
sum[id] += val;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid) update(id << 1, l, mid, pos, val);
else update(id << 1 | 1, mid + 1, r, pos, val);
push_up(id);
} int query(int id,int l,int r,int x,int y)
{
// printf("id=%d l=%d r=%d x=%d y=%d\n", id, l, r, x, y);
if (x <= l && y >= r) return sum[id];
int mid = (l + r) >> 1;
int ans = 0;
if (x <= mid) ans += query(id << 1, l, mid, x, y);
if (y > mid) ans += query(id << 1 | 1, mid + 1, r, x, y);
return ans;
}
int el[maxn], er[maxn], tot = 0, head[maxn], cnt;
struct node
{
int v, nxt;
node(int v=0,int nxt=0):v(v),nxt(nxt){}
}ex[maxn]; void init()
{
memset(head, -1, sizeof(head));
tot = 0, cnt = 0;
} void add(int u,int v)
{
ex[cnt] = node(v, head[u]);
head[u] = cnt++;
ex[cnt] = node(u, head[v]);
head[v] = cnt++;
// printf("u=%d v=%d\n", u, v);
} void dfs(int u,int pre)
{
el[u] = ++tot;
for(int i=head[u];i!=-1;i=ex[i].nxt)
{
int v = ex[i].v;
if (v == pre) continue;
dfs(v, u);
}
er[u] = tot;
// printf("el[%d]=%d er[%d]=%d\n", u, el[u], u, er[u]);
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
init();
int n, m;
scanf("%d%d", &n, &m);
build(1, 1, n);
for(int i=1;i<n;i++)
{
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
}
dfs(1, -1);
while(m--)
{
int opt, x;
scanf("%d%d", &opt, &x);
if (opt == 0) update(1, 1, n, el[x], 1);
if (opt == 1) update(1, 1, n, el[x], -1);
if (opt == 2)
{
int ans = query(1, 1, n, el[x], er[x]);
if (ans) printf("Yes\n");
else printf("No\n");
}
}
}
}

  

dfs+线段树 zhrt的数据结构课的更多相关文章

  1. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  2. Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线

    Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of ...

  3. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

  4. Educational Codeforces Round 6 E. New Year Tree dfs+线段树

    题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...

  5. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

  6. 线段树讲解(数据结构、C++)

    声明    : 仅一张图片转载于http://www.cnblogs.com/shuaiwhu/archive/2012/04/22/2464583.html,自己画太麻烦了...那个博客的讲解也很好 ...

  7. hdu 5692(dfs+线段树) Snacks

    题目http://acm.hdu.edu.cn/showproblem.php?pid=5692 题目说每个点至多经过一次,那么就是只能一条路线走到底的意思,看到这题的格式, 多个询问多个更新, 自然 ...

  8. HDU 3974 Assign the task (DFS+线段树)

    题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...

  9. hdu-2586 How far away ?(lca+bfs+dfs+线段树)

    题目链接: How far away ? Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Ot ...

随机推荐

  1. git获取特定的commit

    git reset --hard [commit_id]

  2. java 字符串截取 - 最后带上mysql字符串截取比较

    Java中的substring()方法有两个方法的重载,一个带一个参数的,一个带两个参数的. 第一种写法: substring(n);//从索引是n的字符开始截取,条件(n>=0,n<字符 ...

  3. 路径跟踪 PathMeasure的简单使用

    平时用path画一些简单的几何图形,呈现的时候也是已经绘制好的图形,想想,如果像动画一样看到它的绘制轨迹,是不是更酷?今天介绍的这个类PathMeasure就是干这个的,知道它的存在还是由于看了启舰写 ...

  4. ubuntu17.10安装lnmp安装包的核心问题-gcc版本、g++版本

    大致碰到的问题都是这样,不是php安装失败,就是MySQL安装失败,或者Nginx也安装失败 基本上是花式报错.后来在军哥的论坛中找到了这个帖子:https://bbs.vpser.net/viewt ...

  5. CSS中“~”(波浪号)、“,”(逗号)、“+”(加号)、“>”(大于号)、“ ”(空格)详解

    “~”:$('pre ~ brother')表示获取pre节点的后面的所有兄弟节点,相当于nextAll()方法: “+”:$('pre + nextbrother')表示获得pre节点的下一个兄弟节 ...

  6. (第七篇)系统编码、自启动配置、HOSTNAME、系统启动、定时任务、进程管理、硬盘及其分区

    linux查看系统编码和修改系统编码的方法 查看支持的字符编码 使用locale命令, 如: root@ubuntu:/etc# locale 然后修改/etc/locale.conf,如改成中文编码 ...

  7. 2019-2020-1 20199328《Linux内核原理与分析》第二周作业

    冯诺依曼体系结构的核心是: 冯诺依曼体系结构五大部分:控制器,运算器,存储器,输入输出设备. 常用的寄存器 AX.BX.CX.DX一般存放一些一般的数据,被称为通用寄存器,分别拥有高8位和低8位. 段 ...

  8. chcp437 转换英语,在西班牙语系统中无效

    https://social.technet.microsoft.com/Forums/en-US/9c772011-5094-4df0-bf73-7140bf91673b/chcp-command- ...

  9. 播放声音 (c++) (windows)

    自己看自己看自己看自己看自己看自己看 在<windows.h>中 一:BOOL WINAPI MessageBeep (_in UINT uType ); 播放一个波形文件 (也就是wac ...

  10. 从GC的SuppressFinalize方法带你深刻认识Finalize底层运行机制

    如果你经常看开源项目的源码,你会发现很多Dispose方法中都有这么一句代码: GC.SuppressFinalize(this); ,看过一两次可能无所谓,看多了就来了兴趣,这篇就跟大家聊一聊. 一 ...