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 ...
随机推荐
- unity中实现静态的3D对象对其他对象的跟随
using UnityEngine; public class FollowPosition : MonoBehaviour { public Transform targetTrans; publi ...
- 笔试题目练习-python
以下内容包含笔试练习库的题目和代码,题目来自牛客网,仅供参考. # coding = utf-8 import sys def test1(): """ 题目描述:计算字 ...
- html生成缩略图来预览解决方案
html生成缩略图来预览解决方案 一.总结 一句话总结:先将html转化为canvas,然后将canvas生成图片ajax上传到服务器,就可以了 html 转化 canvas 图片 上传 html2c ...
- 配置rsync 同步数据 rpm包安装rsync及配置
[root@Hammer home]# rpm -qa |grep rsync #检查系统是否安装了rsync软件包rsync-2.6.8-3.1[root@Hammer CentOS]# rpm - ...
- 源代码方式调试Mycat
如果是第一次刚接触MyCat建议下载源码在本地通过eclipse等工具进行配置和运行,便于深入了解和调试程序运行逻辑. 1)源代码方式调试与配置 由于MyCat源代码目前主要托管在github上,大家 ...
- Vue.js 渲染函数, JSX(未掌握,未学完)
渲染函数 , JSX(没完成学习) 基础: 实例属性:vm.$slots default 属性包括了所有没有被包含在具名插槽中的节点. 渲染函数: render: function(createEle ...
- php--------ThinkPHP3.2验证码使用
Thinkphp框架验证码的路径目录:ThinkPHP -> Library -> Think -> Verify.class.php ,只要在控制器中实例化这个类就能实现验证码的功 ...
- Android发展历程
Android历代版本的命名: 我们都是知道,安卓系统的历来的名称都是很有意思的,下面无限互联Android培训的老师归纳了一下:Android在正式发行之前,最开始拥有两个内部测试版本,并且以著名的 ...
- FasfDFS intall nginx with image filter
centOS7 x64 1. install gd-devel 2. ./configure --prefix=/usr/local/nginx --with-http_image_filter_mo ...
- Deep Learning of Graph Matching 阅读笔记
Deep Learning of Graph Matching 阅读笔记 CVPR2018的一篇文章,主要提出了一种利用深度神经网络实现端到端图匹配(Graph Matching)的方法. 该篇文章理 ...