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 ...
随机推荐
- swiper轮播在ie浏览器上遇到的显示问题探索
前言: 最近项目有一个需求,想要下图效果,鼠标指向头像图片,图片会放大同时上面的轮播会跟着切换: 鼠标移开头像图片,图片变回原来的大小 注:下图是我根据上面需求已经实现的效果,所以截图方便说明 思考: ...
- 使用PageHepler分页
首先需要引入依赖 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>p ...
- Jquery百宝箱
引入jquery <script src="https://blog-static.cnblogs.com/files/dongxiaodong/jquery-3.3.1.min.js ...
- python基础-变量运算符(3)
一.注释 注释就是对代码的解释和说明.目的是为了让别人和自己很容易看懂.为了让别人一看就知道这段代码是做什么用的.正确的程序注释一般包括序言性注释和功能性注释.序言性注释的主要内容包括模块的接口.数据 ...
- python(leetcode)-重复元素算法题
leetcode初级算法 问题描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 该问题表述非常简单 ...
- (转)p解决 java.util.prefs.BackingStoreException 报错问题
原文:https://blog.csdn.net/baidu_32739019/article/details/78405444 https://developer.ibm.com/answers/q ...
- nginx入门教程
nginx入门教程 一.概述 什么是nginx? Nginx (engine x) 是一款轻量级的Web 服务器 .反向代理服务器及电子邮件(IMAP/POP3)代理服务器. 什么是反向 ...
- 【干货】利用MVC5+EF6搭建博客系统(一)EF Code frist、实现泛型数据仓储以及业务逻辑
习MVC有一段时间了,决定自己写一套Demo了,写完源码再共享. PS:如果图片模糊,鼠标右击复制图片网址,然后在浏览器中打开即可. 一.框架搭建 二.创建数据库 1.创建一个空的EF code fr ...
- js中对象和对象创建方法
这一次我们来说一说在JavaScript中经常会用到的一个复杂基本类型,对象,先从对象的属性讲起,再讲对象的创建方法,基本涵盖了创建对象的各种方法,大家一起学习呀~ 一.对象 要掌握对象的使用及继承, ...
- 翻译:SET子句(已提交到MariaDB官方手册)
本文为mariadb官方手册:SET的译文. 原文:https://mariadb.com/kb/en/set/我提交到MariaDB官方手册的译文:https://mariadb.com/kb/zh ...