浅谈莫队: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. 虚构 css 父级选择器

    能 CSS 解决的绝不用 JS,这句话又一次故作装逼地说出来还是挺爽的... 比如下拉列表,能用 CSS 的 :focus 就不用 JS 的 .on("focus blur") 能 ...

  2. Ajax跨域请求action方法,无法传递及接收cookie信息(应用于系统登录认证及退出)解决方案

    最近的项目中涉及到了应用ajax请求后台系统登录,身份认证失败,经过不断的调试终于找到解决方案. 应用场景: 项目测试环境:前端应用HTML,js,jQuery ajax请求,部署在Apache服务器 ...

  3. 摊铺机基本参数介绍(三一重工SSP220C-5)

    三一重工SSP220C-5稳定土摊铺机参数 SSP系列稳定土摊铺机SSP220C-5 动力强劲162kw 动力充分满足摊铺机各种工况下动力需求 高效任何工况,确保摊铺能力大于900t/h,行业内绝无仅 ...

  4. INSPIRED启示录 读书笔记 - 第22章 原型测试

    物色测试者 1.如果你已经拥有一批特约用户,可以邀请他们参加测试 2.如果是企业级产品,同类产品的展销会是寻找目标用户的好去处 3.可以在分类信息网站上发布广告,征集测试者.征集要求可以写得笼统些,不 ...

  5. Android深度探索(卷1)HAL与驱动开发 虚拟环境的安装

    前言: 最近在看<Android深度探索(卷1)HAL与驱动开发>安装随书带的虚拟环境浪费了很多时间,说是虚拟环境的安装倒不如说是虚拟环境的导入,其实没什么技术含量,也没有什么复杂的,只是 ...

  6. linux基础(10)-导航菜单

    导航菜单实战 例:编写一个shell脚本,包含多个菜单,其中需要一个退出选项:可单选也可多选:根据序号选择后,显示所选菜单名称. #!/bin/bash ####################### ...

  7. Avoid RegionServer Hotspotting Despite Sequential Keys

    n HBase world, RegionServer hotspotting is a common problem.  We can describe this problem with a si ...

  8. UOJ222 【NOI2016】区间

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

  9. python之Django rest_framework总结

    一.rest api    a.api就是接口         如: - http://www.oldboyedu.com/get_user/                - http://www. ...

  10. flask--Wtform

    一.Wtform WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 安装:    pip3 install wtform 用途:  1. 用户登录注册       ...