[WC2013]糖果公园

思路:

  带修改树上莫队(模板);

来,上代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 100005
#define ll long long struct QueryType {
ll u,v,t,id;
};
struct QueryType qu[maxn]; struct ChangeType {
ll to,x,h;
};
struct ChangeType cha[maxn]; ll n,m,q,ti[maxn],totq,totc,vi[maxn],wi[maxn];
ll siz,deep[maxn],f[maxn],top[maxn],bel[maxn],size[maxn];
ll E[maxn<<],V[maxn<<],head[maxn],cnt,dis[maxn],ans[maxn]; bool if_[maxn]; inline void in(ll &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} void pre(ll now,ll fa)
{
deep[now]=deep[fa]+,size[now]=;
bel[now]=((++cnt)+)/siz,f[now]=fa;
for(ll i=head[now];i;i=E[i])
{
if(V[i]==fa) continue;
pre(V[i],now),size[now]+=size[V[i]];
}
} void dfs(ll now,ll chain)
{
ll pos=;top[now]=chain;
for(ll i=head[now];i;i=E[i])
{
if(V[i]==f[now]) continue;
if(size[V[i]]>size[pos]) pos=V[i];
}
if(pos==) return ;
dfs(pos,chain);
for(ll i=head[now];i;i=E[i])
{
if(V[i]==pos||V[i]==f[now]) continue;
dfs(V[i],V[i]);
}
} ll solve_lca(ll x,ll y)
{
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
x=f[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
return x;
} bool cmp(QueryType aa,QueryType bb)
{
if(bel[aa.u]==bel[bb.u])
{
if(bel[aa.v]==bel[bb.v]) return aa.t<bb.t;
else return bel[aa.v]<bel[bb.v];
}
else return bel[aa.u]<bel[bb.u];
} inline void change(ll x)
{
if(if_[cha[x].to])
{
cnt-=wi[ti[dis[cha[x].to]]]*vi[dis[cha[x].to]];
ti[dis[cha[x].to]]--;
}
cha[x].h=dis[cha[x].to];
dis[cha[x].to]=cha[x].x;
if(if_[cha[x].to])
{
ti[cha[x].x]++;
cnt+=wi[ti[cha[x].x]]*vi[cha[x].x];
}
} inline void unchange(ll x)
{
if(if_[cha[x].to])
{
cnt-=wi[ti[cha[x].x]]*vi[cha[x].x];
ti[cha[x].x]--;
}
dis[cha[x].to]=cha[x].h;
if(if_[cha[x].to])
{
ti[cha[x].h]++;
cnt+=wi[ti[cha[x].h]]*vi[cha[x].h];
}
} inline void updata(ll x)
{
if(if_[x])
{
cnt-=wi[ti[dis[x]]]*vi[dis[x]];
ti[dis[x]]--;
}
else
{
ti[dis[x]]++;
cnt+=wi[ti[dis[x]]]*vi[dis[x]];
}
if_[x]=!if_[x];
} inline void out(ll x)
{
if(x>) out(x/);
putchar(x%+);
} int main()
{
freopen("park.in","r",stdin);
freopen("park.out","w",stdout);
in(n),in(m),in(q);ll u,v;siz=sqrt(n);
for(ll i=;i<=m;i++) in(vi[i]);
for(ll i=;i<=n;i++) in(wi[i]);
for(ll i=;i<n;i++)
{
in(u),in(v);
E[++cnt]=head[u],V[cnt]=v,head[u]=cnt;
E[++cnt]=head[v],V[cnt]=u,head[v]=cnt;
}
for(ll i=;i<=n;i++) in(dis[i]);
cnt=,pre(,),dfs(,);ll ty;
for(ll i=;i<=q;i++)
{
in(ty);
if(ty)
{
in(qu[++totq].u),in(qu[totq].v),qu[totq].t=totc,qu[totq].id=totq;
if(bel[qu[totq].u]>bel[qu[totq].v]) swap(qu[totq].u,qu[totq].v);
}
else in(cha[++totc].to),in(cha[totc].x);
}
sort(qu+,qu+totq+,cmp),u=,v=,cnt=;ll t=;
for(ll no=;no<=totq;no++)
{
while(t<qu[no].t) change(++t);
while(t>qu[no].t) unchange(t--);
ll lca=solve_lca(u,qu[no].u);
while(u!=lca) updata(u),u=f[u];u=qu[no].u;
while(u!=lca) updata(u),u=f[u];u=qu[no].u;
lca=solve_lca(v,qu[no].v);
while(v!=lca) updata(v),v=f[v];v=qu[no].v;
while(v!=lca) updata(v),v=f[v];v=qu[no].v;
lca=solve_lca(u,v);
updata(lca),ans[qu[no].id]=cnt,updata(lca);
}
for(ll i=;i<=totq;i++) out(ans[i]),putchar('\n');
fclose(stdin),fclose(stdout);
return ;
}

AC日记——[WC2013]糖果公园 cogs 1817的更多相关文章

  1. COGS1817. [WC2013]糖果公园

    1817. [WC2013]糖果公园 ★★★☆   输入文件:park.in   输出文件:park.out   简单对比时间限制:8 s   内存限制:512 MB [题目描述] Candyland ...

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

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

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

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

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

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

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

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

  6. 【BZOJ】3052: [wc2013]糖果公园 树分块+带修改莫队算法

    [题目]#58. [WC2013]糖果公园 [题意]给定n个点的树,m种糖果,每个点有糖果ci.给定n个数wi和m个数vi,第i颗糖果第j次品尝的价值是v(i)*w(j).q次询问一条链上每个点价值的 ...

  7. 【Luogu P4074】[WC2013]糖果公园(树上带修改莫队)

    题目描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩. 糖果公园的结构十分奇特,它由 \(n\) 个游 ...

  8. 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 ...

  9. P4074 [WC2013]糖果公园 树上莫队带修改

    题目链接 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩. 糖果公园的结构十分奇特,它由 nn 个游览点构 ...

随机推荐

  1. Java-JNA使用心得2

    自5月初第一次尝试使用Java封装调用C的dll之后,已经先后经历了3次小项目了. 上月末是最近的一次项目实际,任务来的急时间又少,还好在加班加点后还是完成了任务,并把第二次没有实现的功能给实现了(C ...

  2. border与background定位

    1.background定位的局限 只能相对于左上角数值定位,不能相对于右下 即background-position默认相对于左上方定位的 2.怎样让图片相对于右下角? background-pos ...

  3. unity3d中C#与JS的一些区别

    unity3d目前支持C#和JS两种脚本语言. 学习过程中发现很多教程使用的是JS语言,自己还是用C#比较多,unity原生使用的是Mono,使用C#会更加接近unity的编程思想. 1.方法的定义, ...

  4. 《Cracking the Coding Interview》——第14章:Java——题目2

    2014-04-26 18:44 题目:在java的try-catch-finally语句块里,如果catch里面有return语句的话,finally还会被执行吗? 解法:会. 代码: // 14. ...

  5. 《Cracking the Coding Interview》——第7章:数学和概率论——题目2

    2014-03-20 01:59 题目:有n只蚂蚁在正n边形的n个顶点,同时以同速率开始沿着边走.每只蚂蚁走的方向是随机的,那么这些蚂蚁至少有两只发生碰撞的概率是多少. 解法:只有所有蚂蚁都往一个方向 ...

  6. java程序员笑不死的经历ส้้้้้้้้้

    ส้้้้้้้้้้ส้้้้้้้้้้ส้้้้้้้้้ 1.程序猿最烦两件事,第一件事是别人要求他给自己的代码写文档,第二件呢?是别人的程序没有留下文档. 2.宪法顶个球!中国的法律都是.t ...

  7. python 多版本的兼容

    1.针对linux版本 linux版本的话,首先调用whereis python 来获取到多版本的路径. root@Ulord-14:~# whereis pythonpython: /usr/bin ...

  8. fragment中的WebView返回上一页

    public final class Text1Fm extends Fragment { static WebView mWeb; private View mContentView; privat ...

  9. [SDOI2015][bzoj3990] 序列 [搜索]

    题面 传送门 思路 首先,这道题目有一个非常显然(但是我不会严格证明,只能意会一下)的结论:一个合法的操作序列中,任意两个操作是可以互换的 那么,这个结论加上本题极小的数据范围,为什么不搜索一下呢? ...

  10. 【CZY选讲·逆序对】

    题目描述 LYK最近在研究逆序对. 这个问题是这样的. 一开始LYK有一个2^n长度的数组ai. LYK有Q次操作,每次操作都有一个参数k.表示每连续2^k长度作为一个小组.假设 n=4,k= ...