「WC2013」糖果公园

传送门

树上带修莫队板子题。

看懂题意后就是板子题了。

参考代码:

  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <cmath>
  4. #define rg register
  5. #define int long long
  6. #define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
  7. using std ::sort; using std ::swap;
  8. template < class T > inline void read(T& s) {
  9. s = 0; int f = 0; char c = getchar();
  10. while ('0' > c || c > '9') f |= c == '-', c = getchar();
  11. while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
  12. s = f ? -s : s;
  13. }
  14. const int _ = 100010;
  15. int tot, head[_]; struct Edge { int ver, nxt; } edge[_ << 1];
  16. inline void Add_edge(int u, int v) { edge[++tot] = (Edge) { v, head[u] }, head[u] = tot; }
  17. int n, m, q, v[_], w[_], c[_];
  18. int gap, pos[_ << 1], cnt[_], ans, res[_];
  19. int len, vis[_], ord[_ << 1], fir[_], las[_];
  20. int dep[_], son[_], siz[_], top[_], fa[_];
  21. int Cnum; struct Change { int x, y; } C[_];
  22. int Qnum; struct Query { int l, r, lca, t, id; } Q[_];
  23. inline bool cmp(const Query& x, const Query& y)
  24. { 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); }
  25. inline void dfs(int u, int f) {
  26. dep[u] = dep[f] + 1, siz[u] = 1, fa[u] = f;
  27. ord[fir[u] = ++len] = u;
  28. for (rg int i = head[u]; i; i = edge[i].nxt) {
  29. int v = edge[i].ver; if (v == f) continue ;
  30. dfs(v, u), siz[u] += siz[v];
  31. if (siz[v] > siz[son[u]]) son[u] = v;
  32. }
  33. ord[las[u] = ++len] = u;
  34. }
  35. inline void dfs(int u, int f, int topf) {
  36. top[u] = topf;
  37. if (son[u]) dfs(son[u], u, topf);
  38. for (rg int i = head[u]; i; i = edge[i].nxt) {
  39. int v = edge[i].ver; if (v == f || v == son[u]) continue ;
  40. dfs(v, u, v);
  41. }
  42. }
  43. inline int LCA(int x, int y) {
  44. int fx = top[x], fy = top[y];
  45. while (fx != fy) {
  46. if (dep[fx] < dep[fy]) swap(x, y), swap(fx, fy);
  47. x = fa[fx], fx = top[x];
  48. }
  49. return dep[x] < dep[y] ? x : y;
  50. }
  51. 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; }
  52. inline void work(int tim) {
  53. if (vis[C[tim].x])
  54. ans += 1ll * w[++cnt[C[tim].y]] * v[C[tim].y] - 1ll * w[cnt[c[C[tim].x]]--] * v[c[C[tim].x]];
  55. swap(c[C[tim].x], C[tim].y);
  56. }
  57. signed main() {
  58. read(n), read(m), read(q);
  59. for (rg int i = 1; i <= m; ++i) read(v[i]);
  60. for (rg int i = 1; i <= n; ++i) read(w[i]);
  61. for (rg int x, y, i = 1; i < n; ++i) read(x), read(y), Add_edge(x, y), Add_edge(y, x);
  62. for (rg int i = 1; i <= n; ++i) read(c[i]);
  63. dfs(1, 0), dfs(1, 0, 1), gap = (long long) pow(len, 2.0 / 3.0);
  64. for (rg int i = 1; i <= len; ++i) pos[i] = (i - 1) / gap + 1;
  65. for (rg int type, x, y, i = 1; i <= q; ++i) {
  66. read(type), read(x), read(y);
  67. if (type == 0) C[++Cnum] = (Change) { x, y };
  68. else {
  69. if (fir[x] > fir[y]) swap(x, y);
  70. int lca = LCA(x, y);
  71. if (x == lca)
  72. Q[++Qnum] = (Query) { fir[x], fir[y], 0, Cnum, Qnum };
  73. else
  74. Q[++Qnum] = (Query) { las[x], fir[y], lca, Cnum, Qnum };
  75. }
  76. }
  77. sort(Q + 1, Q + Qnum + 1, cmp);
  78. for (rg int l = 1, r = 0, tim = 0, i = 1; i <= Qnum; ++i) {
  79. while (l > Q[i].l) calc(ord[--l]);
  80. while (l < Q[i].l) calc(ord[l++]);
  81. while (r < Q[i].r) calc(ord[++r]);
  82. while (r > Q[i].r) calc(ord[r--]);
  83. while (tim < Q[i].t) work(++tim);
  84. while (tim > Q[i].t) work(tim--);
  85. if (Q[i].lca) calc(Q[i].lca);
  86. res[Q[i].id] = ans;
  87. if (Q[i].lca) calc(Q[i].lca);
  88. }
  89. for (rg int i = 1; i <= Qnum; ++i) printf("%lld\n", res[i]);
  90. return 0;
  91. }

