对树的dfs序分块,打开了新世界的大门233

第一关键字是l所在的块,第二关键字是r所在的块,第三关键字是时间,分完块后暴力莫队即可

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 100003;
void read(int &k) {
k = 0; int fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = (k << 1) + (k << 3) + c - '0';
k = k * fh;
} bool vis[N];
ll A[N], ans = 0;
struct TIME {int x, y, la;} Time[N];
struct edge {int nxt, to;} E[N << 1];
struct node {int l, r, lca, id, tim;} Q[N];
int cal[N], n, m, q, V[N], W[N], f[N][17], deep[N], bel[N << 1];
int point[N], color[N], colorchange[N], pos[N << 1], L[N], R[N], cnt = 0; void ins(int x, int y) {E[++cnt].nxt = point[x]; E[cnt].to = y; point[x] = cnt;}
void _(int x, int fa) {
pos[L[x] = ++cnt] = x;
for(int i = 1; i <= 16; ++i) {f[x][i] = f[f[x][i - 1]][i - 1]; if (f[x][i] == 0) break;}
for(int tmp = point[x]; tmp; tmp = E[tmp].nxt)
if (E[tmp].to != fa)
{deep[E[tmp].to] = deep[x] + 1; f[E[tmp].to][0] = x; _(E[tmp].to, x);}
pos[R[x] = ++cnt] = x;
}
int LCA(int x, int y) {
if (deep[x] < deep[y]) swap(x, y);
int d = deep[x] - deep[y];
for(int i = 0; i <= 16; ++i) if (d & (1 << i)) x = f[x][i];
if (x == y) return x;
for(int i = 16; i >= 0; --i) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
void update(int x) {
if (vis[x]) {ans -= 1ll * V[color[x]] * W[cal[color[x]]]; --cal[color[x]];}
else {++cal[color[x]]; ans += 1ll * V[color[x]] * W[cal[color[x]]];}
vis[x] = !vis[x];
}
void change(int a, int b) {
if (vis[a]) {update(a); color[a] = b; update(a);}
else color[a] = b;
}
void Timechange(int &a, int b) {
while (a < b) {++a; change(Time[a].x, Time[a].y);}
while (a > b) {change(Time[a].x, Time[a].la); --a;}
}
bool cmp(node A, node B) {return bel[A.l] == bel[B.l] ? (bel[A.r] == bel[B.r] ? A.tim < B.tim : A.r < B.r) : A.l < B.l;} int main() {
read(n); read(m); read(q);
for(int i = 1; i <= m; ++i) read(V[i]);
for(int i = 1; i <= n; ++i) read(W[i]);
int op, u, v, timecount = 0, tmp = 0, lca;
for(int i = 1; i < n; ++i) {
read(u); read(v);
ins(u, v); ins(v, u);
}
for(int i = 1; i <= n; ++i) read(color[i]), colorchange[i] = color[i]; cnt = 0;
_(1, 0); for(int i = 1; i <= q; ++i) {
read(op); read(u); read(v);
if (op == 0) {Time[++timecount] = (TIME) {u, v, colorchange[u]}; colorchange[u] = v;}
else {
if (L[u] > L[v]) swap(u, v);
lca = LCA(u, v);
if (lca == u) Q[++tmp] = (node) {L[u], L[v], 0, tmp, timecount};
else Q[++tmp] = (node) {R[u], L[v], lca, tmp, timecount};
}
} int nn = n << 1, sq = pow(nn, 2.0 / 3) * 0.5, sign = 0; cnt = 1;
for(int i = 1; i <= nn; ++i) {
bel[i] = sign;
++cnt; if (cnt > sq) {cnt = 1; ++sign;}
} sort(Q + 1, Q + tmp + 1, cmp); int l = 1, r = 0, now = 0, tol, tor;
for(int i = 1; i <= tmp; ++i) {
tol = Q[i].l; tor = Q[i].r;
Timechange(now, Q[i].tim);
while (l < tol) update(pos[l++]);
while (l > tol) update(pos[--l]);
while (r < tor) update(pos[++r]);
while (r > tor) update(pos[r--]);
if (Q[i].lca) update(Q[i].lca);
A[Q[i].id] = ans;
if (Q[i].lca) update(Q[i].lca);
} for(int i = 1; i <= tmp; ++i) printf("%lld\n", A[i]); return 0;
}

dfs序分块战术核导弹速度超快~

【BZOJ 3052】【WC 2013】糖果公园的更多相关文章

  1. [WC 2013]糖果公园

    Description 题库链接 给你一棵 \(n\) 个节点,有 \(m\) 种颜色的树.每个节点上有一个颜色.定义一条树上路径的价值为 \[\sum_c V_c(\sum_{i=1}^{tim_c ...

  2. 【BZOJ】3052: [wc2013]糖果公园 树分块+带修改莫队算法

    [题目]#58. [WC2013]糖果公园 [题意]给定n个点的树,m种糖果,每个点有糖果ci.给定n个数wi和m个数vi,第i颗糖果第j次品尝的价值是v(i)*w(j).q次询问一条链上每个点价值的 ...

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

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

  4. bzoj 3052: [wc2013]糖果公园 带修改莫队

    3052: [wc2013]糖果公园 Time Limit: 250 Sec  Memory Limit: 512 MBSubmit: 506  Solved: 189[Submit][Status] ...

  5. [BZOJ 3052] [wc2013] 糖果公园 【树上莫队】

    题目链接:BZOJ - 3052 题目分析 这道题就是非常经典的树上莫队了,并且是带修改的莫队. 带修改的莫队:将询问按照 左端点所在的块编号为第一关键字,右端点所在的块为第二关键字,位于第几次修改之 ...

  6. bzoj 3052 糖果公园

    Written with StackEdit. Description \(Candyland\) 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的 ...

  7. 【BZOJ-3052】糖果公园 树上带修莫队算法

    3052: [wc2013]糖果公园 Time Limit: 200 Sec  Memory Limit: 512 MBSubmit: 883  Solved: 419[Submit][Status] ...

  8. 【WC2013】糖果公园

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

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

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

  10. UOJ58 【WC2013】糖果公园

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

随机推荐

  1. TopCoder SRM 642 Div.2 1000 --二分+BFS

    题意: 给你一张图,N个点(0~N-1),m条边,国王要从0到N-1,国王携带一个值,当走到一条边权大于此值的边时,要么不走,要么提升该边的边权,提升k个单位花费k^2块钱,国王就带了B块钱,问能携带 ...

  2. 关于AngularJs中的路由学习总结

    AngularJs中的路由,应用比较广泛,主要是允许我们通过不同的url访问不同的内容,可实现多视图的单页web应用.下面看看具体怎么使用. 关于路由  通常我们的URL形式为http://jtjds ...

  3. java 22 - 21 多线程之多线程的代码实现方式3

    JDK5新增了一个Executors工厂类来产生线程池,有如下几个方法 A.public static ExecutorService newCachedThreadPool() B.public s ...

  4. 转: Git远程操作详解 (阮一峰)

    说明: 通过这个说明终于把远程操作给搞明白了. http://www.ruanyifeng.com/blog/2014/06/git_remote.html

  5. 转:windows下命令行工具

    转自: http://www.cnblogs.com/haochuang/p/5593411.html Windows下CMD不好用,远没有Linux,或者一些SSH工具用起来方便.其实Windows ...

  6. Eclipse中启用Oracle jdbc logging

    根据自己用的JRE版本, jre1.5选择ojdbc5_g.jar, jre6选择ojdbc6_g.jar, 只有带_g的dirver才输出debug信息. maven 的配置信息: <depe ...

  7. PAT 1007. 素数对猜想 (20)

    让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数."素数对猜想"认为"存在无穷多对相邻且 ...

  8. Log4net在类库中的用法

    app.config应当放置在解决方案的根目录下.具体流程如下: 第一步:应该下载log4net.dll并引入到你的项目中,下载见附件 第二步:需要配置相关的配置文件App.config或Web.co ...

  9. HP “云图”GPU虚拟化工作站解决方案

    HP PCS ”云图”GPU虚拟化工作站解决方案 ——将图形计算从桌面移到数据中心 惠普云图形GPU虚拟化桌面系统是以用户为中心的私有云服务.除了保留了传统桌面虚拟化方案以集中设备为中心统一管理等优点 ...

  10. hashset hastable dictionary concurrentdictionary区别

    1.HashTable 哈希表(HashTable)表示键/值对的集合.在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现 ...