bzoj-2243 2243: [SDOI2011]染色(树链剖分)
题目链接:
2243: [SDOI2011]染色
Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 6267 Solved: 2291
Description
给定一棵有n个节点的无根树和m个操作,操作有2类:
1、将节点a到节点b路径上所有点都染成颜色c;
2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“112221”由3段组成:“11”、“222”和“1”。
请你写一个程序依次完成这m个操作。
Input
第一行包含2个整数n和m,分别表示节点数和操作数;
第二行包含n个正整数表示n个节点的初始颜色
下面 行每行包含两个整数x和y,表示x和y之间有一条无向边。
下面 行每行描述一个操作:
“C a b c”表示这是一个染色操作,把节点a到节点b路径上所有点(包括a和b)都染成颜色c;
“Q a b”表示这是一个询问操作,询问节点a到节点b(包括a和b)路径上的颜色段数量。
Output
对于每个询问操作,输出一行答案。
Sample Input
2 2 1 2 1 1
1 2
1 3
2 4
2 5
2 6
Q 3 5
C 2 1 1
Q 3 5
C 5 1 2
Q 3 5
Sample Output
1
2
HINT
数N<=10^5,操作数M<=10^5,所有的颜色C为整数且在[0, 10^9]之间。
题意:
思路:
树链剖分的入门题?反正上次写hdu边那个一个T,今天写这个点的版本的还好;调了一会就过了;
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define lson o<<1
#define rson o<<1|1
const int maxn=1e5+10;
int n,m,a[maxn];
int siz[maxn],dep[maxn],fa[maxn],son[maxn],top[maxn],tid[maxn],Rank[maxn],tim=0;
vector<int>ve[maxn];
void dfs1(int cur,int father,int deep)
{
fa[cur]=father;
siz[cur]=1;
dep[cur]=deep;
int len=ve[cur].size();
for(int i=0;i<len;i++)
{
int x=ve[cur][i];
if(x==father)continue;
dfs1(x,cur,deep+1);
siz[cur]+=siz[x];
if(son[cur]==-1||siz[x]>siz[son[cur]])son[cur]=x;
}
}
void dfs2(int cur,int tp)
{
top[cur]=tp;
tid[cur]=++tim;
Rank[tim]=cur;
if(son[cur]==-1)return ;
dfs2(son[cur],tp);
int len=ve[cur].size();
for(int i=0;i<len;i++)
{
int x=ve[cur][i];
if(x==fa[cur]||x==son[cur])continue;
dfs2(x,x);
}
}
struct Tree
{
int l,r,sum,lc,rc,mark;
}tr[maxn*4];
inline void pushup(int o)
{
tr[o].sum=tr[lson].sum+tr[rson].sum;
tr[o].lc=tr[lson].lc;tr[o].rc=tr[rson].rc;
if(tr[lson].rc==tr[rson].lc)tr[o].sum--;
}
inline void pushdown(int o)
{
if(tr[o].mark>=0)
{
tr[lson].sum=tr[rson].sum=1;
tr[lson].lc=tr[lson].rc=tr[o].mark;
tr[rson].lc=tr[rson].rc=tr[o].mark;
tr[lson].mark=tr[rson].mark=tr[o].mark;
tr[o].mark=-1;
}
}
void build(int o,int L,int R)
{
tr[o].l=L;tr[o].r=R;
tr[o].mark=-1;
if(L>=R)
{
tr[o].sum=1;
tr[o].lc=tr[o].rc=a[Rank[L]];
return ;
}
int mid=(L+R)>>1;
build(lson,L,mid);
build(rson,mid+1,R);
pushup(o);
}
void update(int o,int L,int R,int num)
{
if(L<=tr[o].l&&R>=tr[o].r)
{
tr[o].sum=1;
tr[o].lc=tr[o].rc=num;
tr[o].mark=num;
return ;
}
int mid=(tr[o].l+tr[o].r)>>1;
pushdown(o);
if(L<=mid)update(lson,L,R,num);
if(R>mid)update(rson,L,R,num);
pushup(o);
}
int query(int o,int L,int R,int & Lc,int & Rc)
{
if(L<=tr[o].l&&R>=tr[o].r)
{
if(L==tr[o].l)Lc=tr[o].lc;
if(R==tr[o].r)Rc=tr[o].rc;
return tr[o].sum;
}
int mid=(tr[o].l+tr[o].r)>>1;
pushdown(o);
if(R<=mid)return query(lson,L,R,Lc,Rc);
else if(L>mid)return query(rson,L,R,Lc,Rc);
else
{
int ans=query(lson,L,R,Lc,Rc)+query(rson,L,R,Lc,Rc);
if(tr[lson].rc==tr[rson].lc)ans--;
return ans;
}
} void change(int u,int v,int w)
{
int fu=top[u],fv=top[v]; while(fu!=fv)
{
if(dep[fu]<dep[fv])swap(u,v),swap(fu,fv);
update(1,tid[fu],tid[u],w);
u=fa[fu];
fu=top[u];
}
if(dep[u]<dep[v])swap(u,v);
update(1,tid[v],tid[u],w);
}
int solve(int u,int v)
{
int ans=0,fu=top[u],fv=top[v];
int preul=-1,preur=-1,prevl=-1,prevr=-1;
int nowul=-1,nowur=-1,nowvl=-1,nowvr=-1;
while(fu!=fv)
{
if(dep[fu]>dep[fv])
{
ans+=query(1,tid[fu],tid[u],nowul,nowur);
if(nowur==preul&&preul!=-1)ans--;
preul=nowul;preur=nowur;
u=fa[fu];
fu=top[u];
}
else
{
ans+=query(1,tid[fv],tid[v],nowvl,nowvr);
if(nowvr==prevl&&prevl!=-1)ans--;
prevl=nowvl;prevr=nowvr;
v=fa[fv];
fv=top[v];
}
}
if(dep[u]>dep[v])
{
ans+=query(1,tid[v],tid[u],nowul,nowur);
if(nowur==preul&&preul!=-1)ans--;
if(nowul==prevl&&prevl!=-1)ans--;
}
else
{
ans+=query(1,tid[u],tid[v],nowvl,nowvr);
if(nowvr==prevl&&prevl!=-1)ans--;
if(nowvl==preul&&preul!=-1)ans--;
}
return ans;
}
int main()
{
int u,v,w;
char s[5];
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
ve[u].push_back(v);
ve[v].push_back(u);
}
memset(son,-1,sizeof(son));
dfs1(1,0,0);
dfs2(1,1);
build(1,1,n);
while(m--)
{
scanf("%s%d%d",s,&u,&v);
if(s[0]=='C')
{
scanf("%d",&w);
change(u,v,w);
}
else printf("%d\n",solve(u,v));
}
return 0;
}
bzoj-2243 2243: [SDOI2011]染色(树链剖分)的更多相关文章
- BZOJ 2243: [SDOI2011]染色 [树链剖分]
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6651 Solved: 2432[Submit][Status ...
- Bzoj 2243: [SDOI2011]染色 树链剖分,LCT,动态树
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 5020 Solved: 1872[Submit][Status ...
- BZOJ 2243: [SDOI2011]染色 树链剖分 倍增lca 线段树
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...
- BZOJ 2243: [SDOI2011]染色 树链剖分+线段树区间合并
2243: [SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数 ...
- BZOJ 2243: [SDOI2011]染色 (树链剖分+线段树合并)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2243 树链剖分的点剖分+线段树.漏了一个小地方,调了一下午...... 还是要细心啊! 结 ...
- [bzoj 2243]: [SDOI2011]染色 [树链剖分][线段树]
Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“ ...
- 2243: [SDOI2011]染色(树链剖分+线段树)
2243: [SDOI2011]染色 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 8400 Solved: 3150[Submit][Status ...
- 2243: [SDOI2011]染色 树链剖分+线段树染色
给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段), 如“112221”由3段组 ...
- POJ 2243 [SDOI2011]染色 | 树链剖分+线段树
原题链接 肯定是树链剖分的题啦 树剖怎么做可以看我上一篇博客 如果我们已经剖完了: 然后考虑怎么维护重链和查询 用线段树维护的时候当前区间的区间颜色个数应该等于左儿子+右儿子,但是当左儿子的右端点和右 ...
随机推荐
- 实例之HTML标签属性
鼠标放在图片上会显示说明文字的代码 <img src="图片地址" width=620 height=138 border=0 title="说明文字" ...
- CSS高级选择符
2016-11-07 <css入门经典>第八章 1.属性选择器 选择器 描述 [attribute] 用于选取带有指定属性的元素. [attribute=value] 用于选取带有指定属性 ...
- angularjs 指令—— 绑定策略(@、=、&)
angularjs 指令—— 绑定策略(@.=.&) 引入主题背景:angular 的指令配置中的template可以直接使用硬编码写相应的代码,不过也可以根据变量,进行动态更新.那么需要用到 ...
- Client JQuery invoke NetSuite Suitelet
Please indicate the source if you need to repost. Client jQuery could initialize a cross-domain requ ...
- MapGuide Maestro 5.1发布了
MapGuide Maestro最为MapGuide开源版的authoring工具真是发展迅速,有些功能比Infrastructure Studio还给力,现在5.1版已经发布了.大家可以到http: ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q66-Q68)
Question 66 You are designing an application that will use a timer job that will run each night to s ...
- 阿帕奇apache服务器和webDav服务器快速配置。
当自己在家敲代码需要发请求时,就可以配置本地apache,Mac电脑自带的服务器.这个比windows上的本地服务器还要好用,下面写下最快速配置方案. 0.在开始之前需要给自己的电脑设置下开机密码,想 ...
- 【代码笔记】iOS-等待动画
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- 【读书笔记】iOS网络-异步请求与运行循环
异步请求需要运行循环.当数据传递到服务器或是被客户端接收时,运行循环用于实现事件与委托对象之间的通信.异步请求在发出时,会在当前线程的运行循环上操作,这个实现细节是很重要的,因为在GCD块中或者是通过 ...
- iOS中的过期方法和新的替代方法
关于iOS中的过期方法和新的替代方法 1.获取某些类的UINavigationBar的统一外观并设置UINavigationbar的背景 注:方法名改了但是基本使用方法不变 + (instancety ...