[COJ0968]WZJ的数据结构(负三十二)
[COJ0968]WZJ的数据结构(负三十二)
试题描述
给你一棵N个点的无根树,边上均有权值,每个点上有一盏灯,初始均亮着。请你设计一个数据结构,回答M次操作。
1 x:将节点x上的灯拉一次,即亮变灭,灭变亮。
2 x k:询问当前所有亮灯的节点中距离x第k小的距离(注意如果x亮着也算入)。
输入
第二行到第N行每行三个正整数ui,vi,wi。表示一条树边从ui到vi,距离为wi。
第N+1行为一个正整数M。
最后M行每行三个或两个正整数,格式见题面。
输出
对于每个询问操作,输出答案。
输入示例
输出示例
数据规模及约定
1<=N,M<=50000
1<=x,ui,vi<=N,1<=v,wi<=1000
题解
动态点分治。对于每个节点我们开一个平衡树,每次修改节点 u 时把 u 以及它到根节点的路径上所有节点上的平衡树都更新一下;对于询问我们先二分答案 x,然后查找一下 u 到根节点路径上所有平衡树,看小于等于 x 的值是否小于 k 个。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 50010
#define maxm 100010
#define maxlog 17 int n, m, head[maxn], nxt[maxm], to[maxm], dist[maxm]; void AddEdge(int a, int b, int c) {
to[++m] = b; dist[m] = c; nxt[m] = head[a]; head[a] = m;
swap(a, b);
to[++m] = b; dist[m] = c; nxt[m] = head[a]; head[a] = m;
return ;
} int dep[maxn], mnd[maxlog][maxn<<1], Log[maxn<<1], clo, pos[maxn];
void build(int u, int pa) {
mnd[0][pos[u] = ++clo] = dep[u];
for(int e = head[u]; e; e = nxt[e]) if(to[e] != pa)
dep[to[e]] = dep[u] + dist[e], build(to[e], u), mnd[0][++clo] = dep[u];
return ;
}
void rmq_init() {
Log[1] = 0;
for(int i = 2; i <= clo; i++) Log[i] = Log[i>>1] + 1;
for(int j = 1; (1 << j) <= clo; j++)
for(int i = 1; i + (1 << j) - 1 <= clo; i++)
mnd[j][i] = min(mnd[j-1][i], mnd[j-1][i+(1<<j-1)]);
return ;
}
int cdist(int a, int b) {
int ans = dep[a] + dep[b];
int l = pos[a], r = pos[b]; if(l > r) swap(l, r);
int t = Log[r-l+1];
return ans - (min(mnd[t][l], mnd[t][r-(1<<t)+1]) << 1);
} int rt, size, siz[maxn], f[maxn];
bool vis[maxn];
void getrt(int u, int pa) {
siz[u] = 1; f[u] = 0;
for(int e = head[u]; e; e = nxt[e]) if(to[e] != pa && !vis[to[e]]) {
getrt(to[e], u);
siz[u] += siz[to[e]];
f[u] = max(f[u], siz[to[e]]);
}
f[u] = max(f[u], size - siz[u]);
if(f[rt] > f[u]) rt = u;
return ;
}
int fa[maxn];
void solve(int u) {
vis[u] = 1;
for(int e = head[u]; e; e = nxt[e]) if(!vis[to[e]]) {
f[rt = 0] = size = siz[u]; getrt(to[e], u);
fa[rt] = u; solve(rt);
}
return ;
} #define maxnode 1600010 struct Node {
int v, r, siz;
Node() {}
Node(int _, int __): v(_), r(__) {}
} ns[maxnode];
int ToT, ch[maxnode][2], Fa[maxnode], rec[maxnode], rcnt;
inline int getnode() {
if(rcnt) {
int o = rec[rcnt--];
ch[o][0] = ch[o][1] = Fa[o] = 0;
return o;
}
return ++ToT;
}
inline void maintain(int o) {
if(!o) return ;
ns[o].siz = ns[ch[o][0]].siz + 1 + ns[ch[o][1]].siz;
return ;
}
inline void rotate(int u) {
int y = Fa[u], z = Fa[y], l = 0, r = 1;
if(z) ch[z][ch[z][1]==y] = u;
if(ch[y][1] == u) swap(l, r);
Fa[u] = z; Fa[y] = u; Fa[ch[u][r]] = y;
ch[y][l] = ch[u][r]; ch[u][r] = y;
maintain(y); maintain(u);
return ;
}
inline void Insert(int& o, int v) {
if(!o) {
ns[o = getnode()] = Node(v, rand());
return maintain(o);
}
bool d = v > ns[o].v;
Insert(ch[o][d], v); Fa[ch[o][d]] = o;
if(ns[ch[o][d]].r > ns[o].r) {
int t = ch[o][d];
rotate(t); o = t;
}
return maintain(o);
}
inline void Del(int& o, int v) {
if(!o) return ;
if(ns[o].v == v) {
if(!ch[o][0] && !ch[o][1]) rec[++rcnt] = o, o = 0;
else if(!ch[o][0]) {
int t = ch[o][1]; Fa[t] = Fa[o]; rec[++rcnt] = o; o = t;
}
else if(!ch[o][1]) {
int t = ch[o][0]; Fa[t] = Fa[o]; rec[++rcnt] = o; o = t;
}
else {
bool d = ns[ch[o][1]].r > ns[ch[o][0]].r;
int t = ch[o][d]; rotate(t); o = t;
Del(ch[o][d^1], v);
}
}
else {
bool d = v > ns[o].v;
Del(ch[o][d], v);
}
return maintain(o);
}
inline int query(int o, int x) {
if(!o) return 0;
int ls = ch[o][0] ? ns[ch[o][0]].siz : 0;
if(x < ns[o].v) return query(ch[o][0], x);
return ls + 1 + query(ch[o][1], x);
} int Rt[maxn], Rtfa[maxn];
bool lit[maxn];
void update(int s) {
if(lit[s]) Insert(Rt[s], 0);
else Del(Rt[s], 0);
for(int u = s; fa[u]; u = fa[u]) {
int d = cdist(fa[u], s);
if(lit[s]) Insert(Rt[fa[u]], d), Insert(Rtfa[u], d);
else Del(Rt[fa[u]], d), Del(Rtfa[u], d);
}
lit[s] ^= 1;
return ;
}
int ask(int s, int x) {
int ans = query(Rt[s], x);
for(int u = s; fa[u]; u = fa[u]) {
int d = cdist(fa[u], s);
ans += query(Rt[fa[u]], x - d) - query(Rtfa[u], x - d);
}
return ans;
} int main() {
n = read();
int sum = 0;
for(int i = 1; i < n; i++) {
int a = read(), b = read(), c = read(); sum += c;
AddEdge(a, b, c);
} build(1, 0); rmq_init();
f[rt = 0] = size = n; getrt(1, 0);
solve(rt); memset(lit, 1, sizeof(lit));
for(int i = 1; i <= n; i++) update(i);
int q = read();
while(q--) {
int tp = read(), u = read();
if(tp == 1) update(u);
if(tp == 2) {
int k = read();
int l = 0, r = sum;
while(l < r) {
int mid = l + r >> 1;
if(ask(u, mid) < k) l = mid + 1; else r = mid;
}
printf("%d\n", l);
}
} return 0;
}
[COJ0968]WZJ的数据结构(负三十二)的更多相关文章
- COJ968 WZJ的数据结构(负三十二)
WZJ的数据结构(负三十二) 难度级别:D: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,边上均有权值,每个点上有 ...
- 数据结构(三十二)图的遍历(DFS、BFS)
图的遍历和树的遍历类似.图的遍历是指从图中的某个顶点出发,对图中的所有顶点访问且仅访问一次的过程.通常有两种遍历次序方案:深度优先遍历和广度优先遍历. 一.深度优先遍历 深度优先遍历(Depth_Fi ...
- COJ966 WZJ的数据结构(负三十四)
WZJ的数据结构(负三十四) 难度级别:C: 运行时间限制:20000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给一棵n个节点的树,请对于形如"u ...
- COJ970 WZJ的数据结构(负三十)
WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...
- COJ 0970 WZJ的数据结构(负三十)树分治
WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...
- [COJ0970]WZJ的数据结构(负三十)
[COJ0970]WZJ的数据结构(负三十) 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计一个数据结构,回答M次操作. 1 x v:对于树上的每一个节点y,如果将x.y在树上的距离记为 ...
- Bootstrap <基础三十二>模态框(Modal)插件
模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等. 如果您想要单独引用该插件的功能,那么您需要引用 ...
- NeHe OpenGL教程 第三十二课:拾取游戏
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- COJ 1003 WZJ的数据结构(三)ST表
WZJ的数据结构(三) 难度级别:B: 运行时间限制:3000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为N的 ...
随机推荐
- VS2010下安装Opencv 分类: Opencv 2014-11-02 13:51 778人阅读 评论(0) 收藏
Opencv作为一种跨平台计算机视觉库,在图像处理领域得到广泛的应用,下面介绍如何在VS2010中安装配置Opencv 一.下载Opencv 下载网址:http://sourceforge.net/p ...
- D. Alyona and a tree 公式转换 + 分块暴力
http://codeforces.com/problemset/problem/740/D 对于每一对<u, v>.设dis[u]表示root到点u的距离,那么dis<u去v> ...
- nvm安装nodejs
1. 安装nvm 下载 nvm-windows解压缩 nvm-windows解压缩 nvm-setup双击运行 nvm-setup.exe选择next选择 [D:\dev][path1] 或 默认路径 ...
- Burp Suite集成sqlmap
本地环境 JDK1.8 Burp Suite 1.7.26 Python2.7 64位安装包 sqlmap zip包 安装python及sqlmap python下载下来默认安装即可,配置系统环境变量 ...
- excel vba 高级过滤
excel vba 高级过滤 Sub shaixuan() Dim database As Range '定义数据区域 Dim criteria_range As Range '定义条件区域 Dim ...
- Record these plug-ins of vscode
实在无聊透顶.写个随笔记录一下vscode的插件好了. 第一次使用(之前一直在用sublime...),以后再更新吧.record my color too! Visual Studio Code B ...
- 【C++】朝花夕拾——STL vector
STL之vector篇 N久之前是拿C的数组实现过vector中的一些简单功能,什么深拷贝.增删查找之类的,以为vector的实现也就是这样了,现在想想真是...too young too naive ...
- 模拟Java-Sping,实现其IOC和AOP核心
简易的IOC框图 注解的方式实现了左边的分支,那么就剩下右边的XML分支: XmlContext:这个类是也是AbstractApplicationContext的子类,和AnnotationCont ...
- CAD参数绘制实心圆弧填充(com接口)
C#中实现代码说明: private void DrawPathToHatch1() { //把路径的开始位置移动指定的点 //参数一为点的X坐标 ,参数二为点的Y坐标,参数三为该点处开始宽度,对Po ...
- MFC如何设置窗口最前
首先,放到最前 this->SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);//使窗口总是在最前面 this->Se ...