思路:

就是把带修莫队移到了树上

块的大小开到(n^2/3)/2 比较好…

这是一个卡OJ好题

//By SiriusRen
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=100050;
int n,m,q,xx,yy,Block,block[N],cnt=1,fa[N][20],tot,top,cnt1,cnt2,op,num[N];
int first[N],next[N*2],v[N*2],C[N],stk[N],deep[N],V[N],W[N],last[N],vis[N];
typedef long long ll;ll Ans[N],ans;
struct Query{
int l,r,lca,id,time;Query(){}
Query(int ll,int rr,int zz,int ii,int tt){l=ll,r=rr,lca=zz,id=ii,time=tt;}
friend bool operator<(Query a,Query b){
if(block[a.l]==block[b.l]){
if(block[a.r]==block[b.r])return a.time<b.time;
return block[a.r]<block[b.r];
}
return block[a.l]<block[b.l];
}
}query[N];
struct Change{
int position,color,lastcolor;Change(){}
Change(int pp,int cc,int ll){position=pp,color=cc,lastcolor=ll;}
}change[N];
void add(int x,int y){v[tot]=y,next[tot]=first[x],first[x]=tot++;}
void dfs(int x){
for(int i=first[x];~i;i=next[i])if(v[i]!=fa[x][0])
fa[v[i]][0]=x,deep[v[i]]=deep[x]+1,dfs(v[i]);
stk[++top]=x;
if(top==Block){
for(int i=1;i<=top;i++)block[stk[i]]=cnt;
top=0,cnt++;
}
}
int lca(int x,int y){
if(deep[x]<deep[y])swap(x,y);
for(int i=19;i>=0;i--)if(deep[x]-(1<<i)>=deep[y])x=fa[x][i];
if(x==y)return x;
for(int i=19;i>=0;i--)if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
void reverse(int x){
if(vis[x])ans-=(ll)V[C[x]]*W[num[C[x]]],num[C[x]]--;
else num[C[x]]++,ans+=(ll)V[C[x]]*W[num[C[x]]];
vis[x]^=1;
}
void change_color(int x,int y){
if(vis[x])reverse(x),C[x]=y,reverse(x);
else C[x]=y;
}
void work(int x,int y){
while(x!=y){
if(deep[x]<deep[y])swap(x,y);
reverse(x),x=fa[x][0];
}
}
int read(){
char p=getchar();int x=0;
while(p<'0'||p>'9')p=getchar();
while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();
return x;
}
int main(){
memset(first,-1,sizeof(first));
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=m;i++)V[i]=read();
for(int i=1;i<=n;i++)W[i]=read();
Block=pow(n,2.0/3.0)*0.5;
for(int i=1;i<n;i++)xx=read(),yy=read(),add(xx,yy),add(yy,xx);
deep[1]=1,dfs(1);
for(int i=1;i<=top;i++)block[stk[i]]=cnt;
for(int j=1;j<=19;j++)
for(int i=1;i<=n;i++)
fa[i][j]=fa[fa[i][j-1]][j-1];
for(int i=1;i<=n;i++)C[i]=read(),last[i]=C[i];
for(int i=1;i<=q;i++){
op=read(),xx=read(),yy=read();
if(op){
if(block[xx]>block[yy])swap(xx,yy);
query[++cnt1]=Query(xx,yy,lca(xx,yy),cnt1,cnt2);
}
else change[++cnt2]=Change(xx,yy,last[xx]),last[xx]=yy;
}
sort(query+1,query+1+cnt1);
for(int i=1,T=0;i<=cnt1;i++){
for(;T<query[i].time;T++)change_color(change[T+1].position,change[T+1].color);
for(;T>query[i].time;T--)change_color(change[T].position,change[T].lastcolor);
if(i==1)work(query[i].l,query[i].r);
else work(query[i-1].l,query[i].l),work(query[i-1].r,query[i].r);
reverse(query[i].lca),Ans[query[i].id]=ans,reverse(query[i].lca);
}
for(int i=1;i<=cnt1;i++)printf("%lld\n",Ans[i]);
}

