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. std::string::insert函数

    string& insert (size_t pos, const string& str); string& insert (size_t pos, const string ...

  2. 新版本Xcode 6的视图调试详解

    开发者会经常遇到视图或者Auto Layout约束中存在bug的情况,并且这种bug很难通过代码发现,所以开发者很有必要熟知如何进行简单高效的视图调试,而Xcode 6的发布使得视图调试变得前所未有的 ...

  3. AJ学IOS(45)之常用的小功能比如打电话、打开网址、发邮件、发短信打开其他应用。

    AJ分享,必须精品 简介 iOS中的很多小功能都是非常简单的,几行代码就搞定了,比如打电话.打开网址.发邮件.发短信.打开其他应用等. 打电话 方法1 最简单最直接的方式:直接跳到拨号界面 NSURL ...

  4. Nikto使用方法

    Introduction Nikto是一款开源的(GPL)网站服务器扫描器,使用Perl基于LibWhisker开发.它可以对网站服务器进行全面的多种扫描,包括6400多个潜在危险的文件/CGI,检查 ...

  5. XFS文件系统的备份与恢复

    永久修改主机名:hostnamectl set-hostname oldboy临时修改主机名:hostname xfsdump备份xfsdump -f 备份的文件位置 要备份的分区或者磁盘 免交互备份 ...

  6. 【山外问道】Linux UUID的查询方法

    本文打印版下载地址 [山外问道]Linux_UUID的查询方法-打印版.pdf 一.查询存储设备的UUID 1.使用blkid命令查看 (1)查询所有存储设备的UUID:blkid. (2)查询指定设 ...

  7. ATmega328P定时器详解

    写这篇文章,纯粹是想为博客拉点点击量.在博客园,游客访问好像是不计入阅读量的,而作为一个十八线博主,注册用户的访问应该以搜索引擎为主,博客园首页为次,个位数的粉丝就别谈了. 所以,希望各位从搜索引擎点 ...

  8. vscode 使用记录

    快捷键 Cmd+P 查找最近的文件 Ctrl+cmd + P 打开命令面板 Ctrl+tab文件间切换 Ctrl+` 打开终端 Cmd +b 隐藏侧边栏 VScode对多行编辑有两种模式 第一种模式 ...

  9. pytorch Model Linear实现线性回归CUDA版本

    实验代码 import torch import torch.nn as nn #y = wx + b class MyModel(nn.Module): def __init__(self): su ...

  10. python学习笔记(六)---文件操作与异常处理机制

    文件读取 读取整个文件 要读取文件,需要一个包含几行文本的文件.下面首先来创建一个文件,它包含精确到小数点后30位的圆周率值,且在小数点后每10位处都换行: pi_digits.txt 3.14159 ...