BZOJ3052: [wc2013]糖果公园【树上带修莫队】
Description
.jpg)
Input
.jpg)
Output
.jpg)
Sample Input
.jpg)
Sample Input
Sample Output
84
131
27
84
HINT
.jpg)
.jpg)
思路
非常模板的树上带修莫队
真的很裸
直接暴力维护就可以了
注意一下询问的第二关键字是第二个节点所在块,第三关键字是时间,不然jmr说会出锅
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int n, m, q, col[N], c[N];
int dfn[N], dfn_ind = 0;
int dep[N], fa[N][20];
int tot = 0, head[N];
ll v[N], w[N], nowans = 0;
int top = 0, st[N << 1], bel[N];
int siz_block = 2000, cnt_block = 0;
int num[N], vis[N];
struct Query {
int u, v, id;
ll ans;
} Q[N];
bool cmp1(const Query &a, const Query &b) {
if (bel[a.u] != bel[b.u]) return bel[a.u] < bel[b.u];
if (bel[a.v] != bel[b.v]) return bel[a.v] < bel[b.v];
return a.id < b.id;
}
bool cmp2(const Query &a, const Query &b) {
return a.id < b.id;
}
struct Modify {
int pos, bef, aft, id;
} M[N << 1];
struct Edge {
int v, nxt;
} E[N << 1];
void addedge(int u, int v) {
E[++tot] = (Edge) {v, head[u]};
head[u] = tot;
}
int dfs(int u) {
int siz = 0;
dfn[u] = ++dfn_ind;
for (int i = 1; i <= 18; i++)
fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int i = head[u]; i; i = E[i].nxt) {
int v = E[i].v;
if (v == fa[u][0]) continue;
fa[v][0] = u;
dep[v] = dep[u] + 1;
siz += dfs(v);
if (siz >= siz_block) {
++cnt_block;
while (siz) {
bel[st[top--]] = cnt_block;
--siz;
}
}
}
st[++top] = u;
return siz + 1;
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
int delta = dep[x] - dep[y];
for (int i = 0; i <= 18; i++)
if ((delta >> i) & 1) x = fa[x][i];
if (x == y) return x;
for (int i = 18; i >= 0; i--) {
if (fa[x][i] != fa[y][i]) {
x = fa[x][i];
y = fa[y][i];
}
}
return fa[x][0];
}
void reverse(int x) {
if (vis[x]) {
nowans -= w[num[col[x]]] * v[col[x]];
num[col[x]]--;
} else {
num[col[x]]++;
nowans += w[num[col[x]]] * v[col[x]];
}
vis[x] ^= 1;
}
void solve(int x, int y) {
while (x != y) {
if (dep[x] > dep[y]) {
reverse(x);
x = fa[x][0];
} else {
reverse(y);
y = fa[y][0];
}
}
}
int main() {
#ifdef dream_maker
freopen("input.txt", "r", stdin);
#endif
scanf("%d %d %d", &n, &m, &q);
for (int i = 1; i <= m; i++) scanf("%lld", &v[i]);
for (int i = 1; i <= n; i++) scanf("%lld", &w[i]);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d %d", &u, &v);
addedge(u, v);
addedge(v, u);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &col[i]);
c[i] = col[i];
}
int cntq = 0, cntm = 0;
for (int i = 1; i <= q; i++) {
int typ; scanf("%d", &typ);
if (typ) {
++cntq;
Q[cntq].id = i;
scanf("%d %d", &Q[cntq].u, &Q[cntq].v);
} else {
++cntm;
M[cntm].id = i;
int x, y;
scanf("%d %d", &x, &y);
M[cntm].pos = x;
M[cntm].bef = c[x];
c[x] = y;
M[cntm].aft = c[x];
}
}
dfs(1);
++cnt_block;
while (top) {
bel[st[top--]] = cnt_block;
}
sort(Q + 1, Q + cntq + 1, cmp1);
int cur = 0;
for (int i = 1; i <= cntq; i++) {
while (cur < cntm && M[cur + 1].id <= Q[i].id) {
++cur;
if (vis[M[cur].pos]) {
nowans -= w[num[col[M[cur].pos]]] * v[col[M[cur].pos]];
num[col[M[cur].pos]]--;
}
col[M[cur].pos] = M[cur].aft;
if (vis[M[cur].pos]) {
num[col[M[cur].pos]]++;
nowans += w[num[col[M[cur].pos]]] * v[col[M[cur].pos]];
}
}
while (cur >= 1 && M[cur].id >= Q[i].id) {
if (vis[M[cur].pos]) {
nowans -= w[num[col[M[cur].pos]]] * v[col[M[cur].pos]];
num[col[M[cur].pos]]--;
}
col[M[cur].pos] = M[cur].bef;
if (vis[M[cur].pos]) {
num[col[M[cur].pos]]++;
nowans += w[num[col[M[cur].pos]]] * v[col[M[cur].pos]];
}
--cur;
}
if (i == 1) {
solve(Q[i].u, Q[i].v);
} else {
solve(Q[i].u, Q[i - 1].u);
solve(Q[i].v, Q[i - 1].v);
}
int t = lca(Q[i].u, Q[i].v);
reverse(t);
Q[i].ans = nowans;
reverse(t);
}
sort(Q + 1, Q + cntq + 1, cmp2);
for (int i = 1; i <= cntq; i++)
printf("%lld\n", Q[i].ans);
return 0;
}
BZOJ3052: [wc2013]糖果公园【树上带修莫队】的更多相关文章
- 【BZOJ-3052】糖果公园 树上带修莫队算法
3052: [wc2013]糖果公园 Time Limit: 200 Sec Memory Limit: 512 MBSubmit: 883 Solved: 419[Submit][Status] ...
- luogu4074 [WC2013]糖果公园(树上带修莫队)
link 题目大意:给一个树,树上每个点都有一种颜色,每个颜色都有一个收益 每次修改一个点上的颜色 或询问一条链上所有颜色第i次遇到颜色j可以获得w[i]*v[j]的价值,求链上价值和 题解:树上带修 ...
- LUOGU P4074 [WC2013]糖果公园 (树上带修莫队)
传送门 解题思路 树上带修莫队,搞了两天..终于开O2+卡常大法贴边过了...bzoj上跑了183s..其实就是把树上莫队和带修莫队结合到一起,首先求出括号序,就是进一次出一次那种的,然后如果求两个点 ...
- [WC2013][luogu4074] 糖果公园 [树上带修改莫队]
题面: 传送门 思路: 一道实现起来细节比较恶心的题目 但是其实就是一个裸的树上带修改莫队 好像树上莫队也出不了什么结合题目,不像序列莫队天天结合AC自动机.后缀数组...... 莫队学习请戳这里:莫 ...
- BZOJ 3052/Luogu P4074 [wc2013]糖果公园 (树上带修莫队)
题面 中文题面,难得解释了 BZOJ传送门 Luogu传送门 分析 树上带修莫队板子题... 开始没给分块大小赋初值T了好一会... CODE #include <bits/stdc++.h&g ...
- BZOJ 4129 Haruna’s Breakfast ( 树上带修莫队 )
题面 求树上某路径上最小的没出现过的权值,有单点修改 添加链接描述 分析 树上带修莫队板题,问题是怎么求最小的没出现过的权值. 因为只有nnn个点,所以没出现过的最小值一定在[0,n][0,n][0, ...
- bzoj4129 Haruna’s Breakfast 树上带修莫队+分块
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4129 题解 考虑没有修改的序列上的版本应该怎么做: 弱化的题目应该是这样的: 给定一个序列,每 ...
- BZOJ 3052 树上带修莫队
思路: 就是把带修莫队移到了树上 块的大小开到(n^2/3)/2 比较好- 这是一个卡OJ好题 //By SiriusRen #include <cmath> #include <c ...
- BZOJ3052:[WC2013]糖果公园(树上莫队)
Description Input Output Sample Input 4 3 51 9 27 6 5 12 33 13 41 2 3 21 1 21 4 20 2 11 1 21 4 2 Sam ...
随机推荐
- Python 错误与异常
2017-08-01 13:40:17 在程序运行过程中,总会遇到各种各样的错误. 有的错误是程序编写有问题造成的,比如本来应该输出整数结果输出了字符串,这种错误我们通常称之为bug,bug是必须修复 ...
- Freemarker生成HTML静态页面
这段时间的工作是做一个网址导航的项目,面向用户的就是一个首页,于是就想到了使用freemarker这个模板引擎来对首页静态化. 之前是用jsp实现,为了避免用户每次打开页面都查询一次数据库,所以使用了 ...
- ln软连接
ln软连接 ln -s 源目录/文件 目标目录/文件 例如,有个应用 /var/www/html/webapp,下面有个logs日志文件夹,想吧 webapp/logs日志文件夹链到/home ...
- English trip -- 国际音标表
26个字母音标表 A a [ei] B b [bi:] C c [si:] D d [di:] E e [i:] F f [ef] G g [dʒi:] H h [eit∫] I i [ai] J j ...
- 转:Too many systemd: Created slice !
OS: centos-release-7-4.1708 /va/log/message 大量这种提示信息: resolvent: Here is how I got rid of these: vi ...
- English trip -- VC(情景课)2 D Reading
Xu言: 业精于勤,荒于嬉:行成于思,毁于随 Before you read 阅读准备 Talk about the picture, what do you see?看图说话,你看到了什么? Lis ...
- codeforces 536a//Tavas and Karafs// Codeforces Round #299(Div. 1)
题意:一个等差数列,首项为a,公差为b,无限长.操作cz是区间里选择最多m个不同的非0元素减1,最多操作t次,现给出区间左端ll,在t次操作能使区间全为0的情况下,问右端最大为多少. 这么一个简单题吞 ...
- Confluence 6 为用户管理连接 Confluence 到 Crowd
你可以连接你的 Confluence 应用程序到 Atlassian Crowd 或 a Jira (5.3 及后续版本)来管理你的用户和用户组以及针对他们的授权. Atlassian Crowd 是 ...
- Party CodeForces - 906C (状压)
大意: 给定n(n<=22)个人, m个关系谁跟谁是朋友, 朋友关系是双向的, 每次操作可以选择一个人, 使他的朋友互相成为朋友, 求最少多少次操作可以使所有人互相认识 这个题挺巧妙的了, 关键 ...
- Error: [ng:areq] Argument 'LoginCtrl' is not a function, got undefined