BZOJ 3052 树上带修莫队的更多相关文章

  1. BZOJ 4129 树上带修莫队+线段树

    思路: 可以先做做BZOJ3585 是序列上的mex 考虑莫队的转移 如果当前数字出现过 线段树上把它置成1 对于询问 二分ans 线段树上查 0到ans的和 是不是ans+1 本题就是把它搞到了序列 ...

  2. BZOJ 3052/Luogu P4074 [wc2013]糖果公园 (树上带修莫队)

    题面 中文题面,难得解释了 BZOJ传送门 Luogu传送门 分析 树上带修莫队板子题... 开始没给分块大小赋初值T了好一会... CODE #include <bits/stdc++.h&g ...

  3. BZOJ 4129 Haruna’s Breakfast ( 树上带修莫队 )

    题面 求树上某路径上最小的没出现过的权值,有单点修改 添加链接描述 分析 树上带修莫队板题,问题是怎么求最小的没出现过的权值. 因为只有nnn个点,所以没出现过的最小值一定在[0,n][0,n][0, ...

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

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

  5. LUOGU P4074 [WC2013]糖果公园 (树上带修莫队)

    传送门 解题思路 树上带修莫队,搞了两天..终于开O2+卡常大法贴边过了...bzoj上跑了183s..其实就是把树上莫队和带修莫队结合到一起,首先求出括号序,就是进一次出一次那种的,然后如果求两个点 ...

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

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

  7. luogu4074 [WC2013]糖果公园(树上带修莫队)

    link 题目大意:给一个树,树上每个点都有一种颜色,每个颜色都有一个收益 每次修改一个点上的颜色 或询问一条链上所有颜色第i次遇到颜色j可以获得w[i]*v[j]的价值,求链上价值和 题解:树上带修 ...

  8. bzoj4129 Haruna’s Breakfast 树上带修莫队+分块

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4129 题解 考虑没有修改的序列上的版本应该怎么做: 弱化的题目应该是这样的: 给定一个序列,每 ...

  9. BZOJ3052(树上带修莫队)

    树上莫队的基本思路是把树按dfs序分块,然后先按x所在块从小到大排序,再按y所在块从小到大排序,处理询问即可. 这道题带修改,再加一个时间维即可. 设块大小为T,那么时间复杂度为$O(nT+\frac ...

随机推荐

  1. Git Learning Part I - Install Git and configure it

    Why we need 'Git' GIt version control: 1. record the history about updating code and deleting code 2 ...

  2. 体验:Anko + Kotlin

    ● 依赖:compile 'org.jetbrains.anko:anko:0.10.0' ● 界面: import android.view.View import org.jetbrains.an ...

  3. hdu1829 A Bug's Life 基础种类并查集

    题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋. 思路是把相同性别的归为一个集合(等价类),异性的异性为同性. #include<iostream> #i ...

  4. 利用Axis2默认口令安全漏洞入侵WebService网站

    近期,在黑吧安全网上关注了几则利用Axis2默认口令进行渗透测试的案例,大家的渗透思路基本一致,利用的技术工具也大致相同,我在总结这几则案例的基础之上进行了技术思路的拓展.黑吧安全网Axis2默认口令 ...

  5. Qt:&OpenCV—Q图像处理基本操作(Code)

    原文链接:http://www.cnblogs.com/emouse/archive/2013/03/31/2991333.html 作者写作一系列:http://www.cnblogs.com/em ...

  6. 我的新书《计算机图形学基础(OpenGL版)》

    我的新书<计算机图形学基础(OpenGL版)>今年6月份在清华大学出版社出版了!新书与原在机械工业出版社出的<计算机图形学>相比,主要有以下不同: 1.加重OpenGL的内容, ...

  7. C# switch 语句

    switch ("MySql") //选择语句 // case语句 成对 结束 执行到 第一个break { case "SqlServer2000": cas ...

  8. windwo下载完nvm无法执行node

    安装node版本管理工具之NVM.安装方法:见链接. window安装完后,下载node后,无法执行node.见图(图片从网上找的). 最后问题原因是,1.nvm安装时,安装目录中存在空格. 解决办法 ...

  9. Java中数组的反转

    public class ArrayDemo2 { public static void main(String[] args) { //定义一个数组存放元素 int[] arr3 = {10, 20 ...

  10. Apex语言(一)开发环境

    1.注册salesforce开发者https://developer.salesforce.com/ 2.开发者登录https://login.salesforce.com/ 3.Apex开发者工具 ...