对树的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. VIJOS P1037搭建双塔[DP]

    描述 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念“9?11”事件,Mr. F决定自己用水晶来搭建一座双塔. Mr. F有N块水晶,每块 ...

  2. 第9章 用内核对象进行线程同步(1)_事件对象(Event)

    9.1 等待函数 (1)WaitForSingleObject(hObject,dwMilliseonds); ①dwMilliseconds为INFINITE时表示无限等待 ②dwMilliseco ...

  3. 关于数组的map、reduce、filter

    map:map()方法定义在Array中,传入自己的参数,就得到一个新的Array作为结果 var aqiData = [ ["北京", 90], ["上海", ...

  4. java 23 - 2 设计模式之单例模式

    单例模式:保证类在内存中只有一个对象. 如何保证类在内存中只有一个对象呢?  A:把构造方法私有  B:在成员位置自己创建一个对象  C:通过一个公共的方法提供访问 单例模式之饿汉式: (一进来就造对 ...

  5. uva216 Getting in Line

    Computer networking requires that the computers in the network be linked. This problem considers a \ ...

  6. iOS本地化

    本地化与相机中显示英文  工程PROJECT -> info ->Localizations 添加相应的国际化语言  一.当你发现相机中显示英文,可以通过它设置 添加一项“Localize ...

  7. css3结构性伪类选择器

  8. domReady方法(dom加载完成执行回调)

    var domReady = function( fn ) { var isReady = false, ready = function(){ if(!isReady){ typeof fn === ...

  9. C# winform生成天气预报(转)

    原文地址 http://www.cnblogs.com/ChowYy/p/3382216.html?utm_source=tuicool&utm_medium=referral 项目需要,然后 ...

  10. python处理经过gzip压缩的网页内容

    Python在进行网页抓取时,有时会获取到经过gzip压缩后的数据(体积小,传输快),导致无法阅读和使用. 如图所示,为http原始报文.可以看到,header区域的“Content-Encoding ...