【BZOJ 3531】【SDOI 2014】旅行
因为有$10^5$个宗教,需要开$10^5$个线段树。
平时开的线段树是“满”二叉树,但在这个题中代表一个宗教的线段树管辖的区间有很多点都不属于这个宗教,也就不用“把枝叶伸到这个点上”,所以这样用类似主席树的数组动态开点来建立$10^5$个只有几个“树枝”的线段树,维护轻重链就可以了
线段树的$L,R,l,r$弄反了调了好久$QAQ$ $so$ $sad$
#include<cstdio>
#include<cstring>
#include<algorithm>
#define read(x) x=getint()
#define N 100003
using namespace std;
inline int getint() {int k = 0, fh = 1; char c = getchar(); for(; c < '0' || c > '9'; c = getchar()) if (c == '-') fh = -1; for(; c >= '0' && c <= '9'; c = getchar()) k = k * 10 + c - '0'; return k * fh;}
struct Tree {
int l, r, ma, sm;
} T[4000010];
struct G {
int nxt, to;
} E[N << 1];
int root[N], point[N], cnt = 0, n, q, W[N], C[N], sz[N], son[N], up[N];
int deep[N], fa[N], watch[N];
inline void ins(int x, int y) {E[++cnt].nxt = point[x]; E[cnt].to = y; point[x] = cnt;}
inline void _(int x) {
sz[x] = 1;
for(int tmp = point[x]; tmp; tmp = E[tmp].nxt) {
int v = E[tmp].to;
if (v == fa[x]) continue;
fa[v] = x;
deep[v] = deep[x] + 1;
_(v);
if (sz[v] > sz[son[x]])
son[x] = v;
sz[x] += sz[v];
}
}
inline void __(int x) {
watch[x] = ++cnt;
if (!son[x]) return;
up[son[x]] = up[x];
__(son[x]);
for(int tmp =point[x]; tmp; tmp = E[tmp].nxt) {
int v = E[tmp].to;
if (v == fa[x] || v == son[x]) continue;
up[v] = v;
__(v);
}
} inline void pushup(int pos) {
T[pos].sm = T[T[pos].l].sm + T[T[pos].r].sm;
T[pos].ma = max(T[T[pos].l].ma, T[T[pos].r].ma);
}
inline void update(int &pos, int l, int r, int to, int num) {
if (pos == 0) pos = ++cnt;
if (l == r) {T[pos].ma = T[pos].sm = num; return;}
int mid = (l + r) >> 1;
if (to <= mid) update(T[pos].l, l, mid, to, num);
else update(T[pos].r, mid + 1, r, to, num);
pushup(pos);
}
inline int Qsum(int pos, int L, int R, int l, int r) {
if (L <= l && r <= R) return T[pos].sm;
int mid = (l + r) >> 1, s = 0;
if (L <= mid) s += Qsum(T[pos].l, L, R, l, mid);
if (R > mid) s += Qsum(T[pos].r, L, R, mid + 1, r);
return s;
}
inline int Qmax(int pos, int L, int R, int l, int r) {
if (L <= l && r <= R) return T[pos].ma;
int mid = (l + r) >> 1, s = 0;
if (L <= mid) s = Qmax(T[pos].l, L, R, l, mid);
if (R > mid) s = max(s, Qmax(T[pos].r, L, R, mid + 1, r));
return s;
} inline int Max(int CC, int x, int y) {
int ans = 0;
while (up[x] != up[y]) {
if (deep[up[x]] < deep[up[y]]) swap(x, y);
ans = max(ans, Qmax(root[CC], watch[up[x]], watch[x], 1, n));
x = fa[up[x]];
}
if (deep[x] > deep[y]) swap(x, y);
ans = max(ans, Qmax(root[CC], watch[x], watch[y], 1, n));
return ans;
}
inline int Sum(int CC, int x, int y) {
int ans = 0;
while (up[x] != up[y]) {
if (deep[up[x]] < deep[up[y]]) swap(x, y);
ans += Qsum(root[CC], watch[up[x]], watch[x], 1, n);
x = fa[up[x]];
}
if (deep[x] > deep[y]) swap(x, y);
ans += Qsum(root[CC], watch[x], watch[y], 1, n);
return ans;
} int main() {
read(n); read(q);
for(int i = 1; i <= n; ++i)
read(W[i]), read(C[i]);
int u, v;
for(int i = 1; i < n; ++i) {
read(u); read(v);
ins(u, v); ins(v, u);
}
_(1);
up[1] = 1;
cnt = 0;
__(1);
cnt = 0;
for(int i = 1; i <= n; ++i)
update(root[C[i]], 1, n, watch[i], W[i]);
char c; int x, y;
for(int i = 1; i <= q; ++i) {
for(c = getchar(); c < 'A' || c > 'Z'; c = getchar());
if (c == 'C') {
c = getchar(); read(x); read(y);
if (c == 'C') {
update(root[C[x]], 1, n, watch[x], 0);
C[x] = y;
update(root[C[x]], 1, n, watch[x], W[x]);
} else {
update(root[C[x]], 1, n, watch[x], y);
W[x] = y;
}
} else {
c = getchar(); read(x); read(y);
if (c == 'S')
printf("%d\n", Sum(C[x], x, y));
else
printf("%d\n", Max(C[x], x, y));
}
}
return 0;
}
hhhhhhhhhhhhhhhhhh调出来真的不容易啊,树链剖分怎么比$LCT$都难写!!!
$Try$ $Everything$
$演唱:Shakira$
Oh oh oh oh oooh
噢 噢 噢 噢
Oh oh oh oh oooh
噢 噢 噢 噢
Oh oh oh oh oooh
噢 噢 噢 噢
Oh oh oh oh oooh
噢 噢 噢 噢
I messed up tonight I lost another fight
刚搞砸了这一头 却又没顾得上那一头
I still mess up but I'll just start again
反复犯错却仍想重新来过
I keep falling down I keep on hitting the ground
不断跌倒 不断失败 也要不断重来
I always get up now to see what's next
想要触碰未来的心总会让我再站起来 继续大步前迈
Birds don't just fly they fall down and get up
在天空飞翔的鸟儿也难免会受伤会折翼 但也会重新振翅飞起
Nobody learns without getting it won
想要靠近蓝天就得有不怕坠落不怕受伤的勇气
I won't give up no I won't give in
我不会轻言放弃 谁也别想能让我放弃
Till I reach the end and then I'll start again
不停向前直到我达成心愿 然后又向更高的天空迈进
No I won't leave I wanna try everything
我不会逃避也不会迟疑 愿去尝试生命不同的绮丽
I wanna try even though I could fail
即使前路崎岖 未来阴晴不定 但至少我能体会到风雨过后的别样风景
I won't give up no I won't give in
我不会轻言放弃 谁也别想能让我放弃
Till I reach the end and then I'll start again
不停向前直到我达成心愿 然后又向更高的天空迈进
No I won't leave I wanna try everything
我不会逃避也不会迟疑 愿去尝试生命不同的绮丽
I wanna try even though I could fail
即使前路崎岖 未来阴晴不定 但至少我能体会到风雨过后的别样风景
Oh oh oh oh oooh
噢 噢 噢 噢
Try everything
去尝试生命不同的绮丽
Oh oh oh oh oooh
噢 噢 噢 噢
Try everything
去体验风雨过后的别样风景
Oh oh oh oh oooh
噢 噢 噢 噢
Try everything
让勇于尝试的勇气写就生命别样的意义
Oh oh oh oh oooh
噢 噢 噢 噢
Look at how far you've come you filled your heart with love
当你想放弃 就回头看看你走过了多少崎岖 心中的那些爱又成就了一个怎样的你
Baby you've done enough that cut your breath
别泄气 你已经做得够好了
Don't beat yourself up don't need to run so fast
别自暴自弃 也别太过心急
Sometimes we come last but we did our best
成功或许会迟些才降临 但尽过全力至少不会遗憾唏嘘
I won't give up no I won't give in
我不会轻言放弃 谁也别想能让我放弃
Till I reach the end and then I'll start again
不停向前直到我达成心愿 然后又向更高的天空迈进
No I won't leave I wanna try everything
我不会逃避也不会迟疑 愿去尝试生命不同的绮丽
I wanna try even though I could fail
即使前路崎岖 未来阴晴不定 但至少我能体会到风雨过后的别样风景
I won't give up no I won't give in
我不会轻言放弃 谁也别想能让我放弃
Till I reach the end and then I'll start again
不停向前直到我达成心愿 然后又向更高的天空迈进
No I won't leave I wanna try everything
我不会逃避也不会迟疑 愿去尝试生命不同的绮丽
I wanna try even though I could fail
即使前路崎岖 未来阴晴不定 但至少我能体会到风雨过后的别样风景
I'll keep on making those new mistakes
我会继续勇敢去体验 即使我还会不停犯错
I'll keep on making them every day
不断尝试不断犯错
Those new mistakes
不断犯错不断重新来过
Oh oh oh oh oooh
噢 噢 噢 噢
Try everything
不断尝试不断重新来过
Oh oh oh oh oooh
噢 噢 噢 噢
Try everything
不断体验生命中的不同结果
Oh oh oh oh oooh
噢 噢 噢 噢
Try everything
不断体验不断迈向更好的自我
Oh oh oh oh oooh
噢 噢 噢 噢
Try everything
让勇于尝试的勇气写就生命的别样意义
【BZOJ 3531】【SDOI 2014】旅行的更多相关文章
- [BZOJ 3530][Sdoi 2014]数数
阿拉~好像最近总是做到 AC 自动机的题目呢喵~ 题目的算法似乎马上就能猜到的样子…… AC 自动机 + 数位 dp 先暴力转移出 f[i][j] :表示从 AC 自动机上第 j 号节点走 i 步且不 ...
- BZOJ 3533 sdoi 2014 向量集
设(x,y)为Q的查询点,分类讨论如下:1.y>0: 最大化a*x+b*y,维护一个上凸壳三分即可 2.y<0:最大化a*x+b*y 维护一个下凸壳三分即可 我们考虑对时间建出一棵线段 ...
- bzoj 3531 旅行
动态开点线段树+树链剖分 对于每一种宗教信仰都开一颗线段树 空间: QlogN 即每一次修改都只会改变logN 个点 时间 O(QlogN) naive题 边没有开两倍 QAQ bzoj 35 ...
- [BZOJ 3531] [Sdoi2014] 旅行 【离线+LCT】
题目链接:BZOJ - 3531 题目分析 题目询问一条路径上的信息时,每次询问有某种特定的文化的点. 每个点的文化就相当于一种颜色,每次询问一条路径上某种颜色的点的信息. 可以使用离线算法, 类似于 ...
- BZOJ 3531(树链剖分+线段树)
Problem 旅行 (BZOJ 3531) 题目大意 给定一颗树,树上的每个点有两个权值(x,y). 要求维护4种操作: 操作1:更改某个点的权值x. 操作2:更改某个点的权值y. 操作3:求a-- ...
- [BZOJ 1879][SDOI 2009]Bill的挑战 题解(状压DP)
[BZOJ 1879][SDOI 2009]Bill的挑战 Description Solution 1.考虑状压的方式. 方案1:如果我们把每一个字符串压起来,用一个布尔数组表示与每一个字母的匹配关 ...
- [BZOJ 3992] [SDOI 2015] 序列统计(DP+原根+NTT)
[BZOJ 3992] [SDOI 2015] 序列统计(DP+原根+NTT) 题面 小C有一个集合S,里面的元素都是小于质数M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数 ...
- [BZOJ 3123] [SDOI 2013]森林(可持久化线段树+并查集+启发式合并)
[BZOJ 3123] [SDOI 2013]森林(可持久化线段树+启发式合并) 题面 给出一个n个节点m条边的森林,每个节点都有一个权值.有两种操作: Q x y k查询点x到点y路径上所有的权值中 ...
- BZOJ 3531: [Sdoi2014]旅行 [树链剖分]
3531: [Sdoi2014]旅行 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1685 Solved: 751[Submit][Status] ...
随机推荐
- 3-2-1-0-GO
正式开始第3份工作,入职第2天,午饭后与Team Leader谈了1个多小时,很有收获. 首先,不同的公司有不同的企业文化和规章制度,需要尊重且入乡随俗,尽快学习并适应,争取早日融入公司和团队当中去, ...
- 以纯面向对象的JS编写最基本的数据字典案例
之前有讲到过数据字典,什么是数据字典,用来干啥的,这个不细说了,今天来说说如何实现数据字典功能 无非就是维护数据字典,对数据字典对象进行增删改成,曾经我写过一个页面跳转形式的,十分简单,不说了,今天用 ...
- poj1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8859 Accepted: 3728 ...
- 第三方登录 (faceBook )
1.iOS 第三方登录(Facebook,Twitter...)http://www.jianshu.com/p/f64d5ad2ef2a
- 获取assemblies信息in .net core
using System; using System.Linq; using System.Reflection; using System.Runtime.Loader; using Microso ...
- js删除数据的几种方法
js 删除数组几种方法 var arr=['a','b','c']; 若要删除其中的'b',有两种方法: 1.delete方法:delete arr[1] 这种方式数组长度不变,此时arr[1]变为u ...
- Debian 8(Jessie) 安装自带Mysql
执行命令 sudo apt-get install mysql-server 这会把mysql-client也装上, 版本都是5.5. 安装过程中会提示你输入两遍root口令. 用ps aux|gre ...
- Maven 常用命令, 备忘
Maven在现在的Java项目中有非常重要的地位, Maven已经不是Ant这样仅仅用于构建, 首先, 它是一个构建工具, 把源代码编译并打包成可发布应用的构件工具其次, 它是一个依赖管理工具, 集中 ...
- Flex ActionScript版本的Map类型
ActionScript中没有Map类型,因为Object就相当于Map了.Object的属性相当于key,值相当于value. 也就是说,没有必要有Map类型.但是,这样做,也会带来一些问题,造成不 ...
- codevs1910 递归函数
难度等级:黄金 codevs1910 递归函数 题目描述 Description 对于一个递归函数w(a, b, c). 如果a <= 0 or b <= 0 or c <= 0就返 ...