「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」糖果公园的更多相关文章

  1. 【BZOJ3052】【UOJ#58】【WC2013】糖果公园(树上莫队)

    [BZOJ3052][UOJ#58][WC2013]糖果公园(树上莫队) 题面 UOJ 洛谷 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引 ...

  2. UOJ #58 【WC2013】 糖果公园

    题目链接:糖果公园 听说这是一道树上莫队的入门题,于是我就去写了--顺便复习了一下莫队的各种姿势. 首先,我们要在树上使用莫队,那么就需要像序列一样给树分块.这个分块的过程就是王室联邦这道题(vfle ...

  3. UOJ58 【WC2013】糖果公园

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. 【WC2013】糖果公园

    Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园玩. 糖果公园的结构十分奇特,它由 nn 个游览点构成,每个游览 ...

  5. 【WC2013】 糖果公园 - 树上莫队

    问题描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩.糖果公园的结构十分奇特,它由 n 个游览点构成, ...

  6. 【BZOJ】【3052】【WC2013】糖果公园

    树分块 老早(大约一个月以前?)就听说这道神题了……orz rausen 一直拖到现在才做……发现还是不会呢= = 只好也去Orz了Hzwer和zky http://hzwer.com/5250.ht ...

  7. 「SCOI2011」糖果

    蒟蒻又回来写题解了... 题面 幼儿园里有 N 个小朋友, lxhgww 老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红 ...

  8. uoj #58. 【WC2013】糖果公园(树上莫队算法+修改操作)

    [题目链接] http://uoj.ac/problem/58 [题意] 有一棵树,结点有自己的颜色,若干询问:u,v路径上的获益,并提供修改颜色的操作. 其中获益定义为Vc*W1+Vc*W2+…+V ...

  9. uoj #58 【WC2013】糖果公园

    题面:http://uoj.ac/problem/58 正解:树上带修改莫队. 首先Orz vfk大神,树上莫队的套路还是很厉害的..http://vfleaking.blog.163.com/blo ...

随机推荐

  1. mysql(4):性能分析和性能优化

    性能分析 慢查询日志分析 ①查询慢查询日志的状态 show global variables like '%slow_query_log%'; ②开启慢查询日志(当mysql重启时会重置) set g ...

  2. Java-POJ1014-Dividing

    多重背包问题的特点是物品数量可以大于1但是有限制.状态定义与01背包一致. 多重背包的解法有多种,复杂度也各不相同. 对于物品数Ci较大的数据,可以采取二进制数进行优化(就是这样,别问就是baidu! ...

  3. 如何做好 Android 端音视频测试?

    在用户眼中,优秀的音视频产品应该具有清晰.低延时.流畅.秒开.抗丢包.高音效等特征.为了满足用户以上要求,网易云信的工程师通过自建源站,在SDK端为了适应网络优化进行QoS优化,对视频编码器进行优化, ...

  4. java_爬虫_获取经过js渲染后的网页源码

    md 弄了一天了……(这个月不会在摸爬虫了,浪费生命) 进入正题: 起初是想写一个爬虫来爬一个网站的视频,但是怎么爬取都爬取不到,分析了下源代码之后,发现源代码中并没有视频的dom 但是在浏览器检查元 ...

  5. JS 数组克隆方法总结

    ES5 方法总结 1.slice let arr = [2,4,434,43] let arr1= arr.slice() arr[0] = 'a' console.log(arr,arr1) // ...

  6. 每天进步一点点------CRC码的FPGA实现

    一.CRC码的FPGA实现之一CRC的原理 实验目的 学习用FPGA设计一个数据通信中常用的数据检错模块——循环冗余检验CRC模块,熟悉理解CRC的检错原理. 实验原理 循环冗余检验(CRC)算法原理 ...

  7. c语言中void *和NULL

    1.  int *str = NULL; #define NULL    ((void *)0) 空指针实质上是有指向的指针,但它指向的地址是很小的地址,约定俗成为地址0.   2. void *vo ...

  8. qt5.9.0 msvc2015优雅的崩溃:dumpfile

    交给客户的软件奔溃了怎么办? 我们不能再客户电脑上安装vs,也不想傻傻的用log来猜测出错的地方. 利用Dbghelp可以解决这一问题. 首先是vs生成release版本的时候需要同时生成pdb文件, ...

  9. 大数据-hdfs技术

    hadoop 理论基础:GFS----HDFS:MapReduce---MapReduce:BigTable----HBase 项目网址:http://hadoop.apache.org/ 下载路径: ...

  10. numpy (1.16.4) has the default value of allow_pickle as False.

    My version of numpy (1.16.4) has the default value of allow_pickle as False. numpy版本是1.16.4,allow_pi ...