因为有$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】旅行的更多相关文章

  1. [BZOJ 3530][Sdoi 2014]数数

    阿拉~好像最近总是做到 AC 自动机的题目呢喵~ 题目的算法似乎马上就能猜到的样子…… AC 自动机 + 数位 dp 先暴力转移出 f[i][j] :表示从 AC 自动机上第 j 号节点走 i 步且不 ...

  2. BZOJ 3533 sdoi 2014 向量集

    设(x,y)为Q的查询点,分类讨论如下:1.y>0:  最大化a*x+b*y,维护一个上凸壳三分即可 2.y<0:最大化a*x+b*y  维护一个下凸壳三分即可 我们考虑对时间建出一棵线段 ...

  3. bzoj 3531 旅行

    动态开点线段树+树链剖分  对于每一种宗教信仰都开一颗线段树 空间: QlogN 即每一次修改都只会改变logN 个点 时间 O(QlogN)  naive题  边没有开两倍  QAQ bzoj 35 ...

  4. [BZOJ 3531] [Sdoi2014] 旅行 【离线+LCT】

    题目链接:BZOJ - 3531 题目分析 题目询问一条路径上的信息时,每次询问有某种特定的文化的点. 每个点的文化就相当于一种颜色,每次询问一条路径上某种颜色的点的信息. 可以使用离线算法, 类似于 ...

  5. BZOJ 3531(树链剖分+线段树)

    Problem 旅行 (BZOJ 3531) 题目大意 给定一颗树,树上的每个点有两个权值(x,y). 要求维护4种操作: 操作1:更改某个点的权值x. 操作2:更改某个点的权值y. 操作3:求a-- ...

  6. [BZOJ 1879][SDOI 2009]Bill的挑战 题解(状压DP)

    [BZOJ 1879][SDOI 2009]Bill的挑战 Description Solution 1.考虑状压的方式. 方案1:如果我们把每一个字符串压起来,用一个布尔数组表示与每一个字母的匹配关 ...

  7. [BZOJ 3992] [SDOI 2015] 序列统计(DP+原根+NTT)

    [BZOJ 3992] [SDOI 2015] 序列统计(DP+原根+NTT) 题面 小C有一个集合S,里面的元素都是小于质数M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数 ...

  8. [BZOJ 3123] [SDOI 2013]森林(可持久化线段树+并查集+启发式合并)

    [BZOJ 3123] [SDOI 2013]森林(可持久化线段树+启发式合并) 题面 给出一个n个节点m条边的森林,每个节点都有一个权值.有两种操作: Q x y k查询点x到点y路径上所有的权值中 ...

  9. BZOJ 3531: [Sdoi2014]旅行 [树链剖分]

    3531: [Sdoi2014]旅行 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1685  Solved: 751[Submit][Status] ...

随机推荐

  1. java编程思想读书笔记2

    六:访问权限控制 1.java中的4种访问制权限: (1).public:最大访问控制权限,对所有的类都可见. (2).protect:同一包可见,不在同一个包的所有子类也可见. (3).defaul ...

  2. AC日记——向量点积计算 openjudge 1.6 09

    09:向量点积计算 总时间限制:  1000ms 内存限制:  65536kB 描述 在线性代数.计算几何中,向量点积是一种十分重要的运算. 给定两个n维向量a=(a1,a2,...,an)和b=(b ...

  3. AC日记——判断字符串是否为回文 openjudge 1.7 33

    33:判断字符串是否为回文 总时间限制:  1000ms 内存限制:  65536kB 描述 输入一个字符串,输出该字符串是否回文.回文是指顺读和倒读都一样的字符串. 输入 输入为一行字符串(字符串中 ...

  4. 精选30个优秀的CSS技术和实例

    精选30个优秀的CSS技术和实例   投递人 墙头草 发布于 2008-12-06 20:57 评论(97) 有17487人阅读 原文链接 [收藏] « » 今天,我为大家收集精选了30个使用纯CSS ...

  5. 第一天接触Orchard

    第一天接触Orchard 为什么要研究Orchard 呢? 楼主是因为要研究下最新的ASP.NET技术, Orchard 用的人多,历史也比较久,算是老牌了 再就是中文资料比较多,Orchard 有中 ...

  6. GIT安装和使用

    GIT 首先登陆github账号   在本地创建一个文件夹   点击文件夹,右键,选择Git create repository here 操作之后,会生成一个.git文件(这个文件为隐藏文件) 在此 ...

  7. Makefile总述②文件命名、包含其他文件makefile、变量、重建重载、解析

    Makefile的内容 在一个完整的 Makefile 中,包含了 5 个东西:显式规则.隐含规则.变量定义.指示符和注释. 显式规则:它描述了在何种情况下如何更新一个或者多个被称为目标的文件( Ma ...

  8. oracl函数

    一:大小写函数 1:lower()全部小写 select lower('HEHE') lowerwords from dual 2:upper()全部大写 3:initcap()首字母大写 4:con ...

  9. 隐写技巧——利用JPEG文件格式隐藏payload

    0x00 前言 继续对图片隐写技巧的学习,这次是对JPEG文件格式的学习和理解.同PNG文件的格式对比,JPEG文件相对简单,读取其中隐藏payload的方式大同小异,两者区别在于文件格式不同,可供利 ...

  10. poj[1185]炮兵阵地

    Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用&quo ...