「WC2013」糖果公园
「WC2013」糖果公园
传送门
树上带修莫队板子题。
看懂题意后就是板子题了。
参考代码:
#include <algorithm>
#include <cstdio>
#include <cmath>
#define rg register
#define int long long
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using std ::sort; using std ::swap;
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
}
const int _ = 100010;
int tot, head[_]; struct Edge { int ver, nxt; } edge[_ << 1];
inline void Add_edge(int u, int v) { edge[++tot] = (Edge) { v, head[u] }, head[u] = tot; }
int n, m, q, v[_], w[_], c[_];
int gap, pos[_ << 1], cnt[_], ans, res[_];
int len, vis[_], ord[_ << 1], fir[_], las[_];
int dep[_], son[_], siz[_], top[_], fa[_];
int Cnum; struct Change { int x, y; } C[_];
int Qnum; struct Query { int l, r, lca, t, id; } Q[_];
inline bool cmp(const Query& x, const Query& y)
{ return pos[x.l] ^ pos[y.l] ? pos[x.l] < pos[y.l] : (pos[x.r] ^ pos[y.r] ? pos[x.r] < pos[y.r] : x.t < y.t); }
inline void dfs(int u, int f) {
dep[u] = dep[f] + 1, siz[u] = 1, fa[u] = f;
ord[fir[u] = ++len] = u;
for (rg int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].ver; if (v == f) continue ;
dfs(v, u), siz[u] += siz[v];
if (siz[v] > siz[son[u]]) son[u] = v;
}
ord[las[u] = ++len] = u;
}
inline void dfs(int u, int f, int topf) {
top[u] = topf;
if (son[u]) dfs(son[u], u, topf);
for (rg int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].ver; if (v == f || v == son[u]) continue ;
dfs(v, u, v);
}
}
inline int LCA(int x, int y) {
int fx = top[x], fy = top[y];
while (fx != fy) {
if (dep[fx] < dep[fy]) swap(x, y), swap(fx, fy);
x = fa[fx], fx = top[x];
}
return dep[x] < dep[y] ? x : y;
}
inline void calc(int x) { vis[x] ? ans -= 1ll * w[cnt[c[x]]--] * v[c[x]] : ans += 1ll * w[++cnt[c[x]]] * v[c[x]], vis[x] ^= 1; }
inline void work(int tim) {
if (vis[C[tim].x])
ans += 1ll * w[++cnt[C[tim].y]] * v[C[tim].y] - 1ll * w[cnt[c[C[tim].x]]--] * v[c[C[tim].x]];
swap(c[C[tim].x], C[tim].y);
}
signed main() {
read(n), read(m), read(q);
for (rg int i = 1; i <= m; ++i) read(v[i]);
for (rg int i = 1; i <= n; ++i) read(w[i]);
for (rg int x, y, i = 1; i < n; ++i) read(x), read(y), Add_edge(x, y), Add_edge(y, x);
for (rg int i = 1; i <= n; ++i) read(c[i]);
dfs(1, 0), dfs(1, 0, 1), gap = (long long) pow(len, 2.0 / 3.0);
for (rg int i = 1; i <= len; ++i) pos[i] = (i - 1) / gap + 1;
for (rg int type, x, y, i = 1; i <= q; ++i) {
read(type), read(x), read(y);
if (type == 0) C[++Cnum] = (Change) { x, y };
else {
if (fir[x] > fir[y]) swap(x, y);
int lca = LCA(x, y);
if (x == lca)
Q[++Qnum] = (Query) { fir[x], fir[y], 0, Cnum, Qnum };
else
Q[++Qnum] = (Query) { las[x], fir[y], lca, Cnum, Qnum };
}
}
sort(Q + 1, Q + Qnum + 1, cmp);
for (rg int l = 1, r = 0, tim = 0, i = 1; i <= Qnum; ++i) {
while (l > Q[i].l) calc(ord[--l]);
while (l < Q[i].l) calc(ord[l++]);
while (r < Q[i].r) calc(ord[++r]);
while (r > Q[i].r) calc(ord[r--]);
while (tim < Q[i].t) work(++tim);
while (tim > Q[i].t) work(tim--);
if (Q[i].lca) calc(Q[i].lca);
res[Q[i].id] = ans;
if (Q[i].lca) calc(Q[i].lca);
}
for (rg int i = 1; i <= Qnum; ++i) printf("%lld\n", res[i]);
return 0;
}
「WC2013」糖果公园的更多相关文章
- 【BZOJ3052】【UOJ#58】【WC2013】糖果公园(树上莫队)
[BZOJ3052][UOJ#58][WC2013]糖果公园(树上莫队) 题面 UOJ 洛谷 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引 ...
- UOJ #58 【WC2013】 糖果公园
题目链接:糖果公园 听说这是一道树上莫队的入门题,于是我就去写了--顺便复习了一下莫队的各种姿势. 首先,我们要在树上使用莫队,那么就需要像序列一样给树分块.这个分块的过程就是王室联邦这道题(vfle ...
- UOJ58 【WC2013】糖果公园
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 【WC2013】糖果公园
Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园玩. 糖果公园的结构十分奇特,它由 nn 个游览点构成,每个游览 ...
- 【WC2013】 糖果公园 - 树上莫队
问题描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩.糖果公园的结构十分奇特,它由 n 个游览点构成, ...
- 【BZOJ】【3052】【WC2013】糖果公园
树分块 老早(大约一个月以前?)就听说这道神题了……orz rausen 一直拖到现在才做……发现还是不会呢= = 只好也去Orz了Hzwer和zky http://hzwer.com/5250.ht ...
- 「SCOI2011」糖果
蒟蒻又回来写题解了... 题面 幼儿园里有 N 个小朋友, lxhgww 老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红 ...
- uoj #58. 【WC2013】糖果公园(树上莫队算法+修改操作)
[题目链接] http://uoj.ac/problem/58 [题意] 有一棵树,结点有自己的颜色,若干询问:u,v路径上的获益,并提供修改颜色的操作. 其中获益定义为Vc*W1+Vc*W2+…+V ...
- uoj #58 【WC2013】糖果公园
题面:http://uoj.ac/problem/58 正解:树上带修改莫队. 首先Orz vfk大神,树上莫队的套路还是很厉害的..http://vfleaking.blog.163.com/blo ...
随机推荐
- StringBuilder与String的区别
String 在进行运算时(如赋值.拼接等)会产生一个新的实例,而 StringBuilder 则不会.所以在大量字符串拼接或频繁对某一字符串进行操作时最好使用 StringBuilder,不要使用 ...
- 矩阵快速幂 裸 hdu1575
裸题,求A^n次后的对角线数字之和 #include<cstdio> #include<algorithm> #include<string.h> using na ...
- Java:面向对象的编程语言
java是面向对象的编程语言 Object,就是指面向对象的对象,对象就是实例. 在java里,对象是类的一个具体实例.就像:人,指一个类.你.我.他.张三.李四.王五等则是一个个具体的实例,也就是j ...
- Docker - 构建一个简单的应用镜像
概述 做个简单的可用镜像 背景 之前的镜像, 都是 命令教程 类的 这次我想构建一个 可以用的 简单镜像镜像 1. 环境 os centos7 docker 18.09 docker image ja ...
- nmon+Java Nmon Analyser进行nmon监控数据可视化分析
我们知道nmon是一款轻量级的系统占用极少,监控功能非常强大支持跨平台ARM,centos,ubuntu等等系统的工具下载地:centos7 wget http://sourceforge.net/p ...
- makecert生成证书
是要命令 makecert -r -pe -n "cn=musetowc" -$ commercial -a sha1 -b 01/01/2020 -e 01/01/2050 -c ...
- C#堆和栈的入门理解
声明:以下内容从网络整理,非原创,适当待入个人理解. 解释1.栈是编译期间就分配好的内存空间,因此你的代码中必须就栈的大小有明确的定义:堆是程序运行期间动态分配的内存空间,你可以根据程序的运行情况确定 ...
- C# 程序集(Assembly)
程序集 程序集是代码进行编译是的一个逻辑单元,把相关的代码和类型进行组合,然后生成PE文件.程序集只是逻辑上的划分,一个程序集可以只由一个文件组成,也可由多个文件组成.不管是单文件程序集还是多文件程序 ...
- IIS的部署(二)------虚拟目录的使用
IIS的虚拟目录 一个站点的网页的存储位置目录是固定的,而且结构和物理保存网页的磁盘路径相同.例如:默认网页的存储位置是c:\inetpub\wwwroot,当访问localhost即访问c:\ine ...
- Mac下ssh远程无密码登录
入手Mac,对很多工具的使用都不太熟悉,这不,做web开发,登录远程服务器非常繁琐,想要去掉输入密码这个环节,找到网友的分享如下: http://www.cnblogs.com/shuaiwhu/ar ...