浅谈莫队:https://www.cnblogs.com/AKMer/p/10374756.html

题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=3052

把问题搬到欧拉序上来就是裸的带修改莫队了。

每次加减某种颜色直接加减个数系数乘美味度即可。

时间复杂度:\(O(n^{\frac{5}{3}})\)

空间复杂度:\(O(n)\)

代码如下:

#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll; const int maxn=1e5+5; ll ans;bool vis[maxn];
int dep[maxn],f[maxn][18];
int v[maxn],w[maxn],lst[maxn],c[maxn];
int now[maxn],pre[maxn<<1],son[maxn<<1];
int n,m,q,block,tot,cnt1,cnt2,cnt,nowl,nowr,T;
int dfn[maxn<<1],L[maxn],R[maxn],sum[maxn],bel[maxn<<1]; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} void add(int a,int b) {
pre[++tot]=now[a];
now[a]=tot,son[tot]=b;
} void dfs(int fa,int u) {
dfn[++cnt]=u,L[u]=cnt;
dep[u]=dep[fa]+1,f[u][0]=fa;
for(int i=1;i<18;i++)
f[u][i]=f[f[u][i-1]][i-1];
for(int p=now[u],v=son[p];p;p=pre[p],v=son[p])
if(v!=fa)dfs(u,v);
dfn[++cnt]=u,R[u]=cnt;
} struct query {
ll res;
int l,r,id,tim; query() {} query(int _l,int _r,int _id,int _tim) {
l=_l,r=_r,id=_id,tim=_tim;
} bool operator<(const query &a)const {
if(bel[l]==bel[a.l]&&bel[r]==bel[a.r])return tim<a.tim;
if(bel[l]==bel[a.l])return bel[l]&1?bel[r]<bel[a.r]:bel[r]>bel[a.r];
return bel[l]<bel[a.l];
}
}Q[maxn]; struct modify {
int node,id,lst; modify() {} modify(int _node,int _id,int _lst) {
node=_node,id=_id,lst=_lst;
}
}C[maxn]; int lca(int u,int v) {
if(dep[u]<dep[v])swap(u,v);
for(int i=17;~i;i--)
if(dep[f[u][i]]>=dep[v])
u=f[u][i];
if(u==v)return u;
for(int i=17;~i;i--)
if(f[u][i]!=f[v][i])
u=f[u][i],v=f[v][i];
return f[u][0];
} bool cmp(query a,query b) {
return a.id<b.id;
} void change(int u,int id) {
if(vis[u]) {
ans+=1ll*w[++sum[id]]*v[id];
ans-=1ll*w[sum[c[u]]--]*v[c[u]];
}
c[u]=id;
} void change(int u) {
if(!vis[u])ans+=1ll*w[++sum[c[u]]]*v[c[u]];
else ans-=1ll*w[sum[c[u]]--]*v[c[u]];
vis[u]^=1;
} int main() {
n=read(),m=read(),q=read();
block=pow(n<<1,2.0/3);
for(int i=1;i<=m;i++)v[i]=read();
for(int i=1;i<=n;i++)w[i]=read();
for(int i=1;i<n;i++) {
int a=read(),b=read();
add(a,b),add(b,a);
}
dfs(0,1);
for(int i=1;i<=n;i++)lst[i]=c[i]=read();
for(int i=1;i<=n<<1;i++)bel[i]=(i-1)/block+1;
for(int i=1;i<=q;i++) {
int opt=read();
if(opt) {
int u=read(),v=read();
if(L[u]>L[v])swap(u,v);
Q[++cnt1]=query(lca(u,v)==u?L[u]:R[u],L[v],i,cnt2);
}
else {
int node=read(),id=read();
C[++cnt2]=modify(node,id,lst[node]);
lst[node]=id;
}
}
sort(Q+1,Q+cnt1+1);
nowl=1,nowr=0,T=0;
for(int i=1;i<=cnt1;i++) {
while(T<Q[i].tim)T++,change(C[T].node,C[T].id);
while(T>Q[i].tim)change(C[T].node,C[T].lst),T--;
while(nowl<Q[i].l)change(dfn[nowl++]);
while(nowl>Q[i].l)change(dfn[--nowl]);
while(nowr<Q[i].r)change(dfn[++nowr]);
while(nowr>Q[i].r)change(dfn[nowr--]);
if(L[dfn[Q[i].l]]==Q[i].l)Q[i].res=ans;
else {
int tmp=lca(dfn[Q[i].l],dfn[Q[i].r]);
change(tmp);Q[i].res=ans;change(tmp);
}
}
sort(Q+1,Q+cnt1+1,cmp);
for(int i=1;i<=cnt1;i++)
printf("%lld\n",Q[i].res);
return 0;
}

