bzoj3052
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3052
题目大意:自己看看,懒得写
题解:带修改的树上莫队,经典爆评测机的题
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define maxn 100100
#define ll long long
using namespace std;
int n,m,q,blo;
ll ans;
int tot,top,totc,totq,lydsytime,belong;
int bin[],deep[maxn],fa[maxn][],dfn[maxn];
int z[maxn];
int num[maxn];
int now[maxn],v[maxn*],pre[maxn*];
int pos[maxn];
ll res[maxn],val[maxn],c[maxn],cpre[maxn],w[maxn];
bool vis[maxn];
struct data{
int x,y,t,id;
ll pre;
}cg[maxn],bg[maxn];
int read()
{
int x=; char ch; bool bo=;
while (ch=getchar(),ch<''||ch>'') if (ch=='-') bo=;
while (x=x*+ch-'',ch=getchar(),ch>=''&&ch<='');
if (bo) return -x; return x;
}
bool cmp(data a,data b)
{
if (pos[a.x]==pos[b.x]) return dfn[a.y]<dfn[b.y]; return pos[a.x]<pos[b.x];
}
void ins(int a,int b){++tot; pre[tot]=now[a]; now[a]=tot; v[tot]=b;
}
void prework(){
bin[]=;
for (int i=; i<=; i++) bin[i]=bin[i-]*;
}
int dfs(int x)
{
int size=;
dfn[x]=++lydsytime;
for (int i=; i<=; i++)
if (deep[x]>=bin[i])
fa[x][i]=fa[fa[x][i-]][i-];
else
break;
for (int p=now[x]; p; p=pre[p])
{
int son=v[p];
if (son==fa[x][]) continue;
deep[son]=deep[x]+;
fa[son][]=x;
size+=dfs(son);
if (size>blo)
{
belong++;
for (int i=; i<=blo; i++) pos[z[top--]]=belong;
size=;
}
}
z[++top]=x;
return size+;
}
int lca(int x,int y)
{
// cout<<" pre "<<x<<" "<<y<<endl;
if (deep[x]<deep[y]) swap(x,y);
int t=deep[x]-deep[y];
for (int i=; bin[i]<=t; i++)
if (bin[i]&t)
x=fa[x][i];
for (int i=; i>=; i--)
if (fa[x][i]!=fa[y][i])
x=fa[x][i], y=fa[y][i];
// cout<<" now "<<x<<" "<<y<<endl;
if (x==y) return x;
return fa[x][];
}
void reverse(int x)
{
//cout<<" re "<<x<<" "<<c[x]<<" "<<val[c[x]]<<" "<<num[c[x]]<<" "<<w[num[c[x]]+1]<<" "<<w[1]<<endl;
if (vis[x]) ans-=val[c[x]]*w[num[c[x]]],num[c[x]]--;
else num[c[x]]++,ans+=val[c[x]]*w[num[c[x]]];
vis[x]^=;
}
void work(int u,int v)
{
//cout<<" pre u v "<<u<<" "<<v<<endl;
while (u!=v)
{
if (deep[u]>deep[v])
{
reverse(u);
u=fa[u][];
}
else
{
reverse(v);
v=fa[v][];
}
//cout<<" now u v "<<u<<" "<<v<<" "<<endl;
}
}
void change(int x,int y)
{
if (vis[x])
{
reverse(x);
c[x]=y;
reverse(x);
}
else c[x]=y;
}
void init()
{
n=read(); m=read(); q=read();
blo=pow(n,2.0/)*0.5;
for (int i=; i<=m; i++) val[i]=read();
for (int i=; i<=n; i++) w[i]=read();
for (int i=; i<n; i++)
{
int u=read(),v=read();
ins(u,v); ins(v,u);
}
dfs();
//for (int i=1; i<=n; i++) cout<<" dsdsd "<<fa[i][0]<<" "<<dfn[i]<<endl;
belong++;
while (top) pos[z[top--]]=belong;
for (int i=; i<=n; i++) c[i]=cpre[i]=read();
for (int i=; i<=q; i++)
{
int type=read(),x=read(),y=read();
if (!type)
{
totc++; cg[totc].x=x,cg[totc].y=y,cg[totc].pre=cpre[x],cpre[x]=y;
}
else
{
totq++; bg[totq].x=x,bg[totq].y=y,bg[totq].t=totc; bg[totq].id=totq;
}
}
sort(bg+,bg+totq+,cmp);
/*for (int i=1; i<=totq; i++)
{
cout<<" "<<bg[i].x<<" "<<bg[i].y<<" "<<bg[i].t<<" "<<endl;
}*/
}
void solve()
{
for (int i=; i<=bg[].t; i++) change(cg[i].x,cg[i].y);
work(bg[].x,bg[].y);
int t=lca(bg[].x,bg[].y);
//cout<<" "<<t<<" "<<bg[1].x<<" "<<bg[1].y<<endl;
reverse(t); res[bg[].id]=ans; reverse(t);
for (int i=; i<=totq; i++)
{
for (int j=bg[i-].t+; j<=bg[i].t; j++)
change(cg[j].x,cg[j].y);
for (int j=bg[i-].t+; j>bg[i].t; j--)
change(cg[j].x,cg[j].pre);
work(bg[i-].x,bg[i].x); work(bg[i-].y,bg[i].y);
int t=lca(bg[i].x,bg[i].y);
reverse(t); res[bg[i].id]=ans; reverse(t);
}
}
int main()
{
prework();
init();
solve();
for (int i=; i<=totq; i++)
printf("%lld\n",res[i]);
}
/*
4 3 5
1 9 2
7 6 5 1
2 3
3 1
3 4
1 2 3 2
1 1 2
1 4 2
0 2 1
1 1 2
1 4 2
*/
一次CE,一次WA,一次AC
-------------哦,对了这道题还是有个坑,就是怎么推出带修改的树上莫队时间复杂度!待zfy来教我补!
bzoj3052的更多相关文章
- BZOJ3052/UOJ#58 [wc2013]糖果公园 莫队 带修莫队 树上莫队
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3052.html 题目传送门 - BZOJ3052 题目传送门 - UOJ#58 题意 给定一棵树,有 ...
- 【BZOJ3052】【UOJ#58】【WC2013】糖果公园(树上莫队)
[BZOJ3052][UOJ#58][WC2013]糖果公园(树上莫队) 题面 UOJ 洛谷 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引 ...
- 【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 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来 ...
- bzoj3052: [wc2013]糖果公园
又是一代神题. uoj测速rank10,bzoj测速rank26(截止当前2016.5.30 12:58) 带修改的树上莫队. 修改很少,块的大小随便定都能A 然而我一开始把开3次根写成了pow(bl ...
- 【BZOJ-3052】糖果公园 树上带修莫队算法
3052: [wc2013]糖果公园 Time Limit: 200 Sec Memory Limit: 512 MBSubmit: 883 Solved: 419[Submit][Status] ...
- BZOJ3052——糖果公园
0.题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3052 1.题目大意:给定一颗n个点的无根树,每个点有一个颜色,要进行q次操作,有两种操 ...
- 【分块】【树上莫队】bzoj1086 bzoj3052
1086 http://vfleaking.blog.163.com/blog/static/174807634201231684436977/ 3052 http://vfleaking.blog. ...
- BZOJ3052(树上带修莫队)
树上莫队的基本思路是把树按dfs序分块,然后先按x所在块从小到大排序,再按y所在块从小到大排序,处理询问即可. 这道题带修改,再加一个时间维即可. 设块大小为T,那么时间复杂度为$O(nT+\frac ...
随机推荐
- Django之路:QuerySet API,后台和表单
一.Django QuerySet API Django模型中我们学习了一些基本的创建和查询.这里专门讲以下数据库接口相关的接口(QuerySet API),当然你也可以选择暂时跳过这节.如果以后用到 ...
- 关于数据结构的10个面试题(c语言实现)
关于数据结构的10个面试题(c语言实现) 2010-04-21 22:17 5702人阅读 评论(0) 收藏 举报 数据结构面试c语言bttree 1. 输入一个链表的头结点,从尾到头 ...
- mysql distinct、group_concat
mysql distinct语句用于查询多条不重复记录值(去重.过滤多余的重复记录) distinct同时作用了两个字段或者两个以上的字段,必须得作用了的字段都相同的才被排除.如果想让单个列" ...
- extjs 框架模板
的 <script> Ext.onReady(function(){ Ext.create('Ext.container.Viewport', { layout: 'border', it ...
- fn标签
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
- android usb挂载分析---vold处理内核消息
android usb挂载分析---vold处理内核消息 分类: u盘挂载2012-03-29 22:25 3215人阅读 评论(0) 收藏 举报 androidactioniteratordiskd ...
- Android新手入门
本博客出自公众号安卓应用频道:http://mp.weixin.qq.com/s?__biz=MzA3MDMyMjkzNg==&mid=2652261947&idx=1&sn= ...
- Sping--IOC概念
1. 新建项目, 引入spring包(sping, common-annotation, common-logging包), 还有junit包. user.java: package com.bjsx ...
- iOS 键盘类型UIKeyboardType
UIKeyboardTypeDefault: UIKeyboardTypeASCIICapable: UIKeyboardTypeNumbersAndPunctuation: UIKeyboardTy ...
- NULL、nil、Nil、NSNull的区别
标志 值 含义 NULL (void *)0 C指针的字面零值 nil (id)0 Objecve-C对象的字面零值 Nil (Class)0 Objecve-C类的字面零值 NSNull [NSNu ...