【块状树】【树链剖分】【线段树】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 ...
随机推荐
- Elasticsearch报错
[2018-07-12T10:32:47,642][INFO ][o.e.b.BootstrapChecks ] [VfCcJIq] bound or publishing to a non-loop ...
- 迅雷Bolt的ClipSubBindBitmap函数特别说明
因为在工作中基于迅雷Bolt开发的是IM产品,需要实现自定义用户头像的功能. 但Bolt中对图像的默认拉伸锯齿效果非常明显,所以自己实现了图像拉伸函数,代码已共享,具体可查看:<迅雷Bolt图像 ...
- Sync Data to AWS S3 on Windows Box
1. Install AWS CLI first, windows download link https://s3.amazonaws.com/aws-cli/AWSCLI64.msi 2. The ...
- js 扒一扒这些题目都考了哪些知识点
1.空数组是true还是false 题目: <script> var tmp = 'a'; if([]==false) tmp+='b'; if(![]) tmp +='c'; alert ...
- SPOJ 1182 Sorted bit sequence
题目链接 题意: 分析: 其实如果会了Ural 1057. Amount of Degrees那道题目,这道题自然也就会了... 我们考虑枚举第$k$个数字的$1$的个数,那么我们需要计算的也就是区间 ...
- 汕头市队赛 yyl杯1 T2
B SRM 05 - YYL 杯 R1 背景&&描述 有一个拥有n个城市的国家.这个国家由n-1条边连接起来.有一天国家发生叛乱.叛军已占领了一些城市.如果叛军占领的城市中,存在两个城 ...
- swift中_的用法,忽略默认参数名。
swift中默认参数名除了第一个之外,其他的默认是不忽略的,但是如果在参数的名字前面加上_,就可以忽略这个参数名了,虽然有些麻烦,但是这种定义也挺好,而且不想知道名字或者不想让别人知道名字的或者不用让 ...
- django2.0的reverse
导入: 官方文档地址:https://yiyibooks.cn/xx/Django_1.11.6/topics/http/urls.html from django.urls import rever ...
- HDU1248 (完全背包简单变形)
寒冰王座 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Python Requests 小技巧总结
关于 Python Requests ,在使用中,总结了一些小技巧把,分享下. 1:保持请求之间的Cookies,我们可以这样做. import requests self.session = req ...