【块状树】【树链剖分】【线段树】bzoj3531 [Sdoi2014]旅行
离线后以宗教为第一关键字,操作时间为第二关键字排序。
<法一>块状树,线下ac,线上tle……
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
queue<int>q;
int f,c;
inline void R(int &x){
c=0;f=1;
for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
for(x=0;c>='0'&&c<='9';c=getchar())(x*=10)+=(c-'0');
x*=f;
}
void P(int x){
if(x<10)putchar(x+'0');
else{P(x/10);putchar(x%10+'0');}
}
#define N 100001
int en,first[N],next[N<<1],v[N<<1];
void AddEdge(const int &U,const int &V)
{
v[++en]=V;
next[en]=first[U];
first[U]=en;
}
int tops[N],en4;
bool delta[N];
struct THING{int op,type,x,y,t,p;}th[N*3];
bool operator < (const THING &a,const THING &b)
{return a.type!=b.type ? a.type<b.type : a.t<b.t;}
int n,m,w[N],ty[N],en2,en3;
int fa[N],dep[N],siz[N],top[N],sz,maxv[N],sumv[N];
int anss[N];
void makeblock()
{
q.push(1);
while(!q.empty())
{
int U=q.front(); q.pop();
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U])
{
dep[v[i]]=dep[U]+1;
fa[v[i]]=U;
if(siz[top[U]]<sz)
{
++siz[top[U]];
top[v[i]]=top[U];
}
q.push(v[i]);
}
}
}
void dfs(int U,int Sumnow,int Maxnow)
{
maxv[U]=Maxnow;
sumv[U]=Sumnow;
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U]&&top[v[i]]==top[U])
dfs(v[i],Sumnow+w[v[i]],max(Maxnow,w[v[i]]));
}
void dfs0(int U)
{
maxv[U]=sumv[U]=w[U]=0;
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U]&&top[v[i]]==top[U])
dfs0(v[i]);
}
void update(const int &p,const int &val)
{
if(!delta[top[p]])
{
dfs0(top[p]);
delta[top[p]]=1;
}
w[p]=val;
if(p==top[p]) dfs(p,val,val);
else dfs(p,val+sumv[fa[p]],max(val,maxv[fa[p]]));
}
int Query_max(int U,int V)
{
int res=0;
while(U!=V)
{
if(top[U]==top[V])
{
if(dep[U]<dep[V]) swap(U,V);
res=max(res,delta[top[U]]?w[U]:0);
U=fa[U];
}
else
{
if(dep[top[U]]<dep[top[V]]) swap(U,V);
res=max(res,delta[top[U]]?maxv[U]:0);
U=fa[top[U]];
}
}
return max(res,delta[top[U]]?w[U]:0);
}
int Query_sum(int U,int V)
{
int res=0;
while(U!=V)
{
if(top[U]==top[V])
{
if(dep[U]<dep[V]) swap(U,V);
res+=(delta[top[U]]?w[U]:0);
U=fa[U];
}
else
{
if(dep[top[U]]<dep[top[V]]) swap(U,V);
res+=(delta[top[U]]?sumv[U]:0);
U=fa[top[U]];
}
}
return res+(delta[top[U]]?w[U]:0);
}
int main()
{
int A,B; char op[3];
R(n); R(m);
for(int i=1;i<=n;++i)
{
R(w[i]); R(ty[i]);
th[i].type=ty[i];
th[i].x=i;
th[i].t=i;
th[i].y=w[i];
}
for(int i=1;i<n;++i)
{
R(A); R(B);
AddEdge(A,B);
AddEdge(B,A);
}
sz=sqrt(0.03*(double)n);
for(int i=1;i<=n;i++)
{
top[i]=i;
siz[i]=1;
}
makeblock();
en2=n;
for(int i=1;i<=m;++i)
{
op[0]=getchar(); op[1]=getchar(); R(A); R(B);
if(op[0]=='C'&&op[1]=='C')
{
th[++en2].type=ty[A];
th[en2].x=A;
th[en2].y=0;
th[en2].t=en2;
ty[A]=B;
th[++en2].type=ty[A];
th[en2].x=A;
th[en2].y=w[A];
th[en2].t=en2;
}
else if(op[0]=='C'&&op[1]=='W')
{
w[A]=B;
th[++en2].type=ty[A];
th[en2].x=A;
th[en2].y=w[A];
th[en2].t=en2;
}
else if(op[0]=='Q'&&op[1]=='S')
{
th[++en2].op=1;
th[en2].type=ty[A];
th[en2].x=A;
th[en2].y=B;
th[en2].t=en2;
th[en2].p=++en3;
}
else
{
th[++en2].op=2;
th[en2].type=ty[A];
th[en2].x=A;
th[en2].y=B;
th[en2].t=en2;
th[en2].p=++en3;
}
}
sort(th+1,th+en2+1);
memset(w,0,sizeof(w));
for(int i=1;i<=n;i++) if(top[i]==i) tops[++en4]=i;
for(int i=1;i<=en4;++i) delta[tops[i]]=1;
for(int i=1;i<=en2;++i)
{
if(th[i].type!=th[i-1].type&&i!=1)
for(int j=1;j<=en4;++j) delta[tops[j]]=0;
if(!th[i].op)
update(th[i].x,th[i].y);
else if(th[i].op==1)
anss[th[i].p]=Query_sum(th[i].x,th[i].y);
else
anss[th[i].p]=Query_max(th[i].x,th[i].y);
}
for(int i=1;i<=en3;++i)
P(anss[i]),puts("");
return 0;
}
<法二>树链剖分+线段树
#include<cstdio>
#include<algorithm>
using namespace std;
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
int f,c;
inline void R(int &x){
c=0;f=1;
for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
for(x=0;c>='0'&&c<='9';c=getchar())(x*=10)+=(c-'0');
x*=f;
}
void P(int x){
if(x<10)putchar(x+'0');
else{P(x/10);putchar(x%10+'0');}
}
#define N 100001
int en,first[N],next[N<<1],v[N<<1];
void AddEdge(const int &U,const int &V)
{
v[++en]=V;
next[en]=first[U];
first[U]=en;
}
struct THING{int op,type,x,y,t,p;}th[N*3];
bool operator < (const THING &a,const THING &b)
{return a.type!=b.type ? a.type<b.type : a.t<b.t;}
int n,m,w[N],ty[N],en2,en3;
int anss[N];
int dep[N],siz[N],fa[N],son[N],top[N],Num[N],tot;
void dfs(int U,int d)
{
dep[U]=d;
siz[U]=1;
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U])
{
fa[v[i]]=U;
dfs(v[i],d+1);
siz[U]+=siz[v[i]];
if(siz[v[i]]>siz[son[U]])
son[U]=v[i];
}
}
void dfs2(int U)
{
if(son[U])
{
top[son[U]]=top[U];
Num[son[U]]=++tot;
dfs2(son[U]);
}
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U]&&v[i]!=son[U])
{
top[v[i]]=v[i];
Num[v[i]]=++tot;
dfs2(v[i]);
}
}
int maxv[N<<2],sumv[N<<2];
bool delta[N<<2];
void pushdown(const int &rt)
{
if(delta[rt])
{
delta[rt<<1]=delta[rt<<1|1]=1;
maxv[rt<<1]=maxv[rt<<1|1]=sumv[rt<<1]=sumv[rt<<1|1]=0;
delta[rt]=0;
}
}
void update(int p,int v,int rt,int l,int r)
{
if(l==r)
{
maxv[rt]=sumv[rt]=v;
return;
}
pushdown(rt);
int m=(l+r>>1);
if(p<=m) update(p,v,lson);
else update(p,v,rson);
sumv[rt]=sumv[rt<<1]+sumv[rt<<1|1];
maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
}
int query1(int ql,int qr,int rt,int l,int r)
{
if(ql<=l&&r<=qr) return sumv[rt];
pushdown(rt);
int m=(l+r>>1),res=0;
if(ql<=m) res+=query1(ql,qr,lson);
if(m<qr) res+=query1(ql,qr,rson);
return res;
}
int query2(int ql,int qr,int rt,int l,int r)
{
if(ql<=l&&r<=qr) return maxv[rt];
pushdown(rt);
int m=(l+r>>1),res=0;
if(ql<=m) res=max(res,query2(ql,qr,lson));
if(m<qr) res=max(res,query2(ql,qr,rson));
return res;
}
int Query1(int U,int V)
{
int f1=top[U],f2=top[V],res=0;
while(f1!=f2)
{
if(dep[f1]<dep[f2])
{
swap(U,V);
swap(f1,f2);
}
res+=query1(Num[f1],Num[U],1,1,n);
U=fa[f1];
f1=top[U];
}
if(dep[U]>dep[V])
swap(U,V);
return res+query1(Num[U],Num[V],1,1,n);
}
int Query2(int U,int V)
{
int f1=top[U],f2=top[V],res=0;
while(f1!=f2)
{
if(dep[f1]<dep[f2])
{
swap(U,V);
swap(f1,f2);
}
res=max(res,query2(Num[f1],Num[U],1,1,n));
U=fa[f1];
f1=top[U];
}
if(dep[U]>dep[V])
swap(U,V);
return max(res,query2(Num[U],Num[V],1,1,n));
}
int main()
{
int A,B; char op[3];
R(n); R(m);
for(int i=1;i<=n;++i)
{
R(w[i]); R(ty[i]);
th[i].type=ty[i];
th[i].x=i;
th[i].t=i;
th[i].y=w[i];
}
for(int i=1;i<n;++i)
{
R(A); R(B);
AddEdge(A,B);
AddEdge(B,A);
}
top[1]=1;
Num[1]=++tot;
dfs(1,1);
dfs2(1);
en2=n;
for(int i=1;i<=m;++i)
{
op[0]=getchar(); op[1]=getchar(); R(A); R(B);
if(op[0]=='C'&&op[1]=='C')
{
th[++en2].type=ty[A];
th[en2].x=A;
th[en2].y=0;
th[en2].t=en2;
ty[A]=B;
th[++en2].type=ty[A];
th[en2].x=A;
th[en2].y=w[A];
th[en2].t=en2;
}
else if(op[0]=='C'&&op[1]=='W')
{
w[A]=B;
th[++en2].type=ty[A];
th[en2].x=A;
th[en2].y=w[A];
th[en2].t=en2;
}
else if(op[0]=='Q'&&op[1]=='S')
{
th[++en2].op=1;
th[en2].type=ty[A];
th[en2].x=A;
th[en2].y=B;
th[en2].t=en2;
th[en2].p=++en3;
}
else
{
th[++en2].op=2;
th[en2].type=ty[A];
th[en2].x=A;
th[en2].y=B;
th[en2].t=en2;
th[en2].p=++en3;
}
}
sort(th+1,th+en2+1);
for(int i=1;i<=en2;++i)
{
if(th[i].type!=th[i-1].type&&i!=1)
delta[1]=1;
if(!th[i].op)
update(Num[th[i].x],th[i].y,1,1,n);
else if(th[i].op==1)
anss[th[i].p]=Query1(th[i].x,th[i].y);
else
anss[th[i].p]=Query2(th[i].x,th[i].y);
}
for(int i=1;i<=en3;++i)
P(anss[i]),puts("");
return 0;
}
【块状树】【树链剖分】【线段树】bzoj3531 [Sdoi2014]旅行的更多相关文章
- 【BZOJ-2325】道馆之战 树链剖分 + 线段树
2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1153 Solved: 421[Submit][Statu ...
- 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树
[BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...
- BZOJ2243 (树链剖分+线段树)
Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...
- POJ3237 (树链剖分+线段树)
Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...
- bzoj4034 (树链剖分+线段树)
Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...
- HDU4897 (树链剖分+线段树)
Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...
- Aizu 2450 Do use segment tree 树链剖分+线段树
Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- HDU 2460 Network(双连通+树链剖分+线段树)
HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...
- bzoj2243[SDOI2011]染色 树链剖分+线段树
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 9012 Solved: 3375[Submit][Status ...
随机推荐
- Ubuntu下安装LNMP之独立添加php扩展模块
使用php的过程中,发现某个扩展没有添加,又不想重新编译php,这个时候我们就需要单独添加需要的扩展模块. 下面以mysqli扩展模块为例,具体介绍安装步骤. 1.安装mysql 具体参考:Ubunt ...
- MySQL 数据库性能优化之缓存参数优化
在平时被问及最多的问题就是关于 MySQL 数据库性能优化方面的问题,所以最近打算写一个MySQL数据库性能优化方面的系列文章,希望对初中级 MySQL DBA 以及其他对 MySQL 性能优化感兴趣 ...
- 兔子与兔子 [Hash]
兔子与兔子 描述 很久很久以前,森林里住着一群兔子.有一天,兔子们想要研究自己的 DNA 序列.我们首先选取一个好长好长的 DNA 序列(小兔子是外星生物,DNA 序列可能包含 26 个小写英文字母) ...
- 关于JAVA正则匹配空白字符的问题(全角空格与半角空格)
今天遇到一个字符串,怎么匹配空格都不成功!!! 我把空格复制到test.properties文件 显示“\u3000” ,这是什么? 这是全角空格!!! 查了一下 \s 不支持全角 1.& ...
- matlab求矩阵、向量的模
求矩阵的模: function count = juZhenDeMo(a,b) [r,c] = size(a);%求a的行列 [r1,c1] = size(b);%求b的行列 count = 0; f ...
- 单选按钮radio与文字如何对齐?
布局如下: <input type="radio" value="立即发送" name="a_1">立即发送 <input ...
- import as from import 区别
在python中import或者from…import是用来导入相应的模块.那每一种有什么具体的差别呢? 一.import 只有import,为最简单的引入对应的包.例如: import ...
- [bzoj1486][HNOI2009]最小圈——分数规划+spfa+负环
题目 传送门 题解 这个题是一个经典的分数规划问题. 把题目形式化地表示,就是 \[Minimize\ \lambda = \frac{\sum W_{i, i+1}}{k}\] 整理一下,就是 \[ ...
- bzoj 1588 裸平衡树
//By BLADEVIL #include <cstdio> #include <set> #define inf 1<<30 using namespace s ...
- LeetCode 4 Median of Two Sorted Array
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...