Codeforces 696E ...Wait for it...(树链剖分)
题目链接 ...Wait for it...
考虑树链剖分。
对于树上的每个点开一个set,记录当前该节点上所有的girls。
每个节点初始的权值为set中的最小值。
询问的时候每次在路径上寻找最小值,并返回这个点的编号。
然后把这个点的set的第一个元素取出,换成下一个元素。
因为女孩总数不超过1e5,所以总查询次数不会超过1e5
修改操作用lazy标记就可以了。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define lson i << 1, L, mid
#define rson i << 1 | 1, mid + 1, R
#define ls i << 1
#define rs i << 1 | 1
#define MP make_pair
#define fi first
#define se second typedef long long LL;
typedef pair <LL, int> PII; const int N = 1e5 + 10;
const LL inf = 1e18; int n, m, q;
int id, cnt = 0;
int ret[N];
int at[N], sz[N], son[N], top[N], f[N], in[N], out[N], deep[N], father[N];
vector <int> v[N];
set <PII> s[N];
LL lazy[N << 2];
PII t[N << 2]; void dfs(int x, int fa, int dep){
sz[x] = 1;
son[x] = 0;
father[x] = fa;
deep[x] = dep;
for (auto u : v[x]){
if (u == fa) continue;
dfs(u, x, dep + 1);
sz[x] += sz[u];
if (sz[son[x]] < sz[u]) son[x] = u;
}
} void dfs2(int x, int tp){
top[x] = tp;
in[x] = f[x] = ++id;
if (son[x]) dfs2(son[x], tp);
for (auto u : v[x]){
if (u == father[x] || u == son[x]) continue;
dfs2(u, u);
}
out[x] = id;
} void pushup(int i){
t[i] = min(t[ls], t[rs]);
} void pushdown(int i){
if (lazy[i]){
lazy[ls] += lazy[i];
lazy[rs] += lazy[i];
t[ls].fi += lazy[i];
t[rs].fi += lazy[i];
lazy[i] = 0;
}
} void build(int i, int L, int R){
lazy[i] = 0;
if (L == R){
t[i] = *s[L].begin();
return;
} int mid = (L + R) >> 1;
build(lson);
build(rson);
pushup(i);
} void update(int i, int L , int R , int l, int r, LL val){
if (l <= L && R <= r){
lazy[i] += val;
t[i].fi += val;
return;
} pushdown(i);
int mid = (L + R) >> 1 ;
if (l <= mid) update(lson, l, r, val);
if (r > mid) update(rson, l, r, val);
pushup(i);
} void modify(int i, int L, int R, int x){
if (L == R){
s[L].erase(s[L].begin());
t[i] = *s[L].begin();
t[i].fi += lazy[i];
return;
}
pushdown(i);
int mid = (L + R) >> 1 ;
if (x <= mid) modify(lson, x) ;
else modify(rson, x);
pushup(i);
} PII query(int i, int L, int R, int l, int r){
if (l <= L && R <= r) return t[i];
pushdown(i);
int mid = (L + R) >> 1 ;
if (r <= mid) return query(lson, l, r);
if (l > mid) return query(rson, l, r);
return min(query(lson, l, r), query(rson, l, r));
} int Query(int x, int y){
PII ans = PII(inf, 0);
while (top[x] ^ top[y]){
if (deep[top[x]] < deep[top[y]]) swap(x, y);
ans = min(ans, query(1, 1, n, f[top[x]], f[x]));
x = father[top[x]];
}
if (deep[x] > deep[y]) swap(x, y);
ans = min(ans, query(1, 1, n, f[x], f[y]));
return ans.se;
} int main(){ scanf("%d%d%d", &n, &m, &q);
rep(i, 2, n){
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
} dfs(1, 0, 0);
dfs2(1, 1); rep(i, 1, n) s[i].insert(MP(inf, 0));
at[0] = N - 1; rep(i, 1, m){
int x;
scanf("%d", &x);
at[i] = x;
s[f[x]].insert(MP(i, i));
} build(1, 1, n); while (q--){
int op, x, y, lim;
LL val;
scanf("%d", &op);
if (op == 2){
scanf("%d%lld", &x, &val);
update(1, 1, n, in[x], out[x], val);
}
else{
scanf("%d%d%d", &x, &y, &lim);
cnt = 0;
while (lim--){
int idx = Query(x, y);
if (idx == 0) break;
ret[++cnt] = idx;
modify(1, 1, n, f[at[idx]]);
}
printf("%d", cnt);
rep(i, 1, cnt) printf(" %d", ret[i]);
putchar(10);
}
} return 0;
}
Codeforces 696E ...Wait for it...(树链剖分)的更多相关文章
- CodeForces 916E Jamie and Tree(树链剖分+LCA)
To your surprise, Jamie is the final boss! Ehehehe. Jamie has given you a tree with n vertices, numb ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge (最小生成树+树链剖分)
题目链接:http://codeforces.com/contest/609/problem/E 给你n个点,m条边. 问枚举每条边,问你加这条边的前提下组成生成树的权值最小的树的权值和是多少. 先求 ...
- Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分
D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...
- 【Codeforces】【网络流】【树链剖分】【线段树】ALT (CodeForces - 786E)
题意 现在有m个人,每一个人都特别喜欢狗.另外还有一棵n个节点的树. 现在每个人都想要从树上的某个节点走到另外一个节点,且满足要么这个人自带一条狗m,要么他经过的所有边h上都有一条狗. 2<=n ...
- Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...
- Codeforces 704E Iron Man [树链剖分,计算几何]
Codeforces 这题--真是搞死我了-- 好不容易下定了决心要不颓废,要写题,结果一调就调了十几个小时-- 思路 我们发现在树上做非常不舒服,于是树链剖分之后一次在重链上的移动就可以看做是在df ...
- D. Happy Tree Party CodeForces 593D【树链剖分,树边权转点权】
Codeforces Round #329 (Div. 2) D. Happy Tree Party time limit per test 3 seconds memory limit per te ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- CodeForces - 343D 树链剖分
题目链接:http://codeforces.com/problemset/problem/343/D 题意:给定一棵n个n-1条边的树,起初所有节点权值为0,然后m个操作. 1 x:把x为根的子树的 ...
随机推荐
- 【kindle】【转发】kindle链接WIFI自动断开问题
在电脑上新建一个新文件,名为“WIFI_NO_NET_PROBE”,同时把后缀名删掉,让它变成一个无格式文件.Kindle 连接电脑,把新建的文件放进Kindle的根目录,断开Kindle之后重启Ki ...
- Python基础——模块与包
在Python中,可以用import导入需要的模块.包.库.文件等. 把工作路径导入系统路径 import os#os是工作台 import sys#sys是系统 sys.path.append(os ...
- .Net Task常见问题
最近尝试使用一下Task,但是使用过程中因为API的不熟悉碰到了很多问题,不清楚什么时间来调用Task.Start(),具体该怎么使用等等. 如下所描述的Task.Start()方法均为实例方法. 1 ...
- 在ie9下在textbox框里面输入内容按enter键会触发按钮的事件
问题 在ie下,如果存在有button标签,如果在textbox里面输入内容,按下enter键,则会触发第一个按钮的click事件,经过测试,在IE10以及以下的都存在这个问题 原因 浏览器默认行为不 ...
- loj2256 「SNOI2017」英雄联盟
真的是裸背包啊-- #include <iostream> #include <cstdio> using namespace std; typedef long long l ...
- Leetcode30--->Substring with Concatenation of All Words(主串中找出连接给定所有单词的子串的位置)
题目:给定一个字符串S(主串),一个字符串数组words,其中的字符串的长度相同.找到所有的子串位置,要求是words中字符串的一个连接: 举例: For example, given:s: &quo ...
- fedora安装rails缺少js runtime和cannot load such file -- sqlite3/sqlite3_native解决办法
装完rails后创建应用程序: rails new demo 进入创建的demo文件夹 cd demo 检查安装环境 rake about 这时出现错误 Could not find a JavaSc ...
- [转]linux多命令的顺序执行
当我们需要一次执行多个命令的时候,命令之间需要用连接符连接,不同的连接符有不同的效果. (1) ; 分号,没有任何逻辑关系的连接符.当多个命令用分号连接时,各命令之间的执行成功与否彼此没有任何影响,都 ...
- 安装anaconda并配置环境
安装anaconda的步骤 1.确定系统信息 uname -a 2.下载对应版本 3.sh 安装shell脚本 4.添加到对应路径 5.安装完anaconda之后,创建虚拟环境 conda creat ...
- List容器——ArrayList及常用API
List: ① List容器是有序的collection(也称为序列).此接口的用户可以对List容器中每个元素的插入位置进行精确地控制.用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜 ...