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的个数. 每个点都在动态修改 这意味着我们的只能暴力的去查每个点. 考虑将所有可以动态修改的点变成静态 ...
随机推荐
- yolo进化史之yolov2
yolov1和当时最好的目标检测系统相比,有很多缺点.比如和Fast R-CNN相比,定位错误更多.和基于区域选择的目标检测方法相比,recall也比较低.yolov2的目标即在保证分类准确度的情况下 ...
- 纯JS实现在一个字符串b中查找另一个字符串a出现的所有位置,并且不使用字符串的方法(递归)
问题:判断字符串A在中所有出现字符串B中(长度大于1)的索引.不得使用字符串方法indexof,substring等 有小伙伴在面试遇到了这个问题,乍一看如果使用使用字符串方法indexof,subs ...
- Go语言基础之指针
区别于C/C++中的指针,Go语言中的指针不能进行偏移和运算,是安全指针. 要搞明白Go语言中的指针需要先知道3个概念:指针地址.指针类型和指针取值. Go语言中的指针 Go语言中的函数传参都是值拷贝 ...
- KafkaProducer源码分析
Kafka常用术语 Broker:Kafka的服务端即Kafka实例,Kafka集群由一个或多个Broker组成,主要负责接收和处理客户端的请求 Topic:主题,Kafka承载消息的逻辑容器,每条发 ...
- UGUI_创建旋转物体,使用Slider控制小球旋转速度
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : ...
- 网络编程之socket模块
一.TCP协议 TCP是可靠的.面向连接的协议(eg:打电话).传输效率低全双工通信(发送缓存&接收缓存).面向字节流.使用TCP的应用:Web浏览器:电子邮件.文件传输程序. 二.基于TCP ...
- 关于ClassLoader的使用
了解ClassLoader: /* * 了解类的加载器:ClassLoader * */ @Test public void testClassLoader() { // 获取到系统类加载器,加载用户 ...
- C# 10分钟入门基于WebOffice实现在线编辑文档,实时保存到服务器(所有office,兼容WPS)
今天,他来了(weboffice在线编辑文档). 上次写了一个在线预览的博,当然,效果并不是太理想,但是紧急解决了当时的问题. 后来,小编重新查找资料,求助大牛,终于使用新的方式替换了之前的low方法 ...
- [Linux][函数]flock函数的用法
表头文件 #include<sys/file.h> 定义函数 int flock(int fd,int operation); 函数说明 flock()会依参数operation所指 ...
- mysql按关键词截取字符串
按关键字截取字符串 :substring_index(被截取字段,关键字,关键字出现的次数) eg:字符串:test.docx:test2.zip substring_index(file_name, ...