[HNOI2016]网络 [树链剖分,可删除堆]
考虑在 |不在| 这条链上的所有点上放上一个 \(x\),删除也是,然后用可删除堆就随便草掉了。
// powered by c++11
// by Isaunoya
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
#define ve vector
#define Tp template
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
#define fir first
#define sec second
// the cmin && cmax
Tp<class T> void cmax(T& x, const T& y) {
if (x < y) x = y;
}
Tp<class T> void cmin(T& x, const T& y) {
if (x > y) x = y;
}
// sort , unique , reverse
Tp<class T> void sort(ve<T>& v) { sort(all(v)); }
Tp<class T> void unique(ve<T>& v) {
sort(all(v));
v.erase(unique(all(v)), v.end());
}
Tp<class T> void reverse(ve<T>& v) { reverse(all(v)); }
const int SZ = 0x191981;
struct FILEIN {
char qwq[SZ], *S = qwq, *T = qwq, ch;
char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
FILEIN& operator>>(char& c) {
while (isspace(c = GETC()))
;
return *this;
}
FILEIN& operator>>(string& s) {
while (isspace(ch = GETC()))
;
s = ch;
while (!isspace(ch = GETC())) s += ch;
return *this;
}
Tp<class T> void read(T& x) {
bool sign = 1;
while ((ch = GETC()) < 0x30)
if (ch == 0x2d) sign = 0;
x = (ch ^ 0x30);
while ((ch = GETC()) > 0x2f) x = x * 0xa + (ch ^ 0x30);
x = sign ? x : -x;
}
FILEIN& operator>>(int& x) { return read(x), *this; }
FILEIN& operator>>(unsigned& x) { return read(x), *this; }
} in;
struct FILEOUT {
const static int LIMIT = 0x114514;
char quq[SZ], ST[0x114];
signed sz, O;
~FILEOUT() { flush(); }
void flush() {
fwrite(quq, 1, O, stdout);
fflush(stdout);
O = 0;
}
FILEOUT& operator<<(char c) { return quq[O++] = c, *this; }
FILEOUT& operator<<(string str) {
if (O > LIMIT) flush();
for (char c : str) quq[O++] = c;
return *this;
}
Tp<class T> void write(T x) {
if (O > LIMIT) flush();
if (x < 0) {
quq[O++] = 0x2d;
x = -x;
}
do {
ST[++sz] = x % 0xa ^ 0x30;
x /= 0xa;
} while (x);
while (sz) quq[O++] = ST[sz--];
return;
}
FILEOUT& operator<<(int x) { return write(x), *this; }
FILEOUT& operator<<(unsigned x) { return write(x), *this; }
} out;
int n, m;
struct P {
priority_queue<int> a, b;
inline void push(int x) { a.push(x); }
inline void erase(int x) { b.push(x); }
inline int top() {
while (!a.empty() && !b.empty() && a.top() == b.top()) a.pop(), b.pop();
if (a.empty()) return -1;
return a.top();
}
};
const int maxn = 1e5 + 51;
P s[maxn << 2];
void upd(int a, int b, int l, int r, int rt, int x) {
if (a <= l && r <= b) {
s[rt].push(x);
return;
}
int mid = l + r >> 1;
if (a <= mid) upd(a, b, l, mid, rt << 1, x);
if (b > mid) upd(a, b, mid + 1, r, rt << 1 | 1, x);
}
void erase(int a, int b, int l, int r, int rt, int x) {
if (a <= l && r <= b) {
s[rt].erase(x);
return;
}
int mid = l + r >> 1;
if (a <= mid) erase(a, b, l, mid, rt << 1, x);
if (b > mid) erase(a, b, mid + 1, r, rt << 1 | 1, x);
}
int ans = -1;
void qry(int l, int r, int x, int rt) {
cmax(ans, s[rt].top());
if (l == r) return;
int mid = l + r >> 1;
(x <= mid) ? qry(l, mid, x, rt << 1) : qry(mid + 1, r, x, rt << 1 | 1);
}
vector<int> g[maxn];
int sz[maxn], son[maxn], d[maxn], fa[maxn];
void dfs(int u) {
sz[u] = 1;
for (int v : g[u])
if (v ^ fa[u]) {
fa[v] = u, d[v] = d[u] + 1, dfs(v), sz[u] += sz[v];
if (sz[v] > sz[son[u]]) son[u] = v;
}
}
int top[maxn], dfn[maxn], idx = 0;
void dfs(int u, int t) {
dfn[u] = ++idx, top[u] = t;
if (son[u]) dfs(son[u], t);
for (int v : g[u])
if (v ^ fa[u] && v ^ son[u]) dfs(v, v);
}
vector<pii> v, vec;
void swap(int& x, int& y) { x ^= y ^= x ^= y; }
void upd(int x, int y, int c) {
v.clear(), vec.clear();
while (top[x] ^ top[y]) {
if (d[top[x]] < d[top[y]]) swap(x, y);
v.push_back({ dfn[top[x]], dfn[x] });
x = fa[top[x]];
}
if (d[x] > d[y]) swap(x, y);
v.push_back({ dfn[x], dfn[y] });
sort(v);
int las = 1;
for (pii x : v) {
if (x.first > las) vec.push_back({ las, x.first - 1 });
las = x.second + 1;
}
if (las <= n) vec.push_back({ las, n });
for (pii x : vec) upd(x.first, x.second, 1, n, 1, c);
}
void erase(int x, int y, int c) {
v.clear(), vec.clear();
while (top[x] ^ top[y]) {
if (d[top[x]] < d[top[y]]) swap(x, y);
v.push_back({ dfn[top[x]], dfn[x] });
x = fa[top[x]];
}
if (d[x] > d[y]) swap(x, y);
v.push_back({ dfn[x], dfn[y] });
sort(v);
int las = 1;
for (pii x : v) {
if (x.first > las) vec.push_back({ las, x.first - 1 });
las = x.second + 1;
}
if (las <= n) vec.push_back({ las, n });
for (pii x : vec) erase(x.first, x.second, 1, n, 1, c);
}
const int maxm = 2e5 + 52;
int A[maxm], B[maxm], V[maxm];
signed main() {
#ifdef _WIN64
freopen("testdata.in", "r", stdin);
#else
ios_base ::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
#endif
// code begin.
in >> n >> m;
rep(i, 2, n) {
int u, v;
in >> u >> v, g[u].pb(v), g[v].pb(u);
}
dfs(1), dfs(1, 1);
rep(i, 1, m) {
int op;
in >> op;
if (op == 0) {
int x, y, c;
in >> x >> y >> c;
upd(x, y, c);
A[i] = x, B[i] = y, V[i] = c;
}
if (op == 1) {
int t;
in >> t, erase(A[t], B[t], V[t]);
}
if (op == 2) {
int u;
in >> u, ans = -1, qry(1, n, dfn[u], 1);
out << ans << '\n';
}
}
return 0;
// code end.
}
[HNOI2016]网络 [树链剖分,可删除堆]的更多相关文章
- BZOJ4538:[HNOI2016]网络(树链剖分,堆)
Description 一个简单的网络系统可以被描述成一棵无根树.每个节点为一个服务器.连接服务器与服务器的数据线则看做 一条树边.两个服务器进行数据的交互时,数据会经过连接这两个服务器的路径上的所有 ...
- [HNOI2016]网络 树链剖分,堆
[HNOI2016]网络 LG传送门 表示乱搞比正解难想. 整体二分很好想吧. 但是为了好写快乐,我们选择三个\(\log\)的乱搞. 先树剖,线段树套堆维护区间最大值.对于一次修改,如果是插入,就把 ...
- uoj30【CF Round #278】Tourists(圆方树+树链剖分+可删除堆)
- 学习了一波圆方树 学习了一波点分治 学习了一波可删除堆(巧用 ? STL) 传送门: Icefox_zhx 注意看代码看怎么构建圆方树的. tips:tips:tips:圆方树内存记得开两倍 CO ...
- luoguP3250 [HNOI2016]网络 树链剖分 + 堆
机房某大佬告诉我,一条链在全局线段树中的区间最多有$log$段 因此同样的,代表不在这条链上的区间同样只有$log$段 对这$log$段区间进行维护即可 为了能够删除,在线段树的每个节点暴力维护一个堆 ...
- BZOJ5210 最大连通子块和 【树链剖分】【堆】【动态DP】
题目分析: 解决了上次提到的<切树游戏>后,这道题就是一道模板题. 注意我们需要用堆维护子重链的最大值.这样不会使得复杂度变坏,因为每个重链我们只考虑一个点. 时间复杂度$O(nlog^2 ...
- 【BZOJ4538】【HNOI2016】网络(树链剖分,线段树,堆)
题目链接,我是真的懒得调题目的格式... 题解 树链剖分搞一下LCA 把线段树弄出来 这只是形式上的线段树 本质上是维护一段区间的一个堆 每次把堆插入节点, 询问的时候查询线段树上的堆的最大值就行了 ...
- 2019.01.13 bzoj4538: [Hnoi2016]网络(树链剖分)
传送门 树链剖分一眼题. 题意简述: 给定一棵树,有三种操作: 加入一条路径 删除一条已加入的路径 询问不过一个点x的路径的最大值. 思路: 直接树链剖分维护答案. 因为询问的事不过点xxx的最大值, ...
- 【bzoj5210】最大连通子块和 树链剖分+线段树+可删除堆维护树形动态dp
题目描述 给出一棵n个点.以1为根的有根树,点有点权.要求支持如下两种操作: M x y:将点x的点权改为y: Q x:求以x为根的子树的最大连通子块和. 其中,一棵子树的最大连通子块和指的是:该子树 ...
- 焦作网络赛E-JiuYuanWantstoEat【树链剖分】【线段树】
You ye Jiu yuan is the daughter of the Great GOD Emancipator. And when she becomes an adult, she wil ...
随机推荐
- 面试官:能解释一下javascript中bind、apply和call这三个函数的用法吗
一.前言 不知道大家还记不记得前几篇的文章:<面试官:能解释一下javascript中的this吗> 那今天这篇文章虽然是介绍javascript中bind.apply和call函数 ...
- React 函数生命周期
React 函数生命周期基础 1 ,概念 在组件创建.到加载到页面上运行.以及组件被销毁的过程中,总是伴随着各种各样的事件,这些在组件特定时期,触发的事件,统称为组件的生命周期:* 2,组件生命周 ...
- C++ 自动类型推断
C++语言提供了自动类型推断的机制,用于简化代码书写,这是一种很不错的特性,使用auto和decltype都可以完成自动类型推断的工作,而且都工作在编译期,这表示在运行时不会有任何的性能损耗. 一.a ...
- c++中各类型数据占据的字节长度
c++中各种类型数据类型占据字节长度 首先罗列一下C++中的数据类型都有哪些: 1.整形:int.long 2.字符型:char.wchar_t 3.布尔型:bool 4.浮点型:float.doub ...
- postgresql spi开发笔记
#include "postgres.h" #include "fmgr.h" #include <string.h> #ifdef PG_MODU ...
- 五大常见算法策略之——动态规划策略(Dynamic Programming)
Dynamic Programming Dynamic Programming是五大常用算法策略之一,简称DP,译作中文是"动态规划",可就是这个听起来高大上的翻译坑苦了无数人 ...
- 【Detection】物体识别-制作PASCAL VOC数据集
PASCAL VOC数据集 PASCAL VOC为图像识别和分类提供了一整套标准化的优秀的数据集,从2005年到2012年每年都会举行一场图像识别challenge 默认为20类物体 1 数据集结构 ...
- learn about sqlserver partitition and partition table --- add or remove table partitions addition more
Yes . In the previous. chapter , we see how to generate "partition function" "parttit ...
- Go语言实现:【剑指offer】题目汇总
所列题目与牛客网<剑指offer>专题相对应. 数组: 和为S的两个数字 和为S的连续正数序列 连续子数组的最大和 数字在排序数组中出现的次数 数组中只出现一次的数字 旋转数组的最小数字 ...
- Jmeter之设置动态关联
前言 在Jmeter中,如何进行接口关联(上一个接口的返回参数作为下一个接口的入参使用)测试呢?下面我们一起来学习吧! 需求:需要利用商品信息接口的返回结果skuName值作为下一个登录接口参数Use ...