「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. NetMQ用作IPC的实例

    发送端/接收端 using System; using System.Threading; using NetMQ; using NetMQ.Sockets; namespace NetMQIPCSe ...

  2. PHP+jPaginate插件制作无刷新分页实例

    jPaginate是一款动感滚动分页插件,它的表现形式是像分页的按钮一样,有意思的是这些按钮却可以左右滚动,可以通过单击或 鼠标滑向点两侧的小箭头来控制按钮的左右滚动. 读取第一页数据: <di ...

  3. bzoj 4827: [HNOI2017]礼物 (FFT)

    一道FFT 然而据说暴力可以水70分 然而我省选的时候看到了直接吓傻了  连暴力都没打 太弱了啊QAQ emmmm 详细的拆开就看其他题解吧233 最后那一步卷积其实我一直没明白 后来画画图终于懂了 ...

  4. MySQL学习(五)事务隔离

    文章部分图片和总结来自参考资料,侵删 概述 MySQL的可重复读隔离级别,事务T启动的时候会创建一个视图read-view,之后事务T执行期间,即使有其他事务修改了数据,事务T看到的仍然跟在启动时看到 ...

  5. 【Python】 基础语法

    Python 语言与 Perl,C 和 Java 等语言有许多相似之处.但是,也存在一些差异. 第一个 Python 程序  欢迎,入坑! Python 标识符 在 Python 里,标识符由字母.数 ...

  6. 出现 HTTP Status 500 - Servlet.init() for servlet springmvc threw exception 异常

    出现这种异常在网上搜了搜 ,大多数都是说jdk和tomcat版本的问题:而我前几天都是运行得好好的,今天就编写了代码一运行项目发现报了这个错误.后台仔细看了看错误信息.结果是在你的项目中有相同的req ...

  7. centos6.5下安装mysql数据库

    centos6.5下安装mysql数据库 1.安装mysql数据库:yum install mysql-server 2.临时启动数据库:service mysqld start 3.开机启动数据库: ...

  8. AcWing 852. spfa判断负环 边权可能为负数。

    #include <cstring> #include <iostream> #include <algorithm> #include <queue> ...

  9. honoka和格点三角形(牛客寒假训练营day1)

    可以把面积为1的好三角形分成两类分开统计:两条边和两个坐标轴平行:只有一条边和某个坐标轴平行. 对于第一种情况,一定是1*2或者2*1的形式,一个1*2的矩形中含有4个不同的三角形.总数是4*((n- ...

  10. 旋转坐标+前缀和(zqu 25001)

    本题题意:在一个矩阵中,去随机一点,设定一个步数K,求出从这个点可以走到的范围的和,求最大值 思路:这个范围的和是一个菱形,我们把他旋转45°,然后成为一个正放的矩阵,求出二维前缀和 然后用前缀和的性 ...