P4074 [WC2013]糖果公园
思路
带修莫队+树上莫队
注意代码细节即可,答案的维护非常简单
蒟蒻的大常数代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cmath>
using namespace std;
int vx[100100*2],fir[100100],nxt[100100*2],cnt;
void addedge(int ui,int vi){
++cnt;
vx[cnt]=vi;
nxt[cnt]=fir[ui];
fir[ui]=cnt;
}
int color[100100],w_p[100100],b_p[100100],b[100100],tx,n,m,q,v[100100],w[100100],U,V,T,in_ans[100100],belong[100100],times[100100];
long long sum,ans[100100];
struct Query{
int t,u,v,id;
bool operator < (const Query &b) const{
return (belong[u]==belong[b.u])?((belong[v]==belong[b.v])?t<b.t:belong[v]<belong[b.v]):belong[u]<belong[b.u];
}
}Q[100100];
struct Modi{
int x,before,after;
}A[100100];
int jump[100100][20],dep[100100],sz,block_cnt,Acnt,Qcnt;
stack<int> S;
void T_modi(int t,int opt){
if(opt==1){
if(in_ans[A[t].x]){
sum-=1LL*v[A[t].before]*w[color[A[t].before]];
color[A[t].before]--;
color[A[t].after]++;
sum+=1LL*v[A[t].after]*w[color[A[t].after]];
}
w_p[A[t].x]=A[t].after;
}
else{
if(in_ans[A[t].x]){
sum-=1LL*v[A[t].after]*w[color[A[t].after]];
color[A[t].after]--;
color[A[t].before]++;
sum+=1LL*v[A[t].before]*w[color[A[t].before]];
}
w_p[A[t].x]=A[t].before;
}
T+=opt;
}
void modi_point(int x){
if(in_ans[x]){
sum-=1LL*v[w_p[x]]*w[color[w_p[x]]];
color[w_p[x]]--;
}
else{
color[w_p[x]]++;
sum+=1LL*v[w_p[x]]*w[color[w_p[x]]];
}
in_ans[x]^=1;
}
void modi_path(int x,int y){
if(dep[x]<dep[y])
swap(x,y);
while(dep[x]>dep[y]){
modi_point(x);
x=jump[x][0];
}
while(x!=y){
modi_point(x);
modi_point(y);
x=jump[x][0];
y=jump[y][0];
}
}
void dfs(int u,int f){
jump[u][0]=f;
for(int i=1;i<20;i++)
jump[u][i]=jump[jump[u][i-1]][i-1];
dep[u]=dep[f]+1;
int t=S.size();
for(int i=fir[u];i;i=nxt[i]){
if(vx[i]==f)
continue;
dfs(vx[i],u);
if(S.size()-t>=sz){
block_cnt++;
while(S.size()>t){
belong[S.top()]=block_cnt;
S.pop();
}
}
}
S.push(u);
}
void init(void){
sz=pow(n,0.6666666);
dfs(1,0);
while(!S.empty()){
belong[S.top()]=block_cnt;
S.pop();
}
}
int lca(int x,int y){
if(dep[x]<dep[y])
swap(x,y);
for(int i=19;i>=0;i--)
if(dep[jump[x][i]]>=dep[y])
x=jump[x][i];
if(x==y)
return x;
for(int i=19;i>=0;i--)
if(jump[x][i]!=jump[y][i])
x=jump[x][i],y=jump[y][i];
return jump[x][0];
}
int main(){
scanf("%d %d %d",&n,&m,&q);
for(int i=1;i<=m;i++)
scanf("%d",&v[i]);
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
for(int i=1;i<n;i++){
int a,b;
scanf("%d %d",&a,&b);
addedge(a,b);
addedge(b,a);
}
for(int i=1;i<=n;i++)
scanf("%d",&w_p[i]),b_p[i]=b[i]=w_p[i];
for(int i=1;i<=q;i++){
int opt,x,y;
scanf("%d %d %d",&opt,&x,&y);
if(opt==0){
A[++Acnt].x=x;
A[Acnt].before=b_p[x];
A[Acnt].after=y;
b_p[x]=y;
}
else{
Q[++Qcnt].t=Acnt;
Q[Qcnt].u=x;
Q[Qcnt].v=y;
Q[Qcnt].id=Qcnt;
}
}
init();
sort(Q+1,Q+Qcnt+1);
U=V=1;
T=0;
for(int i=1;i<=Qcnt;i++){
modi_path(U,Q[i].u);
modi_path(V,Q[i].v);
U=Q[i].u;
V=Q[i].v;
while(T<Q[i].t)
T_modi(T+1,1);
while(T>Q[i].t)
T_modi(T,-1);
int Lca=lca(U,V);
modi_point(Lca);
ans[Q[i].id]=sum;
modi_point(Lca);
}
for(int i=1;i<=Qcnt;i++)
printf("%lld\n",ans[i]);
return 0;
}
P4074 [WC2013]糖果公园的更多相关文章
- 洛谷 P4074 [WC2013]糖果公园 解题报告
P4074 [WC2013]糖果公园 糖果公园 树上待修莫队 注意一个思想,dfn序处理链的方法,必须可以根据类似异或的东西,然后根据lca分两种情况讨论 注意细节 Code: #include &l ...
- P4074 [WC2013]糖果公园 树上莫队带修改
题目链接 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩. 糖果公园的结构十分奇特,它由 nn 个游览点构 ...
- Machine Learning Codeforces - 940F(带修莫队) && 洛谷P4074 [WC2013]糖果公园
以下内容未验证,有错请指正... 设块大小为T,则块数为$\frac{n}{T}$ 将询问分为$(\frac{n}{T})^2$块(按照左端点所在块和右端点所在块分块),同块内按时间从小到大依次处理 ...
- luogu P4074 [WC2013]糖果公园
传送门 这种题显然要用树上莫队 何为树上莫队?就是在树上跑莫队算法就是先把树分块,然后把询问离线,按照左端点所在块为第一关键字,右端点所在块为第二关键字,时间戳(如果有修改操作)为第三关键字排序,然后 ...
- 洛谷P4074 [WC2013]糖果公园(莫队)
传送门 总算会树形莫队了…… 上次听说树形莫队是给树分块,实在看不懂.然后用括号序列的方法做总算能弄明白了 先说一下什么是括号序列,就是在$dfs$的时候,进入的时候记录一下,出去的时候也记录一下 拿 ...
- [洛谷P4074][WC2013]糖果公园
题目大意:给一棵$n$个节点的树,每个点有一个值$C_i$,每次询问一条路径$x->y$,求$\sum\limits_{c}val_c\times \sum\limits_{i=1}^{cnt_ ...
- BZOJ 3052/Luogu P4074 [wc2013]糖果公园 (树上带修莫队)
题面 中文题面,难得解释了 BZOJ传送门 Luogu传送门 分析 树上带修莫队板子题... 开始没给分块大小赋初值T了好一会... CODE #include <bits/stdc++.h&g ...
- LUOGU P4074 [WC2013]糖果公园 (树上带修莫队)
传送门 解题思路 树上带修莫队,搞了两天..终于开O2+卡常大法贴边过了...bzoj上跑了183s..其实就是把树上莫队和带修莫队结合到一起,首先求出括号序,就是进一次出一次那种的,然后如果求两个点 ...
- bzoj 3052: [wc2013]糖果公园 带修改莫队
3052: [wc2013]糖果公园 Time Limit: 250 Sec Memory Limit: 512 MBSubmit: 506 Solved: 189[Submit][Status] ...
随机推荐
- 将raspberry 3B+的apt替换为国内源
前段时间买了一块树莓派,想着自己拿来玩一下下(没什么钱烧更好的硬件,只能玩这个了,好在还够玩).于是就折腾起来,装了raspberry的系统. 这时候,因为默认apt是国外的源,在GFW这种神奇东西的 ...
- [Cometoj#4 C]方块切割_质因数分解_贪心
方块切割 题目链接:https://cometoj.com/contest/39/problem/C?problem_id=1583 数据范围:略. 题解: 首先,如果我们知道了多少道在行上,多少刀在 ...
- 实时监控服务器某个端口状态TCPing
在给客户做运维服务期间,发现了一个奇怪的现象:备份系统(第三方国产)告警日志显示,每天晚上备份服务器的客户端在3点左右离线然后上线,再离线再上线,每晚两次,很是诡异. 联系了厂家技术支持,前后花了两天 ...
- HashMap集合-遍历方法
# HashMap集合-遍历方法 先定义好集合: public static void main(String[] args) { Map<String,String> onemap=ne ...
- 关于使用pietty或putty终端连接ubuntu虚拟机时报被拒绝连接问题
首先如果要使用终端进行远程连接的ubuntu虚拟机的话,必须保证其虚拟机ip能在window下ping的动.具体的ubuntu网络配置这里不再讲,我这里使用的是NAT连接. 然后检查ssh服务是否有安 ...
- 学习GTK+ (1) ——编写helloworld
环境 我使用的是新安装的manjaro 18.1 (kde版),安装新系统后后直接可以开始写代码,不需要安装各种调用的库等. 推荐一个网站,gnome开发者 https://developer.gno ...
- 移动端测试之APP安全测试
现在APP测试已经是测试行业的一个重要分支,对APP测试技能和经验的要求也越来越高,看到一篇关于APP安全测试的总结,分享给需要的朋友.1.软件权限1)扣费风险:包括发送短信.拨打电话.连接网络等2) ...
- Get HttpWebResponse and HttpClient Return String by proxy
#region Get HttpClient Return String /// <summary> /// Get HttpClient Return String /// </s ...
- 怎样限制第三方Cookie
使用Cookie的 SameSite 属性. 1. SameSite=Strict; 这个模式下, 服务器将会完全禁止第三方Cookie, 在跨站点时, 任何情况下都不会发送Cookie, 也就是说, ...
- Mysql定时备份[Windows]
基于mysql5.6.39版本 一.备份脚本 1.windows环境创建批处理文件 @echo off rem ******MySQL backup start****** set mysqlHome ...