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 ...
随机推荐
- Linux环境下 RabbitMQ 的下载与安装
0 环境 CentOS7 RabbitMQ 3.6.5 erlang 18.3 socat rabbitmq是使用erlang语言编写的,所以需要先安装erlang,其次rabbitmq安装依赖于so ...
- Android JNI(一)——NDK与JNI基础
本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...
- m_Orchestrate learning system---三十三、公共变量多弄成全局变量
m_Orchestrate learning system---三十三.公共变量多弄成全局变量 一.总结 一句话总结:比如班级id,小组id,这样省事,而且减少数据库的访问,加快访问速度,而且节约代码 ...
- Tips for Sync Vimtex and PDF
vimtex synctex: \lv. skim synctex: to display the TeX source line corresponding to a point in the PD ...
- 76. Minimum Window Substring *HARD*
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- kill word out e ef en em
1● e 2● ef 出,出来 3● en 4● em 使~进入状态,包围,进入~之中
- vs2015 企业版、专业版如何破解(秘钥)
安装完vs2015 企业版后,在菜单帮助---注册产品,显示产品试用期30天,怎么破解呢? 一.破解秘钥 企业版 HM6NR-QXX7C-DFW2Y-8B82K-WTYJV 专业版 HMG ...
- learning shell args handing key=value example (2)
Shell args handing key=value example [Purpose] Learning how to handing ker=value args [Eevi ...
- 申请和使用github共享代码
1.申请github帐号 https://github.com/join?source=header-home 2.创建项目 2.1 或者: 2.2 输入信息 2.3创建成功,地址及基本命令提示 3. ...
- hibernate缓存清除(转)
文章有点杂,这不是原文,谢谢贡献者 http://www.360doc.com/content/16/0413/16/32415095_550307388.shtml 一.hibernate一级缓存( ...