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::assign函数

    string& assign (const string& str); string& assign (const string& str, size_t subpos ...

  2. C语言小练习之学生信息管理系统

    C语言小练习之学生信息管理系统 main.c文件   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2 ...

  3. GeoGebra函数使用

    分段函数使用 输入指令: If(x < -2, x, -2 < x < 2, x², x > 2, x)

  4. stand up meeting 12-7

    weekend updates: 1.答题界面和结果界面的跳转和数据传输已全部完成. 2.答题界面完成简单的getRankingData API结果展示,答题时间,错误数目和错题题目的展示,点击题目可 ...

  5. .NET Core 初识

    什么是 ASP.NET Core? ASP.NET Core 是一个新的开源和跨平台的框架,用于构建如 Web 应用.物联网(IoT)应用和移动后端应用等连接到互联网的基于云的现代应用程序.ASP.N ...

  6. python 携程asyncio 实现高并发示例2

    https://www.bilibili.com/video/BV1g7411k7MD?from=search&seid=13649975876676293013 import asyncio ...

  7. keras API的使用,神经网络层,优化器,损失函数,查看模型层数,compile和fit训练

    layers介绍 Flatten和Dense介绍 优化器 损失函数 compile用法 第二个是onehot编码 模型训练 model.fit  两种创建模型的方法 from tensorflow.p ...

  8. php的一个有意思的命令:-S

    php -S localhost:8188 /web 会启动一个监控IP:PORT 的http服务,算是简易的web服务器吧.基本上,实现了PHP+MySQL就可以建立一个简易测试网站的环境.

  9. 非常简单的string驻留池,你对它真的了解吗

    昨天看群里在讨论C#中的string驻留池,炒的火热,几轮下来理论一堆堆,但是在证据提供上都比较尴尬.虽然这东西很基础,但比较好的回答也不是那么容易,这篇我就以我能力范围之内跟大家分享一下 一:无处不 ...

  10. sql注入 pikachu

    闭合条件的探测 数字型注入 burp抓包 分别测试 id=1 and 1=1和id=1 or 1=1 存在漏洞 字符型注入 ' 报错 探测闭合条件 ' order by 1# 未报错.构成闭合 同上有 ...