BZOJ [Scoi2015]情报传递
Description
Input
Output
Sample Input
0 1 1 2 2 3 3
6
1 4 7 0
2 1
2 4
2 7
1 4 7 1
1 4 7 3
Sample Output
5 2
5 1
HINT
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#define MAXN 201000
using namespace std;
struct tree{
int sum1,sum2,l,r;
}tr[MAXN*];
struct que{
int id,x,y,c,t,ans1,ans2;
}q1[MAXN];
struct que2{
int id,x,d;
}q2[MAXN];
struct edge{
int first,next,to;
}a[MAXN*];
int dep[MAXN],fa[MAXN],size[MAXN],son[MAXN],top[MAXN],pos[MAXN];
int n,q,num=,num2=,cnt=,cnt2=; void addedge(int from,int to){
a[++num].to=to;
a[num].next=a[from].first;
a[from].first=num;
} void dfs1(int now,int f){
fa[now]=f,size[now]=,dep[now]=dep[f]+;
for(int i=a[now].first;i;i=a[i].next){
int to=a[i].to;
if(to==f) continue;
dfs1(to,now);
size[now]+=size[to];
if(size[son[now]]<size[to]) son[now]=to;
}
} void dfs2(int now,int tp){
top[now]=tp,pos[now]=++num2;
if(son[now]) dfs2(son[now],tp);
for(int i=a[now].first;i;i=a[i].next){
int to=a[i].to;
if(to==fa[now]||to==son[now]) continue;
dfs2(to,to);
}
} bool cmp(que x,que y){if(x.t!=y.t)return x.t<y.t;else return x.id<y.id;}
bool cmp2(que2 x,que2 y){return x.id<y.id;}
bool cmp3(que x,que y){return x.id<y.id;} int query(int xv,int L,int R,int l,int r,int hh){
if(l==L&&r==R){
if(hh==) return tr[xv].sum1;
else return tr[xv].sum2;
}
int mid=(L+R)/;
if(r<=mid) return query(xv*,L,mid,l,r,hh);
else if(l>mid) return query(xv*+,mid+,R,l,r,hh);
else return query(xv*,L,mid,l,mid,hh)+query(xv*+,mid+,R,mid+,r,hh);
} void getans(int hh){
int x=q1[hh].x,y=q1[hh].y,topx=top[x],topy=top[y],ans1=,ans2=;
while(topx!=topy){
if(dep[topx]<dep[topy]) swap(topx,topy),swap(x,y);
ans1+=query(,,num2,pos[topx],pos[x],);
ans2+=query(,,num2,pos[topx],pos[x],);
x=fa[topx];
topx=top[x];
}
if(dep[x]<dep[y]) swap(x,y);
ans1+=query(,,num2,pos[y],pos[x],);
ans2+=query(,,num2,pos[y],pos[x],);
q1[hh].ans1=ans1,q1[hh].ans2=ans2;
} void change(int xv,int L,int R,int id,int hh){
if(L==R&&L==id){
if(hh==) tr[xv].sum1=;
else tr[xv].sum2=;
return;
}
int mid=(L+R)/;
if(id<=mid) change(xv*,L,mid,id,hh);
else change(xv*+,mid+,R,id,hh);
tr[xv].sum1=tr[xv*].sum1+tr[xv*+].sum1;
tr[xv].sum2=tr[xv*].sum2+tr[xv*+].sum2;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++){
int x;scanf("%d",&x);
addedge(i,x),addedge(x,i);
}
dfs1(,);dfs2(,);
scanf("%d",&q);
for(int i=;i<=q;i++){
int id;scanf("%d",&id);
if(id==){
int x,y,c;scanf("%d%d%d",&x,&y,&c);cnt++;
q1[cnt].x=x,q1[cnt].y=y,q1[cnt].c=c,q1[cnt].t=i-c,q1[cnt].id=i;
}
else{
int x;scanf("%d",&x);
q2[++cnt2].x=x;q2[cnt2].id=i;
}
}
sort(q1+,q1+cnt+,cmp);
sort(q2+,q2+cnt2+,cmp2);
int now=;
for(int i=;i<=n;i++) change(,,num2,pos[i],);
for(int i=;i<=cnt;i++){
while(now<=cnt2&&q2[now].id<q1[i].t){
change(,,num2,pos[q2[now].x],);now++;
}
getans(i);
}
sort(q1+,q1+cnt+,cmp3);
for(int i=;i<=cnt;i++){
printf("%d %d\n",q1[i].ans2,q1[i].ans1);
}
return ;
}
BZOJ [Scoi2015]情报传递的更多相关文章
- BZOJ 4448: [Scoi2015]情报传递 树链剖分 主席树
4448: [Scoi2015]情报传递 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4448 Description 奈特公司是一个巨 ...
- BZOJ_4448_[Scoi2015]情报传递_主席树
BZOJ_4448_[Scoi2015]情报传递_主席树 Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有n名情报员.每名情报员口J-能有 若T名(可能没有) ...
- 【BZOJ4448】[Scoi2015]情报传递 主席树+LCA
[BZOJ4448][Scoi2015]情报传递 Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有n名情报员.每名情报员能有若干名(可能没有)下线,除1名大头 ...
- [SCOI2015]情报传递[树剖+主席树]
[SCOI2015]情报传递 题意大概就是 使得在 \(i\) 时刻加入一个情报员帮您传情报 然后询问 \(x,y,c\) 指 \(x\)到\(y\)多少个人有风险-(大于c)的都有风险-每天风险值+ ...
- bzoj 4448 [Scoi2015]情报传递(主席树,LCA)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4448 [题意] 给定一颗树,询问一条路径上权值小于t-c的点数. [思路] 将一个2查 ...
- bzoj 4448: [Scoi2015]情报传递
Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有n名情报员.每名情报员口J-能有 若T名(可能没有)下线,除1名大头日外其余n-1名情报员有且仅有1名上线.奈 ...
- bzoj 4448 [Scoi2015]情报传递 (树链剖分+主席树)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4448 题面: Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络 ...
- 洛谷 4216 BZOJ 4448 [SCOI2015]情报传递
[题解] 每个情报员的危险值val[i]应该是一个分段函数,前面一段是平行于x轴的横线,后面一段是一次函数.我们可以用fx(t)=t-b[x]表示这个一次函数.每次询问一条链上fx(t)大于c的点的个 ...
- bzoj 4448 [Scoi2015]情报传递 主席树
比较套路的题目. 可以发现难点在于某个点的权值动态修改 且我们要维护树上一条路径上的点权>x的个数. 每个点都在动态修改 这意味着我们的只能暴力的去查每个点. 考虑将所有可以动态修改的点变成静态 ...
随机推荐
- 【Mac】快速复制文件路径
一.使用Automator新建服务 二.配置 三.使用 四.创建快捷键 一.使用Automator新建服务 在应用程序文件夹里打开Automator,选择文件菜单,新建一个服务,如下  二.配置 在 ...
- Mac环境IntelliJ IDEA配置JDK提示The selected directory is not a valid home for JDK
笔者使用mac配置如下: 硬件环境:MacBook Pro 操作系统:MacOS Sierra 10.13.6 开发工具:IntelliJ IDEA 2016.x JDK版本:已有Open JDK 8 ...
- Go操作kafka
Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据,具有高性能.持久化.多副本备份.横向扩展等特点.本文介绍了如何使用Go语言发送和接收kafka消息. s ...
- java多线程之ThreadPoolExecutor
ThreadPoolExecutor类 简介 java.uitl.concurrent.ThreadPoolExecutor类是线程池中最核心的一个类,因此如果要透彻地了解Java中的线程池,必须 ...
- mybatis拦截器实现通用权限字段添加
实现效果 日常sql中直接使用权限字段实现权限内数据筛选,无需入参,直接使用,使用形式为:select * from crh_snp.channelinfo where short_code in ( ...
- android 屏幕切换
1.将Activity固定位竖屏可以在配置文件这么写 <activity android:screenOrientation="portrait"> 横屏显示: < ...
- 从零开始构建Linux
目的:深入了解以Linux内核为基础的系统是如何组成,运行,以构建一个最基础的,纯净的系统. LFS构建步骤宿主机准备- linux操作系统安装- 使用独立硬盘,创建分区- 配置用户和组- 下载所有需 ...
- thinkphp6 常用方法文档
请求变量 use think\facade\Request; Request::param('name'); Request::param();全部请求变量 返回数组 Request::param([ ...
- 松软科技课堂:Winform之TextBox
松软科技文(www.sysoft.net.cn): 文本框的几种模式:Multiline(多行).PasswordChar(密码)将文本框的PasswordChar设为*就是密码框效果,将MultiL ...
- jenkins自动化部署项目9 --定时构建
举几个例子:每隔5分钟构建一次H/5 * * * * 每两小时构建一次H H/2 * * * 每天中午12点定时构建一次H 12 * * * 每天下午18点定时构建一次H 18 * * * 在每个小时 ...