【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] ...
随机推荐
- [多图]Windows 10 Build 10565今推送:优化界面菜单 Cortana改进
酷站网软:此前的Windows Build 10558并没有向公众发布,而是直到近日才向Fast Ring用户推送了更多功能和改进的Build 10565版.除之前版本上的加入了Messaging.E ...
- CSS中单位px和em,rem的区别
PX特点: 1 IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的原因在于其使用了em或rem作为字体单位: 3. Firefox能够调整px和em,rem,但是96%以上 ...
- mysql连接数设置操作(Too many connections)
mysql在使用过程中,发现连接数超了~~~~ [root@linux-node1 ~]# mysql -u glance -h 192.168.1.17 -pEnter password: ERRO ...
- C语言:联合变量
#include <stdio.h> union hold{ int digit; double big; char letter; }; int main(){ union hold a ...
- CoreAnimation笔记
核心动画继承结构 CoreAnimation Core Animation是直接作用在CALayer上的(并非UIView上)非常强大的跨Mac OS X和iOS平台的动画处理API,Core Ani ...
- oracle编程总结
1,SEQUENCE的使用 问题:在MSSQL中,我们可以通过设置自增长来作为主键,但是oracle里面没有这个 解决方案:使用SEQUENCE来实现,具体步骤如下 (1)首先建立一个序列(就是每次查 ...
- 窗口 - dialog - 与后端交互
与后端交互,一般需要提交表单数据,所以,这次渲染得dialog其实是一个<form> <form id="loginForm"> <table ali ...
- Java集合系列:-----------08HashMap的底层实现
对于HashMap感觉一直是看了忘,忘了看.这里就单独写一篇日志来记录一下.HashMap的底层实现. 非常好的讲HashMap的博客:http://blog.csdn.net/vking_wang/ ...
- BZOJ 2301 【HAOI2011】 Problem b
Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...
- java:读/写配置文件
package jimmy; import java.io.*; import java.util.Properties; public class Program { public static v ...