[洛谷P4074][WC2013]糖果公园
题目大意:给一棵$n$个节点的树,每个点有一个值$C_i$,每次询问一条路径$x->y$,求$\sum\limits_{c}val_c\times \sum\limits_{i=1}^{cnt_c}worth_i(cnt_c=\sum\limits_{i\in(x->y)}[C_i==c])$。带修改
题解:树上带修莫队,在普通的树上莫队上加一维时间即可
卡点:$res$忘记开$long\;long$
C++ Code:
#include <cstdio>
#include <algorithm>
#define maxn 100010
#define N (maxn << 1)
#define bl(x) ((x) >> 11)
int n, m, l, r, p;
long long res;
long long ans[maxn];
int C[maxn], num[maxn];
long long W[maxn], V[maxn];
bool vis[maxn];
int Qcnt, Mcnt;
struct Query {
int l, r, tim, id, lca;
bool addlca;
inline bool operator < (const Query &rhs) const {
return (bl(l) == bl(rhs.l)) ? (bl(r) == bl(rhs.r) ? tim < rhs.tim : r < rhs.r) : l < rhs.l;
}
} q[maxn];
inline void swap(int &a, int &b) {a ^= b ^= a ^= b;}
struct Modity {
int pos, C, tim;
inline void modify() {
if (vis[pos]) {
res -= W[num[::C[pos]]--] * V[::C[pos]];
res += W[++num[C]] * V[C];
}
swap(::C[pos], C);
}
} M[maxn]; int date[N], in[maxn], out[maxn], idx;
namespace tree {
int head[maxn], cnt;
struct Edge {
int to, nxt;
} e[maxn << 1];
void add(int a, int b) {
e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
e[++cnt] = (Edge) {a, head[b]}; head[b] = cnt;
} int fa[maxn];
void dfs(int u) {
date[in[u] = ++idx] = u;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v != fa[u]) {
fa[v] = u;
dfs(v);
}
}
date[out[u] = ++idx] = u;
}
} namespace tarjan {
int head[maxn], cnt;
struct QUERY {
int v, nxt, id;
} Q[maxn << 1];
void add(int a, int b, int c) {
Q[++cnt] = (QUERY) {b, head[a], c}; head[a] = cnt;
Q[++cnt] = (QUERY) {a, head[b], c}; head[b] = cnt;
} int f[maxn];
void init(int n) {
for (int i = 1; i <= n; i++) f[i] = i;
}
int find(int x) {return (x == f[x] ? x : (f[x] = find(f[x])));}
void dfs(int u) {
for (int i = tree::head[u]; i; i = tree::e[i].nxt) {
int v = tree::e[i].to;
if (v != tree::fa[u]) {
dfs(v);
f[v] = find(u);
}
}
for (int i = head[u]; i; i = Q[i].nxt) q[Q[i].id].lca = find(Q[i].v);
}
} #define ONLINE_JUDGE
#include <cctype>
namespace R {
int x;
#ifdef ONLINE_JUDGE
char *ch, op[1 << 26];
inline void init() {
fread(ch = op, 1, 1 << 26, stdin);
}
inline int read() {
while (isspace(*ch)) ch++;
for (x = *ch & 15, ch++; isdigit(*ch); ch++) x = x * 10 + (*ch & 15);
return x;
}
#else
char ch;
inline int read() {
ch = getchar();
while (isspace(ch)) ch = getchar();
for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15);
return x;
}
#endif
} int O;
int main() {
#ifdef ONLINE_JUDGE
R::init();
#endif
tarjan::init(n = R::read()); m = R::read(), O = R::read();
for (int i = 1; i <= m; i++) V[i] = R::read();
for (int i = 1; i <= n; i++) W[i] = R::read();
for (int i = 1; i < n; i++) tree::add(R::read(), R::read());
tree::dfs(1);
for (int i = 1; i <= n; i++) C[i] = R::read();
for (int i = 1; i <= O; i++) {
if (R::read()) {
q[++Qcnt].tim = i;
tarjan::add(q[Qcnt].l = R::read(), q[Qcnt].r = R::read(), q[Qcnt].id = Qcnt);
} else M[++Mcnt].pos = R::read(), M[Mcnt].C = R::read(), M[Mcnt].tim = i;
}
tarjan::dfs(1);
for (int i = 1; i <= Qcnt; i++) {
int &l = q[i].l, &r = q[i].r;
if (in[l] > in[r]) swap(l, r);
l = (q[i].addlca = (q[i].lca != l)) ? out[l] : in[l];
r = in[r];
}
std::sort(q + 1, q + Qcnt + 1);
l = 1, r = 0, p = 0;
for (int i = 1; i <= Qcnt; i++) {
while (l > q[i].l) (vis[date[--l]] ^= 1) ? (res += W[++num[C[date[l]]]] * V[C[date[l]]]) : (res -= W[num[C[date[l]]]--] * V[C[date[l]]]);
while (r < q[i].r) (vis[date[++r]] ^= 1) ? (res += W[++num[C[date[r]]]] * V[C[date[r]]]) : (res -= W[num[C[date[r]]]--] * V[C[date[r]]]);
while (l < q[i].l) (vis[date[l]] ^= 1) ? (res += W[++num[C[date[l]]]] * V[C[date[l++]]]) : (res -= W[num[C[date[l]]]--] * V[C[date[l++]]]);
while (r > q[i].r) (vis[date[r]] ^= 1) ? (res += W[++num[C[date[r]]]] * V[C[date[r--]]]) : (res -= W[num[C[date[r]]]--] * V[C[date[r--]]]);
while (p < Mcnt && M[p + 1].tim < q[i].tim) M[++p].modify();
while (p && M[p].tim > q[i].tim) M[p--].modify();
ans[q[i].id] = res + (q[i].addlca ? W[num[C[q[i].lca]] + 1] * V[C[q[i].lca]] : 0);
}
for (int i = 1; i <= Qcnt; i++) printf("%lld\n", ans[i]);
return 0;
}
[洛谷P4074][WC2013]糖果公园的更多相关文章
- 洛谷 P4074 [WC2013]糖果公园 解题报告
P4074 [WC2013]糖果公园 糖果公园 树上待修莫队 注意一个思想,dfn序处理链的方法,必须可以根据类似异或的东西,然后根据lca分两种情况讨论 注意细节 Code: #include &l ...
- Machine Learning Codeforces - 940F(带修莫队) && 洛谷P4074 [WC2013]糖果公园
以下内容未验证,有错请指正... 设块大小为T,则块数为$\frac{n}{T}$ 将询问分为$(\frac{n}{T})^2$块(按照左端点所在块和右端点所在块分块),同块内按时间从小到大依次处理 ...
- 洛谷P4074 [WC2013]糖果公园(莫队)
传送门 总算会树形莫队了…… 上次听说树形莫队是给树分块,实在看不懂.然后用括号序列的方法做总算能弄明白了 先说一下什么是括号序列,就是在$dfs$的时候,进入的时候记录一下,出去的时候也记录一下 拿 ...
- P4074 [WC2013]糖果公园 树上莫队带修改
题目链接 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩. 糖果公园的结构十分奇特,它由 nn 个游览点构 ...
- luogu P4074 [WC2013]糖果公园
传送门 这种题显然要用树上莫队 何为树上莫队?就是在树上跑莫队算法就是先把树分块,然后把询问离线,按照左端点所在块为第一关键字,右端点所在块为第二关键字,时间戳(如果有修改操作)为第三关键字排序,然后 ...
- BZOJ 3052/Luogu P4074 [wc2013]糖果公园 (树上带修莫队)
题面 中文题面,难得解释了 BZOJ传送门 Luogu传送门 分析 树上带修莫队板子题... 开始没给分块大小赋初值T了好一会... CODE #include <bits/stdc++.h&g ...
- P4074 [WC2013]糖果公园
思路 带修莫队+树上莫队 注意代码细节即可,答案的维护非常简单 蒟蒻的大常数代码 #include <cstdio> #include <algorithm> #include ...
- LUOGU P4074 [WC2013]糖果公园 (树上带修莫队)
传送门 解题思路 树上带修莫队,搞了两天..终于开O2+卡常大法贴边过了...bzoj上跑了183s..其实就是把树上莫队和带修莫队结合到一起,首先求出括号序,就是进一次出一次那种的,然后如果求两个点 ...
- bzoj 3052: [wc2013]糖果公园 带修改莫队
3052: [wc2013]糖果公园 Time Limit: 250 Sec Memory Limit: 512 MBSubmit: 506 Solved: 189[Submit][Status] ...
随机推荐
- iOS圆角view的Swift实现(利用Core Graphics绘制)
iOS圆角view的Swift实现(利用Core Graphics绘制) 因为app的列表用用到了圆形图片的头像,所以去探究并思考了一下这个问题.首先这个问题有两个方向的解决方案: 把图片弄成圆形的. ...
- ES6的数组方法之Array.from
首先说说什么是数组:数组在类型划分上归为Object,属于比较特殊的对象,数组的索引值类似于对象的key值. 数组的几个注意点: 1.数组的长度是可读属性,不可更改,数组的长度根据索引最大值. 2.数 ...
- 线段树的应用xx中学模拟lites
跟昨天那个自己写的,没有按照模板来的一看风格就不相类似,今天模拟赛的时候就是用的我的那个自己YY的代码,才拿了10分.个人认为关键的问题应该在于对于数据的处理太过繁琐了,所以回来之后,就拿了大佬的程序 ...
- go get超时解决办法
go get gopkg.in/yaml.v2超时,发现被墙了,解决办法如下: 1.安装golang.org/x/net $ mkdir -p $GOPATH/src/golang.org/x/ $ ...
- PHP中json_encode后,在json字符串中依然显示中文的解决方案
<?php header("Content-Type:text/html;charset=utf-8;"); $arr = array ('Version_code'=> ...
- Hbase学习指南
本篇Hbase组件基于CDH5进行安装,安装过程:https://www.cnblogs.com/dmjx/p/10037066.html Hbase简介 HBase是一个高可靠.高性能.面向列.可伸 ...
- 一行代码搞定checkbox全选和全不选
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- 以CentOS为操作系统的vps或服务器安装lnmp运行环境的方法
安装步骤: 1.使用putty或类似的SSH工具登陆VPS或服务器: 登陆后运行:screen -S lnmp 如果提示screen: command not found 命令不存在可以执行:yum ...
- 部分和问题 南阳acm1058(递归+dfs)
部分和问题 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 给定整数a1.a2........an,判断是否可以从中选出若干数,使它们的和恰好为K. 输入 首先, ...
- [BZOJ1060][ZJOI2007]时态同步(树形DP)
[我是传送门] 因为边权只能增加,那么设f[u]为u子树上从i出发到达某个叶节点的最大路径, 显然Ans应该增加f[u]-f[v]-e[i].w Code #include <cstdio> ...