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 ...
随机推荐
- SQL Timeout超时的处理方法
第一步:修改Web.config配置文件.在数据库连接字符串中加上连接时间Connect Timeout,根据实际情况定时间. <!--连接数据库--> <connectionStr ...
- Python map/reduce
2017-07-31 18:20:59 一.map函数 map():会根据提供的函数对指定序列做映射.第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 ...
- 雷林鹏分享:Ruby 命令行选项
Ruby 命令行选项 Ruby 一般是从命令行运行,方式如下: $ ruby [ options ] [.] [ programfile ] [ arguments ... ] 解释器可以通过下列选项 ...
- 【异常】Application failed to start due to an exception org.springframework.beans.factory.BeanCurrentlyInCreationException
一. 异常信息: 2018-05-17 18:03:22.224 -DEBUG [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter ...
- Codeforces Round #364 (Div. 1) (差一个后缀自动机)
B. Connecting Universities 大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值 单独考虑每条边$(u,v)$的贡献即可, ...
- gvim配置文件
vimrc配置 source $VIMRUNTIME/vimrc_example.vim source $VIMRUNTIME/mswin.vim behave mswin "设置文件的 ...
- redhat linux 6.2 安装配置GUI
redhat6.2默认不安装GUI,启动时默认进入text模式,下面介绍下安装.配置GUI的步骤: 1.登录root 2.配置及测试yum vi /etc/yum.repos.d/rhel-sou ...
- 《程序员面试金典》习题解答(C/C++)
一.数据结构 1.数组与字符串 1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.假使不允许使用额外的数据结构,又该如何处理? /* 假设字符集为ASCII字符串,那么字符串至多有256个 ...
- office每次打开都要重新配置
office每次打开都要重新配置 1● 找到路径 C:\Program Files\Common Files\microsoft shared\OFFICE14\Office Setup Co ...
- idea开发工具安装说明
开发工具安装说明 安装JDK1.8 第一步,双击"jdk-8u45-windows-i586.exe"安装文件,进行安装,具体安装过程如下图所示: 第二步,右键我的电脑-属性- ...