BZOJ3052:[WC2013]糖果公园的更多相关文章

  1. BZOJ3052:[WC2013]糖果公园(树上莫队)

    Description Input Output Sample Input 4 3 51 9 27 6 5 12 33 13 41 2 3 21 1 21 4 20 2 11 1 21 4 2 Sam ...

  2. bzoj3052: [wc2013]糖果公园

    又是一代神题. uoj测速rank10,bzoj测速rank26(截止当前2016.5.30 12:58) 带修改的树上莫队. 修改很少,块的大小随便定都能A 然而我一开始把开3次根写成了pow(bl ...

  3. BZOJ3052: [wc2013]糖果公园【树上带修莫队】

    Description Input Output Sample Input Sample Input Sample Output 84 131 27 84 HINT 思路 非常模板的树上带修莫队 真的 ...

  4. BZOJ3052 [wc2013] 糖果公园 【树上莫队】

    树上莫队和普通的序列莫队很像,我们把树进行dfs,然后存一个长度为2n的括号序列,就是一个点进去当作左括号,出来当作右括号,然后如果访问从u到v路径,我们可以转化成括号序列的区间,记录x进去的时候编号 ...

  5. 【树上莫队】【带修莫队】bzoj3052 [wc2013]糖果公园

    #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...

  6. 【BZOJ3052】[wc2013]糖果公园 带修改的树上莫队

    [BZOJ3052][wc2013]糖果公园 Description Input Output Sample Input Sample Input Sample Output 84 131 27 84 ...

  7. [BZOJ3052][UOJ#58][WC2013]糖果公园

    [BZOJ3052][UOJ#58][WC2013]糖果公园 试题描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来 ...

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

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

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

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

  10. 洛谷 P4074 [WC2013]糖果公园 解题报告

    P4074 [WC2013]糖果公园 糖果公园 树上待修莫队 注意一个思想,dfn序处理链的方法,必须可以根据类似异或的东西,然后根据lca分两种情况讨论 注意细节 Code: #include &l ...

随机推荐

  1. 【TopCoder】SRM152 DIV2总结

    为什么平常刷的时候感觉还不错,比赛的时候只能做出来一道题=.= 250分题:大水题,根据题目规则把一个字符串翻译成数字,直接代码:GitHub 我是通过遍历一个个数出来的,看到大神的解法是把字符用‘- ...

  2. Windos Server Tomcat 双开配置

    Tomcat 双开配置 tomcat_1   server.mxl文件 # 修改端口 <Connector port=" protocol="HTTP/1.1" c ...

  3. R和Python小数的保留

    R: 1.保留几位有效数字: signif(x,digits) 2.保留几位小数: round(x,digits) Python: 1.“%.2f”%a

  4. accept= 'image/*'反映缓慢

    input[type='file']的accept属性用来指定上传文件的MIME类型. 将其设为accept= 'image/*',顾名思义,过滤掉所有非图片文件, 但在实际操作中,发现有时会出现响应 ...

  5. nginx日志中$request_body 十六进制字符(\\x22) 引号问题处理记录

    在使用nginx记录访问日志时,发现在含有 request_body 的 PUT , POST 请求时,日志中会含有 x22 x9B x5C x09 x08 字符,不利于阅读和处理. 具体 支持 re ...

  6. Pandas基础用法-数据处理【全】-转

    完整资料:[数据挖掘入门介绍] (https://github.com/YouChouNoBB/data-mining-introduction) # coding=utf-8 # @author: ...

  7. mysql中的一些操作

    查询mysql中事务提交的情况: show variables like '%commit%'; 可以查看当前autocommit值 在mysql数据库中它的默认值是"on"代表自 ...

  8. Linux下Python科学计算包numpy和SciPy的安装

      系统环境: OS:RedHat5 Python版本:Python2.7.3 gcc版本:4.1.2 各个安装包版本: scipy-0.11.0 numpy-1.6.2 nose-1.2.1 lap ...

  9. 【P2964】硬币的游戏(DP+前缀和)

    一道DP,思维难度真是不小. 首先对于这个题的数据,我们可以发现差不多可以支持n^2logn,但是貌似也不会有这种复杂度的线性DP(至少这个题看上去不是这样).所以我们考虑N^2做法.因为求得是价值和 ...

  10. contenteditable支持度

    contenteditable attribute (basic support) - Working Draft Global user stats*: Support: 86.71% Partia ...