Description

Input

Output

Sample Input

Sample Input

Sample Output

84

131

27

84

HINT


思路

非常模板的树上带修莫队

真的很裸

直接暴力维护就可以了

注意一下询问的第二关键字是第二个节点所在块,第三关键字是时间,不然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]糖果公园【树上带修莫队】的更多相关文章

  1. 【BZOJ-3052】糖果公园 树上带修莫队算法

    3052: [wc2013]糖果公园 Time Limit: 200 Sec  Memory Limit: 512 MBSubmit: 883  Solved: 419[Submit][Status] ...

  2. luogu4074 [WC2013]糖果公园(树上带修莫队)

    link 题目大意:给一个树,树上每个点都有一种颜色,每个颜色都有一个收益 每次修改一个点上的颜色 或询问一条链上所有颜色第i次遇到颜色j可以获得w[i]*v[j]的价值,求链上价值和 题解:树上带修 ...

  3. LUOGU P4074 [WC2013]糖果公园 (树上带修莫队)

    传送门 解题思路 树上带修莫队,搞了两天..终于开O2+卡常大法贴边过了...bzoj上跑了183s..其实就是把树上莫队和带修莫队结合到一起,首先求出括号序,就是进一次出一次那种的,然后如果求两个点 ...

  4. [WC2013][luogu4074] 糖果公园 [树上带修改莫队]

    题面: 传送门 思路: 一道实现起来细节比较恶心的题目 但是其实就是一个裸的树上带修改莫队 好像树上莫队也出不了什么结合题目,不像序列莫队天天结合AC自动机.后缀数组...... 莫队学习请戳这里:莫 ...

  5. BZOJ 3052/Luogu P4074 [wc2013]糖果公园 (树上带修莫队)

    题面 中文题面,难得解释了 BZOJ传送门 Luogu传送门 分析 树上带修莫队板子题... 开始没给分块大小赋初值T了好一会... CODE #include <bits/stdc++.h&g ...

  6. BZOJ 4129 Haruna’s Breakfast ( 树上带修莫队 )

    题面 求树上某路径上最小的没出现过的权值,有单点修改 添加链接描述 分析 树上带修莫队板题,问题是怎么求最小的没出现过的权值. 因为只有nnn个点,所以没出现过的最小值一定在[0,n][0,n][0, ...

  7. bzoj4129 Haruna’s Breakfast 树上带修莫队+分块

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4129 题解 考虑没有修改的序列上的版本应该怎么做: 弱化的题目应该是这样的: 给定一个序列,每 ...

  8. BZOJ 3052 树上带修莫队

    思路: 就是把带修莫队移到了树上 块的大小开到(n^2/3)/2 比较好- 这是一个卡OJ好题 //By SiriusRen #include <cmath> #include <c ...

  9. 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 ...

随机推荐

  1. Android JNI(一)——NDK与JNI基础

    本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...

  2. robot切换窗口

    在用robot做自动化测试的时候,经常遇到新开窗口,需要切换窗口的情况. 两种方式来解决: (1)用robot的关键字来解决 实现窗口切换的功能 @{handlelist} List Windows ...

  3. poj 1182 (带权并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 71361   Accepted: 21131 Description ...

  4. 37. Sudoku Solver *HARD*

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  5. win7 安装express

    1.以管理员身份打开cmd(避免出错) npm install -g express-generator@4 Start the server: npm start 链接,github官网说明 htt ...

  6. spring cloud学习(六)Spring Cloud Config

    Spring Cloud Config 参考个人项目 参考个人项目 : (希望大家能给个star~) https://github.com/FunriLy/springcloud-study/tree ...

  7. Sql Server中集合的操作(并集、差集、交集)学习

    首先我们做一下测试数据 1.创建测试数据 --创建人员表1-- create table Person1 ( Uid ,) primary key, Name ) not null ) --创建人员表 ...

  8. 读书笔记 C# 控制台应用程序之Main方法浅析

    Main方法是C#控制台应用程序和Windows窗体应用程序的入口点.Main方法可以有形参,也可以没有,可以有返回值(int整型),也可以没有.如下定义: 无返回值.无形参的格式: static v ...

  9. DevExpress WPF入门指南:DXWindow应用

    [DevExpress v17.2 版本更新公开课]点击免费报名 DevExpress WPF Window control有一点非常棒,就是可以和其他视觉主题保持统一性.DXWindow class ...

  10. cocos2d-x中用到的一些宏

    最近我们的cocos2d-x游戏项目已经进入了正式开发的阶段了,几个dev都辛苦码代码.cocos2d-x还是一套比较方便的api的,什么action啊.director啊.ccpoint啊都蛮便捷的 ...