bzoj4448 情报传递
离线+树上主席树,主席树维护时间标记
注意查询时如果c<0要把c赋为0;
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#define re(i,l,r) for(int i=(l);i<=(r);i++)
#define rre(i,r,l) for(int i=(r);i>=(l);i--)
using namespace std;
template <typename Q>
void inin(Q &ret)
{
ret=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<)+(ret<<)+ch-'',ch=getchar();
ret=f?-ret:ret;
}
const int xxx=;
int n,fa[xxx],w[xxx],head[xxx<<],next[xxx<<],zhi[xxx<<],ed;
void add(int a,int b)
{
next[++ed]=head[a],head[a]=ed,zhi[ed]=b;
next[++ed]=head[b],head[b]=ed,zhi[ed]=a;
}
namespace pou
{
int top[xxx],shen[xxx],son[xxx];
int dfs(int x)
{
int ret=,temp,Max=;
for(int i=head[x];i;i=next[i])if(zhi[i]!=fa[x])
{
shen[zhi[i]]=shen[x]+,fa[zhi[i]]=x;
temp=dfs(zhi[i]);ret+=temp;
if(Max<temp)Max=temp,son[x]=zhi[i];
}
return ret;
}
void dfs(int x,int t)
{
if(!x)return ;
top[x]=t;
dfs(son[x],t);
for(int i=head[x];i;i=next[i])if(zhi[i]!=fa[x]&&zhi[i]!=son[x])
dfs(zhi[i],zhi[i]);
}
int lca(int x,int y)
{
while(top[x]!=top[y])
if(shen[top[x]]>shen[top[y]])x=fa[top[x]];
else y=fa[top[y]];
return shen[x]<shen[y]?x:y;
}
}
namespace pst
{
int root[xxx],sum[xxx<<],ed,ch[xxx<<][];
void update(int l,int r,int L,int &R,int x)
{
R=++ed;sum[R]=sum[L]+(x?:);
if(!x){ch[R][]=ch[L][],ch[R][]=ch[L][];return ;}
if(l==r)return ;
int mid=(l+r)>>;
if(x<=mid)ch[R][]=ch[L][],update(l,mid,ch[L][],ch[R][],x);
else ch[R][]=ch[L][],update(mid+,r,ch[L][],ch[R][],x);
}
void build(int x)
{
update(,n,root[fa[x]],root[x],w[x]);
for(int i=head[x];i;i=next[i])if(zhi[i]!=fa[x])
build(zhi[i]);
}
int query(int l,int r,int x,int c)
{
if(c==)return ;
if(l>=&&r<=c)return sum[x];
int mid=(l+r)>>;
if(c<=mid)return query(l,mid,ch[x][],c);
else return query(l,mid,ch[x][],c)+query(mid+,r,ch[x][],c);
}
}
struct que
{
int x,y,c;
}sta[];int top;
int main()
{
inin(n);
re(i,,n)
{
int x;inin(x);
if(x)add(i,x);
}pou::shen[]=;
pou::dfs();
pou::dfs(,);
int q;inin(q);
re(i,,q)
{
int opt,x,y,c;inin(opt),inin(x);
if(opt==)inin(y),inin(c),sta[++top]=(que){x,y,i-c-};
else w[x]=w[x]?w[x]:i;
}
pst::build();
re(i,,top)
{
using namespace pou;
using namespace pst;
int temp=lca(sta[i].x,sta[i].y),x=sta[i].x,y=sta[i].y,c=sta[i].c;if(c<)c=;
printf("%d ",shen[sta[i].x]+shen[sta[i].y]-(shen[temp]<<)+);
printf("%d\n",query(,n,root[x],c)+query(,n,root[y],c)-*query(,n,root[fa[temp]],c)-(w[temp]<=c&&w[temp]>));
}
return ;
}
bzoj4448 情报传递的更多相关文章
- 【BZOJ4448】[Scoi2015]情报传递 主席树+LCA
[BZOJ4448][Scoi2015]情报传递 Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有n名情报员.每名情报员能有若干名(可能没有)下线,除1名大头 ...
- bzoj4448 SCOI2015 情报传递 message
传送门bzoj4448 题解 离线之后构建树上主席树,每个点的线段树维护到根路径的信息,不用链剖(我的链剖只是拿来求\(\mathrm{lca}\)的),时空复杂度\(O(n\log{n})\). c ...
- BZOJ4448 SCOI2015情报传递(离线+树链剖分+树状数组)
即滋磁单点修改,询问路径上小于某数的值有多少个.暴力树剖套个主席树(或者直接树上主席树,似乎就1个log了?感觉不一定比两个log快)即可,然而不太优美. 开始觉得可以cdq,然而就变成log^3了. ...
- BZOJ4448:[SCO2015]情报传递
题目大意:给你一棵树,有两种操作,一个是修改某个点的权值,另一个是询问两点之间的距离以及路径上小于某个值的数的个数. 询问两点之间距离直接lca即可,对于求个数的问题可以用主席树完成. #includ ...
- bzoj4448 [Scoi2015]情报传递
第一问不解释,对于第二问的处理,可以使用cdq分治,假设分治的询问区间是[L,R],那么我们对于标号在[L,mid]的修改操作赋予一个权值,因为在当前[L,R]中[L,mid]的修改操作只会对[mid ...
- 2019.03.26 bzoj4448: [Scoi2015]情报传递(归并排序+树链剖分)
传送门 题意简述: 给一棵nnn个点的树,树上每个点表示一个情报员,一共有mmm天,每天会派发以下两种任务中的一个任务: 1.搜集情报:指派T号情报员搜集情报 2.传递情报:将一条情报从X号情报员传递 ...
- BZOJ4448[Scoi2015]情报传递——主席树+LCA
题目描述 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有n名情报员.每名情报员口J-能有 若T名(可能没有)下线,除1名大头目外其余n-1名情报员有且仅有1名上线.奈特公司纪律森严 ...
- 【bzoj4448】SCOI2015 情报传递
树剖,可以相当于一个染色问题,以及询问先前节点的染色个数. 把第二种操作离线掉,然后用线段树模拟即可. #include<bits/stdc++.h> #define N 800010 # ...
- 【bzoj4448】[Scoi2015]情报传递 主席树
题目描述 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有n名情报员.每名情报员口J-能有若T名(可能没有)下线,除1名大头日外其余n-1名情报员有且仅有1名上线.奈特公司纪律森严, ...
随机推荐
- Centos安装elasticsearch教程
elasticsearch安装是ytkah在做laravel电商站内搜索要实现的,通过自己的搜索和学习能力不算很费力解决了.下面就整理一下安装elasticsearch教程,服务器是Centos 7, ...
- Spark partitionBy
partitionBy 重新分区, repartition默认采用HashPartitioner分区,自己设计合理的分区方法(比如数量比较大的key 加个随机数 随机分到更多的分区, 这样处理数据倾斜 ...
- 【Java】-NO.16.EBook.4.Java.1.006-【疯狂Java讲义第3版 李刚】- 垃圾回收
1.0.0 Summary Tittle:[Java]-NO.16.EBook.4.Java.1.006-[疯狂Java讲义第3版 李刚]- 垃圾回收 Style:EBook Series:Java ...
- 【LeetCode每天一题】Substring with Concatenation of All Words(具备列表中所有单词的字串)
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- 【LeetCode每天一题】Longest Common Prefix(最长前缀)
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- [Java in NetBeans] Lesson 00. Getting Set-up for Learning Java
这个课程的参考视频在youtube. 主要学到的知识点有: set up needs Java SE JDK, NetBeans IDE class name should be the same l ...
- 笔记:Python 字符串小记
1. 字符串是以''或""括起来的任意文本,如果'本身也是一个字符,那就可以用""括起来,比如"I'm OK" >>> p ...
- php 中 get_cfg_var() 与 ini_get() 的异同
背景 get_cfg_var() 取的值是配置文件中的值 ini_get() Gets the value of a configuration option, 则取的当前值(运行时,PHP系统定义) ...
- Java编写email实现内容换行
用java编写的发送邮件要使用<br>来实现换行,而不能使用\n,或者\r\n // 5. Content: 邮件正文(可以使用html标签)(内容有广告嫌疑,避免被邮件服务器误认为是滥发 ...
- Linux之HugePages快速配置
关于Linux系统的HugePages与Oracle数据库优化,可以参考熊爷之前的文章,相关概念介绍的非常清晰: Linux大内存页Oracle数据库优化 本文旨在Linux系统上快速配置HugePa ...