BZOJ3052:[WC2013]糖果公园
浅谈莫队: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]糖果公园的更多相关文章
- 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 ...
- bzoj3052: [wc2013]糖果公园
又是一代神题. uoj测速rank10,bzoj测速rank26(截止当前2016.5.30 12:58) 带修改的树上莫队. 修改很少,块的大小随便定都能A 然而我一开始把开3次根写成了pow(bl ...
- BZOJ3052: [wc2013]糖果公园【树上带修莫队】
Description Input Output Sample Input Sample Input Sample Output 84 131 27 84 HINT 思路 非常模板的树上带修莫队 真的 ...
- BZOJ3052 [wc2013] 糖果公园 【树上莫队】
树上莫队和普通的序列莫队很像,我们把树进行dfs,然后存一个长度为2n的括号序列,就是一个点进去当作左括号,出来当作右括号,然后如果访问从u到v路径,我们可以转化成括号序列的区间,记录x进去的时候编号 ...
- 【树上莫队】【带修莫队】bzoj3052 [wc2013]糖果公园
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...
- 【BZOJ3052】[wc2013]糖果公园 带修改的树上莫队
[BZOJ3052][wc2013]糖果公园 Description Input Output Sample Input Sample Input Sample Output 84 131 27 84 ...
- [BZOJ3052][UOJ#58][WC2013]糖果公园
[BZOJ3052][UOJ#58][WC2013]糖果公园 试题描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来 ...
- 【BZOJ-3052】糖果公园 树上带修莫队算法
3052: [wc2013]糖果公园 Time Limit: 200 Sec Memory Limit: 512 MBSubmit: 883 Solved: 419[Submit][Status] ...
- bzoj 3052: [wc2013]糖果公园 带修改莫队
3052: [wc2013]糖果公园 Time Limit: 250 Sec Memory Limit: 512 MBSubmit: 506 Solved: 189[Submit][Status] ...
- 洛谷 P4074 [WC2013]糖果公园 解题报告
P4074 [WC2013]糖果公园 糖果公园 树上待修莫队 注意一个思想,dfn序处理链的方法,必须可以根据类似异或的东西,然后根据lca分两种情况讨论 注意细节 Code: #include &l ...
随机推荐
- perspective 能玩点什么
今天看又在看张鑫旭的博客,本来是在玩 transform:Matrix() 的,有讲到单个变化的矩阵设置,但多个变化的就不是那么回事了. 不过这都不是事啦,人生嘛,显然总会有些难关不是轻易能过去的,反 ...
- pearson相关分析在R中的实现
三个相关性函数: cor():R自带的,输入数据可以是vector,matrix,data.frame,输出两两的相关系数R值 cor.test():R自带的,输入数据只能是两个vector,输出两个 ...
- centos 下安装python3.6.2
具体详情: http://www.cnblogs.com/vurtne-lu/p/7068521.html
- ⭐内核MKDEV(MAJOR, MINOR)宏
版本:linux-2.6.24.4宏: MKDEV(MAJOR, MINOR); 说明: 获取设备在设备表中的位置. MAJOR 主设备号 MINOR 次设 ...
- INSPIRED启示录 读书笔记 - 第41章 产品经理的反省清单
十大问题 1.产品能吸引目标消费者的关注吗? 2.产品的设计是否人性化,是否易于操作? 3.产品能在竞争中取胜吗?即使是面对未来风云变化的市场,依旧有取胜的把握吗? 4.我了解目标用户吗?产品(不是理 ...
- 机器学习性能指标之ROC和AUC理解与曲线绘制
一. ROC曲线 1.roc曲线:接收者操作特征(receiveroperating characteristic),roc曲线上每个点反映着对同一信号刺激的感受性. 横轴:负正类率(false po ...
- SpringBoot Maven打包项目JAR/WAR
安装Maven 1. 登录 http://maven.apache.org/download.cgi 2. 下载 maven 压缩包 3. 解压apache-maven-3.6.0-bin.tar.g ...
- Windows Server 2008 架设 Web 服务器教程(图文详解)
Windows Server 2008 架设 Web 服务器教程(图文详解) 一.安装 IIS 7.0 : 虽然 Windows Server 2008 内置了I IS 7.0,但是默认情况下并没有安 ...
- Classloader机制
1.概述? 类加载器:负责.class文件加载到内存中,并为之生成对应的Class对象,也就是字节码对象.这样就可以使用这个类中的成员变量和方法了.而被加载到内存中的class文件就会变成class对 ...
- 使用easyui的form提交表单,在IE下出现类似附件下载时提示是否保存的现象
之前开发时遇到的一个问题,使用easyui的form提交表单,在Chrome下时没问题的,但是在IE下出现类似附件下载时提示是否保存的现象. 这里记录一下如何解决的.其实这个现象不光是easyui的f ...