BZOJ1146——[CTSC2008]网络管理Network
1、题目大意:就是在动态的树上路径权值第k大。
2、分析:这个就是树链剖分+树套树
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 1000000
int Height[M], Top[M], value[M], num[M], Size[M], Fa[M];
int ST_tot, tot;
int son[M], head[M], Next[M];
int n, m;
struct Node{
Node *ch[2];
int cnt, num, r, v;
bool operator < (const Node& rhs) const{
return r < rhs.r;
}
int cmp(int x){
if(x == v) return -1;
if(x < v) return 0;
return 1;
}
void maintain(){
cnt = num;
if(ch[0]) cnt += ch[0] -> cnt;
if(ch[1]) cnt += ch[1] -> cnt;
}
} *root[2 * M], ft[5 * M];
int treap_tot;
inline void treap_rotate(Node* &o, int d){
Node* k = o -> ch[d ^ 1];
o -> ch[d ^ 1] = k -> ch[d];
k -> ch[d] = o;
o -> maintain();
k -> maintain();
o = k;
return;
}
inline void treap_insert(Node* &o, int x){
if(o == NULL){
o = &ft[treap_tot ++];
o -> ch[0] = o -> ch[1] = NULL;
o -> cnt = o -> num = 1;
o -> v = x;
o -> r = rand();
}
else{
int d = o -> cmp(x);
if(d == -1){
o -> num ++;
}
else{
treap_insert(o -> ch[d], x);
if(o < o -> ch[d]) treap_rotate(o, d ^ 1);
}
}
o -> maintain();
}
inline void treap_remove(Node* &o, int x){
int d = o -> cmp(x);
if(d == -1){
if(o -> num > 1) o -> num --;
else if(o -> ch[0] == NULL) o = o -> ch[1];
else if(o -> ch[1] == NULL) o = o -> ch[0];
else {
int d2;
if(o -> ch[0] > o -> ch[1]) d2 = 1;
else d2 = 0;
treap_rotate(o, d2);
treap_remove(o -> ch[d2], x);
}
}
else treap_remove(o -> ch[d], x);
if(o) o -> maintain();
}
inline int treap_lessk(Node* &o, int k){
if(o == NULL) return 0;
int d = o -> cmp(k);
if(d == -1){
int ret = 0;
if(o -> ch[0]) ret += o -> ch[0] -> cnt;
return ret;
}
else if(d == 0){
return treap_lessk(o -> ch[0], k);
}
else{
int ss = o -> num;
if(o -> ch[0]) ss += o -> ch[0] -> cnt;
return treap_lessk(o -> ch[1], k) + ss;
}
}
inline void init(){
Top[1] = 1;
memset(head, -1, sizeof(head));
tot = ST_tot = 0;
}
inline void add(int l, int r, int o, int x, int y, int z){
if(y != -1) treap_remove(root[o], y);
treap_insert(root[o], z);
if(l == r) {
return;
}
int mid = (l + r) / 2;
if(x <= mid) add(l, mid, 2 * o, x, y, z);
else add(mid + 1, r, 2 * o + 1, x, y, z);
}
inline int query(int l, int r, int o, int x, int y, int z){
if(x <= l && r <= y) return root[o] -> cnt - treap_lessk(root[o], z);
int ret = 0, mid = (l + r) / 2;
if(x <= mid) ret += query(l, mid, 2 * o, x, y, z);
if(y > mid) ret += query(mid + 1, r, 2 * o + 1, x, y, z);
return ret;
}
inline void insert(int x, int y){
tot ++;
son[tot] = y;
Next[tot] = head[x];
head[x] = tot;
}
inline void dfs1(int x, int fa, int height){
Fa[x] = fa;
Height[x] = height;
Size[x] = 1;
for(int i = head[x]; i != -1; i = Next[i]) if(son[i] != fa){
dfs1(son[i], x, height + 1);
Size[x] += Size[son[i]];
}
}
inline void dfs2(int x, int fa){
++ ST_tot;
num[x] = ST_tot;
add(1, n, 1, ST_tot, -1, value[x]);
int o = 0, ss = 0;
for(int i = head[x]; i != -1; i = Next[i]) if(son[i] != fa){
if(Size[son[i]] > ss){
ss = Size[son[i]];
o = i;
}
}
if(o != 0){
Top[son[o]] = Top[x];
dfs2(son[o], x);
}
for(int i = head[x]; i != -1; i = Next[i]) if(son[i] != fa && o != i){
Top[son[i]] = son[i];
dfs2(son[i], x);
}
}
inline void real_add(int x, int y){
add(1, n, 1, num[x], value[x], y);
value[x] = y;
}
inline int check(int x, int y, int k){
int ret = 0;
while(Top[x] != Top[y]){
if(Height[Top[x]] < Height[Top[y]]) swap(x, y);
ret += query(1, n, 1, num[Top[x]], num[x], k);
x = Fa[Top[x]];
}
if(Height[x] < Height[y]) swap(x, y);
ret += query(1, n, 1, num[y], num[x], k);
return ret;
}
inline int real_query(int x, int y, int k){
int l = -1, r = 100000000;
while(l < r){
int mid = (l + r) / 2;
if(mid == l) mid ++;
if(check(x, y, mid) >= k) l = mid;
else r = mid - 1;
}
if(l == -1) return -1;
return l;
}
int main(){
scanf("%d%d", &n, &m);
init();
for(int i = 1; i <= n; i ++) scanf("%d", &value[i]);
for(int i = 1; i < n; i ++){
int x, y;
scanf("%d%d", &x, &y);
insert(x, y);
insert(y, x);
}
dfs1(1, 0, 1);
dfs2(1, 0);
for(int i = 1; i <= m; i ++){
int k, x, y;
scanf("%d%d%d", &k, &x, &y);
if(k == 0){
real_add(x, y);
}
else if(k > 0){
int qq = real_query(x, y, k);
if(qq == -1){
printf("invalid request!\n");
}
else{
printf("%d\n", qq);
}
}
}
return 0;
}
BZOJ1146——[CTSC2008]网络管理Network的更多相关文章
- [BZOJ1146][CTSC2008]网络管理Network
[BZOJ1146][CTSC2008]网络管理Network 试题描述 M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个 部门之间协同工作,公司搭建 ...
- BZOJ1146 [CTSC2008]网络管理Network 树链剖分 主席树 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1146 题意概括 在一棵树上,每一个点一个权值. 有两种操作: 1.单点修改 2.询问两点之间的树链 ...
- [BZOJ1146][CTSC2008]网络管理Network(二分+树链剖分+线段树套平衡树)
题意:树上单点修改,询问链上k大值. 思路: 1.DFS序+树状数组套主席树 首先按照套路,关于k大值的问题,肯定要上主席树,每个点维护一棵权值线段树记录它到根的信息. 关于询问,就是Que(u)+Q ...
- 2019.01.13 bzoj1146: [CTSC2008]网络管理Network(整体二分+树剖)
传送门 题意简述:给一棵树,支持单点修改,询问路径上两点间第kkk大值. 思路: 读懂题之后立马可以想到序列上带修区间kkk大数的整体二分做法,就是用一个bitbitbit来支持查值. 那么这个题把树 ...
- 【树上莫队】【带修莫队】【权值分块】bzoj1146 [CTSC2008]网络管理Network
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...
- 【树链剖分】【函数式权值分块】bzoj1146 [CTSC2008]网络管理Network
裸题,直接上.复杂度O(n*sqrt(n)*log(n)). //Num[i]表示树中的点i在函数式权值分块中对应的点 //Map[i]表示函数式权值分块中的点i在树中对应的点 #include< ...
- 【BZOJ1146】[CTSC2008]网络管理Network 树状数组+DFS序+主席树
[BZOJ1146][CTSC2008]网络管理Network Description M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工 ...
- BZOJ 1146: [CTSC2008]网络管理Network [树上带修改主席树]
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3522 Solved: 1041[Submi ...
- 【BZOJ-1146】网络管理Network DFS序 + 带修主席树
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3495 Solved: 1032[Submi ...
随机推荐
- BZOJ3685: 普通van Emde Boas树
显然这题的所有操作都可以用set,但是直接用set肯定要T,考虑到读入量较大,使用fread读入优化,就可以卡过去了. #include<bits/stdc++.h> using name ...
- PHP中常用的字符串格式化函数总结
注意:在PHP中提供的字符串函数处理的字符串,大部分都不是在原字符串上修改,而是返回一个格式化后的新字符串. 一.取出空格和字符串填补函数 空格也是一个有效的字符,在字符串中也会占据一个位置.用户在表 ...
- sql 行转 列, 列转行
行列互转 复制代码 create table test(id ),quarter int,profile int) insert into test values(,,) insert into te ...
- radar js
<!doctype html> <html> <head> <meta charset="utf-8"> <link href ...
- LaTex 使用 - 配置
Reference Link: http://www.howtotex.com/howto/installing-latex-on-windows/ MikTeX:http://miktex.org/ ...
- xheditor编辑器的使用
xheditor编辑器的使用 一个博客.cms网站都一定会用到一个html编辑器,刚好xmfdsh在做网站时候需要用到这类编辑器,在对比了之后,发现其实差不了多少,刚好一个不错的friend在用xhe ...
- Java并发编程核心方法与框架-Future和Callable的使用
Callable接口与Runnable接口对比的主要优点是Callable接口可以通过Future获取返回值.但是Future接口调用get()方法取得结果时是阻塞的,如果调用Future对象的get ...
- H5 使用
关闭页面 http://www.bcty365.com/content-146-3343-1.html 回退页面: plus.key.addEventListener('backbutton', fu ...
- 黄永成-thinkphp讲解-个人博客讲解26集
如果是要导入 前后台分组都公用的 第三方类库, 就将类库(*.class.php文件) 放在 App下的Class文件夹中. 导入方法是: import('Class.Image', APP_PATH ...
- Java字节流:FilterInputStream FilterOutputStream
----------------------------------------------------------------------------------- FilterInputStrea ...