loj#2049. 「HNOI2016」网络(set 树剖 暴力)
题意
Sol
下面的代码是\(O(nlog^3n)\)的暴力。
因为从一个点向上只会跳\(logn\)次,所以可以暴力的把未经过的处理出来然后每个点开个multiset维护最大值
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 2e5 + 10, SS = MAXN * 4, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, Q, fa[MAXN], siz[MAXN], son[MAXN], id[MAXN], top[MAXN], dep[MAXN], times;
vector<int> v[MAXN];
void dfs1(int x, int _fa) {
siz[x] = 1; dep[x] = dep[_fa] + 1; fa[x] = _fa;
for(auto &to : v[x]) {
if(to == _fa) continue;
dfs1(to, x);
siz[x] += siz[to];
if(siz[to] > siz[son[x]]) son[x] = to;
}
}
void dfs2(int x, int topf) {
top[x] = topf; id[x] = ++times;
if(!son[x]) return ;
dfs2(son[x], topf);
for(auto &to : v[x]) {
if(top[to]) continue;
dfs2(to, to);
}
}
multiset<int> s[SS];
struct Query {
int a, b, v;
}q[MAXN];
vector<Pair> line[MAXN];
int ls[SS], rs[SS], root, tot;
void Erase(multiset<int> &s, int v) {
auto it = s.find(v);
if(it != s.end()) s.erase(it);
}
void Get(vector<Pair> &v, int x, int y) {
vector<Pair> tmp;
while(top[x] ^ top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
tmp.push_back({id[top[x]], id[x]});
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
tmp.push_back({id[x], id[y]});
sort(tmp.begin(), tmp.end());
int las = 1;
for(auto x : tmp) {
if(las <= x.fi - 1) v.push_back({las, x.fi - 1});
las = x.se + 1;
}
if(las <= N) v.push_back({las, N});
}
int Mx(multiset<int> &s) {
if(s.empty()) return -1;
auto it = s.end(); it--;
return *it;
}
void IntAdd(int &k, int l, int r, int ql, int qr, int v, int opt) {
if(!k) k = ++tot;
if(ql <= l && r <= qr) {
if(opt == 1) s[k].insert(v);
else Erase(s[k], v);
return ;
}
int mid = l + r >> 1;
if(ql <= mid) IntAdd(ls[k], l, mid, ql, qr, v, opt);
if(qr > mid) IntAdd(rs[k], mid + 1, r, ql, qr, v, opt);
}
int Query(int k, int l, int r, int p) {
if(!k) return -1;
int ans = Mx(s[k]), mid = l + r >> 1;
if(l == r) return Mx(s[k]);
if(p <= mid) chmax(ans, Query(ls[k], l, mid, p));
else chmax(ans, Query(rs[k], mid + 1, r, p));
return ans;
}
void TreeAdd(int x, int y, int v, int opt) {
while(top[x] ^ top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
IntAdd(root, 1, N, id[top[x]], id[x], v, opt);
x = fa[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
IntAdd(root, 1, N, id[x], id[y], v, opt);
}
void Add(int ti, int opt) {
int x = q[ti].a, y = q[ti].b, v = q[ti].v;
if(opt == 1) Get(line[ti], x, y);
for(auto x : line[ti])
IntAdd(root, 1, N, x.fi, x.se, v, opt);
}
signed main() {
// Fin(a); Fout(b);
N = read(); Q = read();
for(int i = 1; i <= N - 1; i++) {
int x = read(), y = read();
v[x].push_back(y);
v[y].push_back(x);
}
dfs1(1, 0);
dfs2(1, 1);
for(int i = 1; i <= Q; i++) {
int opt = read();
if(opt == 0) {
int a = read(), b = read(), v = read(); q[i] = {a, b, v};
Add(i, 1);
} else if(opt == 1) {
int ti = read();
Add(ti, -1);
} else if(opt == 2) {
int x = read();
printf("%d\n", Query(root, 1, N, id[x]));
}
}
return 0;
}
loj#2049. 「HNOI2016」网络(set 树剖 暴力)的更多相关文章
- 「HNOI2016」网络 解题报告
「HNOI2016」网络 我有一个绝妙的可持久化树套树思路,可惜的是,它的空间是\(n\log^2 n\)的... 注意到对一个询问,我们可以二分答案 然后统计经过这个点大于当前答案的路径条数,如果这 ...
- loj#2255. 「SNOI2017」炸弹 线段树优化建图,拓扑,缩点
loj#2255. 「SNOI2017」炸弹 线段树优化建图,拓扑,缩点 链接 loj 思路 用交错关系建出图来,发现可以直接缩点,拓扑统计. 完了吗,不,瓶颈在于边数太多了,线段树优化建图. 细节 ...
- loj #2051. 「HNOI2016」序列
#2051. 「HNOI2016」序列 题目描述 给定长度为 n nn 的序列:a1,a2,⋯,an a_1, a_2, \cdots , a_na1,a2,⋯,an,记为 a[1: ...
- LOJ 2551 「JSOI2018」列队——主席树+二分
题目:https://loj.ac/problem/2551 答案是排序后依次走到 K ~ K+r-l . 想维护一个区间排序后的结果,使得可以在上面二分.求和:二分可以知道贡献是正还是负. 于是想用 ...
- LOJ 2555 「CTSC2018」混合果汁——主席树
题目:https://loj.ac/problem/2555 二分答案,在可以选的果汁中,从价格最小的开始选. 按价格排序,每次可以选的就是一个前缀.对序列建主席树,以价格为角标,维护体积和.体积*价 ...
- LOJ#2052. 「HNOI2016」矿区(平面图转对偶图)
题面 传送门 题解 总算会平面图转对偶图了-- 首先我们把无向边拆成两条单向边,这样的话每条边都属于一个面.然后把以每一个点为起点的边按极角排序,那么对于一条边\((u,v)\),我们在所有以\(v\ ...
- loj2049 「HNOI2016」网络
好像复杂度来说不是正解--不加谜之优化(下叙)能被loj上的加强数据卡 #include <algorithm> #include <iostream> #include &l ...
- loj 2955 「NOIP2018」保卫王国 - 树链剖分 - 动态规划
题目传送门 传送门 想抄一个短一点ddp板子.然后照着Jode抄,莫名其妙多了90行和1.3k. Code /** * loj * Problem#2955 * Accepted * Time: 26 ...
- LOJ #2048. 「HNOI2016」最小公倍数
题意 有 \(n\) 个点,\(m\) 条边,每条边连接 \(u \Leftrightarrow v\) 且权值为 \((a, b)\) . 共有 \(q\) 次询问,每次询问给出 \(u, v, q ...
随机推荐
- 《http权威指南》读书笔记9
概述 最近对http很感兴趣,于是开始看<http权威指南>.别人都说这本书有点老了,而且内容太多.我个人觉得这本书写的太好了,非常长知识,让你知道关于http的很多概念,不仅告诉你怎么做 ...
- LabVIEW(八):程序结构中的循环结构
1.程序结构分为三种:循环结构.分支结构.顺序结构. 本文主要讨论循环结构. 2.While循环 左下角:循环计数端子i,从0开始计数,每进行一次循环,i自动增加1. 右下角:循环条件端子,当循环达到 ...
- 什么 是JavaScript中的字符串类型之间的转换问题详解? 部分4
字符串类型 单双引号都可以!建议使用单引号!(本人建议:个人觉得单个字符串更利于网页优化@特别地方特别处理!); 判断字符串的长度获取方式:变量名.length html中转义符: < < ...
- Elastic Search 上市了,市值翻倍,这群人财务自由了!
国庆长假,大部分人还深浸在风花雪月之中,而就在昨天(美国时间10月5号),我们 Java 程序员所熟知的大名鼎鼎的 Elastic Search 居然在美国纽约证券交易所上市了! 当说到搜索时,大部分 ...
- hd1007
#include <iostream> #include <algorithm> #include <cmath> using namespace std; str ...
- nginx中root和alias的区别
nginx中root和alias的区别
- 【转】vmware 安装 osx 无法登录 appstore 的解决办法 (伪造smbios设备信息)
伪造smbios设备信息 原文网址:http://www.insanelymac.com/forum/topic/292170-how-to-spoof-real-mac-in-vmware/page ...
- python练习三—解析xml
使用python解析xml,主要使用sax的ContentHandler中的标签开始和标签结束的方法驱动,然后在开始(或者结束)事件中决定使用什么处理方法,使用dispatcher来决定并分发到指定方 ...
- MFC控件GDI编程
MFC控件GDI编程 一丶学习内容 1.了解常用的GDI函数绘图. 2.使用常用的画笔画刷. 二丶常用的GDI函数绘图 上方则为我们常用的GDI函数了. 画线 矩形. 以及圆 等等. 2.1 画线代码 ...
- Kubernetes理论基础
Kubernetes理论基础 Kubernetes定义 kubernetes是Google开源的容器集群管理系统,2014年6月开源.在Docker技术之上,为容器应用提供资源调度.部署运行.服务 ...