hdu5893 List wants to travel(树链剖分+线段树)
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 270 Accepted Submission(s): 47
Can you help him? (He is so lazy to do this by himself.)
bidirectionoal road between city a and city b, and the cost is c.(a != b and c <= 100000). Then there are M lines of operation. For example, "Change a b c" means changing all the costs of the road which are passed by him when he travels from city a to city
b to c. "Query a b" means he wants you to tell him how many different kinds of continuous same cost he has to pay traveling from city a to city b.(if a == b, the cost is 0).
1 2 2
2 3 1
1 7 2
1 4 2
3 5 2
3 6 1
5 8 2
5 9 3
Query 1 8
Change 2 6 3
Query 1 6
2
题意:和bzoj2243题目几乎一样,区别在于bzoj颜色在点上,这题颜色在边上。
思路:先树链剖分,然后用线段树维护num(不同颜色的段数),cl(线段左端点的颜色),cr(线段右端点的颜色),然后因为处理的是线段,所以这里要把线段上的颜色当成这条线段连接的两个点深度更大的点的颜色,然后就和bzoj2243一样了。比赛的时候记得这题,但是因为是线段上的颜色,就不会了,亥,还是太弱了。。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
#define lson th<<1
#define rson th<<1|1
typedef long long ll;
typedef long double ldb;
#define inf 999999999
#define pi acos(-1.0)
#define maxn 40010
#define maxm 50050
struct edge{
int to,next,len;
}e[2*maxm];
int pos,tot,Lc,Rc;
int top[maxn],son[maxn],fa[maxn],dep[maxn],num[maxn],p[maxn],a[maxn],c[maxn],first[maxn];
void dfs1(int u,int pre,int deep)
{
int i,j,v;
dep[u]=deep;
fa[u]=pre;
num[u]=1;
for(i=first[u];i!=-1;i=e[i].next){
v=e[i].to;
if(v==pre)continue;
a[v]=e[i].len;
dfs1(v,u,deep+1);
num[u]+=num[v];
if(son[u]==-1 || num[son[u] ]<num[v]){
son[u]=v;
}
}
}
void dfs2(int u,int tp)
{
int i,j,v;
top[u]=tp;
if(son[u]!=-1){
p[u]=++pos;
c[pos]=a[u];
dfs2(son[u],tp);
}
else{
p[u]=++pos;
c[pos]=a[u];
return;
}
for(i=first[u];i!=-1;i=e[i].next){
v=e[i].to;
if(v==son[u] || v==fa[u])continue;
dfs2(v,v);
}
}
struct node{
int l,r,num,cl,cr,flag;
}b[4*maxn];
void pushup(int th)
{
b[th].cl=b[lson].cl;
b[th].cr=b[rson].cr;
b[th].num=b[lson].num+b[rson].num;
if(b[lson].cr==b[rson].cl)b[th].num--;
}
void pushdown(int th)
{
if(b[th].flag==1){
b[lson].flag=b[rson].flag=1;
b[lson].cl=b[lson].cr=b[rson].cl=b[rson].cr=b[th].cl;
b[lson].num=b[rson].num=1;
b[th].flag=-1;
}
}
void build(int l,int r,int th)
{
int mid;
b[th].l=l;b[th].r=r;
b[th].flag=-1;
if(l==r){
b[th].cl=b[th].cr=c[l];
b[th].num=1;
return;
}
mid=(b[th].l+b[th].r)/2;
build(l,mid,lson);
build(mid+1,r,rson);
pushup(th);
}
void update(int l,int r,int color,int th)
{
int mid;
if(b[th].l==l && b[th].r==r){
b[th].num=1;
b[th].flag=1;
b[th].cl=b[th].cr=color;
return;
}
pushdown(th);
mid=(b[th].l+b[th].r)/2;
if(r<=mid)update(l,r,color,lson);
else if(l>mid)update(l,r,color,rson);
else{
update(l,mid,color,lson);
update(mid+1,r,color,rson);
}
pushup(th);
}
int question(int l,int r,int th,int L,int R)
{
int mid,num;
if(b[th].l==L)Lc=b[th].cl;
if(b[th].r==R)Rc=b[th].cr;
if(b[th].l==l && b[th].r==r){
return b[th].num;
}
pushdown(th);
mid=(b[th].l+b[th].r)/2;
if(r<=mid)return question(l,r,lson,L,R);
else if(l>mid)return question(l,r,rson,L,R);
else{
num=question(l,mid,lson,L,R)+question(mid+1,r,rson,L,R);
if(b[lson].cr==b[rson].cl)num--;
return num;
}
}
int solve(int u,int v)
{
int f1,f2;
f1=top[u];f2=top[v];
int num=0,pre1,pre2;
pre1=pre2=-1;
while(f1!=f2){
if(dep[f1]<dep[f2]){
swap(pre1,pre2);swap(f1,f2);swap(u,v);
}
num+=question(p[f1],p[u],1,p[f1],p[u]);
if(pre1==Rc)num--;
pre1=Lc;
u=fa[f1];
f1=top[u];
}
if(u!=v){
if(dep[u]<dep[v]){
swap(pre1,pre2);swap(u,v);
}
num+=question(p[son[v]],p[u],1,p[son[v]],p[u]);
if(pre1!=-1 && Rc==pre1)num--;
if(pre2!=-1 && Lc==pre2)num--;
}
else if(pre1==pre2)num--;
return num;
}
void gengxin(int u,int v,int value)
{
int f1,f2;
f1=top[u];f2=top[v];
while(f1!=f2){
if(dep[f1]<dep[f2]){
swap(f1,f2);swap(u,v);
}
update(p[f1],p[u],value,1);
u=fa[f1];
f1=top[u];
}
if(dep[u]<dep[v]){
swap(u,v);
}
if(u!=v){
update(p[son[v]],p[u],value,1);
}
}
void add(int u,int v,int len)
{
tot++;
e[tot].next=first[u];e[tot].to=v;e[tot].len=len;
first[u]=tot;
}
int main()
{
int i,j,n,m,u,v,f,g,h,w;
char s[10];
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(first,-1,sizeof(first));
memset(son,-1,sizeof(son));
pos=0;tot=0;
for(i=1;i<=n-1;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);add(v,u,w);
}
dfs1(1,0,1);
dfs2(1,1);
build(1,pos,1);
for(i=1;i<=m;i++){
scanf("%s",s);
if(s[0]=='Q'){
scanf("%d%d",&f,&g);
printf("%d\n",solve(f,g));
}
else{
scanf("%d%d%d",&f,&g,&h);
gengxin(f,g,h);
}
}
}
return 0;
}
hdu5893 List wants to travel(树链剖分+线段树)的更多相关文章
- 【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 ...
随机推荐
- .NET斗鱼直播弹幕客户端(2021)
.NET斗鱼直播弹幕客户端(2021) 离之前更新的两篇<.NET斗鱼直播弹幕客户端>已经有一段时间,近期有许多客户向我反馈刚好有这方面的需求,但之前的代码不能用了--但网上许多流传的No ...
- 【Nginx】yum安装nginx
这里是nginx的yum安装源: centos7: rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-cent ...
- 【Linux】在docker上部署grafana+zabbix监控实录
------------------------------------------------------------------------------------------------- ...
- oracle创建恢复编录(recovery catalog)
1.在要作为恢复编录的数据库创建用户 create user rman identified by oracle default tablespace system temporary TABLESP ...
- 入门OJ:八中生成树2
题目描述 八中里面有N个建设物,M条边.对于这种要建最小生成树的问题,你应该很熟练了.现在老大决定降低某条边的费用,然后这条边必须要被选中,因为这条路他每天都要走,自然......问选了这条边后是否可 ...
- MySQL简介及安装 mysql Ver 14.14 Distrib 5.7.28
1.MySQL简介 1.数据库产品演变 第一代数据库架构: RDBMS 关系型数据库时代 : 合的时代 代表产品 :Oracle .MSSQL .MySQL.SQL server 第二代数据库架构:拆 ...
- 转 Fiddler5 发送HTTP请求
Fiddler5 发送HTTP请求 文章转自:https://www.cnblogs.com/zhengna/p/10879573.html 1.Fiddler Composer发送HTTP请求 C ...
- XShell下便捷上载/下载文件到虚拟机
1.客户机联网后,安装 rz,sz 服务,命令如下: yum install lrzsz 2.XShell连接客户机: 2.1 上传文件:运行rz,在弹窗内选择Windows本地文件上传到客户机当前目 ...
- git的使用学习笔记---分支删除
一.使用场景: 1.修改bug,原来分支不管用 2,分支太多不易管理 二.方法 git branch -d branch1 无法删除:原因在与该分支为目前工作的分支,所以要切换分支 git check ...
- 高性能缓存 Caffeine 原理及实战
一.简介 Caffeine 是基于Java 8 开发的.提供了近乎最佳命中率的高性能本地缓存组件,Spring5 开始不再支持 Guava Cache,改为使用 Caffeine. 下面是 Caffe ...