【块状树】【树链剖分】【线段树】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 ...
随机推荐
- js 读写文件
读写文件: var f = fso.CreateTextFile("c:\\pexam\\"+name+".txt", true); f.write(arr); ...
- Linux下部署weblogic应用
1.Linux下weblogic安装 2.Linux下设置weblogic监听服务器地址(默认为本机) 1).修改domain\config\config.xml文件 修改 <server> ...
- UVa10288概率
题意: 每张彩票上印有一张图案,要集齐n个不同的图案才能获奖.输入n,求要获奖购买彩票张数的期望(假设获得每个图案的概率相同). 分析: 假设现在已经有k种图案,令s = k/n,得到一个新图案需要t ...
- (转载)--SG函数和SG定理【详解】
在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念: P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败. N点:必胜点 ...
- 将setter方法与itemClick: 进行类比
https://www.evernote.com/shard/s227/sh/a0c3afa3-8792-4756-8594-d2387a7f57ad/b561ff665af9ad401c8e ...
- bzoj 1251 裸splay
裸的splay,只需要注意 max[-1]:=-maxlongint 就行了,否则在update的时候子节点的 max值会在max[-1]里选 /*************************** ...
- 一致性hash算法小结
把服务器的IP或者主机名作为key对2^32求余,余数一定是2^32-1,然后放到(平行映射)0~2^32次方首尾相连的环上. 理想状态下服务器会均匀分布在这个环上,当数据存储时,依然把key对2 ...
- Type of flip id
http://www.haskell.org/pipermail/beginners/2011-March/006477.html The point is that the type of id h ...
- 热安装NGINX并支持多站点SSL
https://www.moonfly.net/801.html http://www.centoscn.com/image-text/config/2015/0423/5251.html 1.查看n ...
- SQL Server数据库优化笔记
不定时更新 select DateDiff(month,'2018-06-07',GETDATE()) PACTBEGINDATE ORDER BY PACTBEGINDATE ASC),GETDAT ...