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的个数. 每个点都在动态修改 这意味着我们的只能暴力的去查每个点. 考虑将所有可以动态修改的点变成静态 ...
随机推荐
- Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战
Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战 Springboot: 2.1.8.RELEASE SpringCloud: Greenwich.SR2 ...
- 搭建自己的技术博客系列(一)使用 hexo 搭建一个精美的静态博客
1.安装 Git 和 nodejs https://hexo.io/zh-cn/docs/
- AutoCompleteTextView自动完成文本框
AutoCompleteTextView是从EditText派生出来的,比普通编辑框多了一个功能,当用户输入一定字符后,自动完成文本框会显示一个下拉单,供用户选择,当选中一个后,被选中的内容会显示在文 ...
- FastReport安装包下载、安装、去除使用限制以及工具箱中添加控件
场景 FastReport .NET 2019是一款适用于Windows Forms, ASP.NET和MVC框架的功能齐全的报表分析解决方案.可用在Microsoft Visual Studio 2 ...
- Winforn中设置ZedGraph曲线图的属性、坐标轴属性、刻度属性
场景 C#窗体应用中使用ZedGraph曲线插件绘制图表: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99716066 在上面 ...
- CODESYS添加target
1.主界面进入Tools 2.Install,选择安装包
- ubuntu系统设置开机后使用使用终端运行应用程序
1.在菜单栏输入start,点击startup application进入 2. 添加开机启动程序 3.因为要用终端启动一个应用程序,所以command输入:gnome-terminal -- 程序执 ...
- C++ std::thread概念介绍
C++ 11新标准中,正式的为该语言引入了多线程概念.新标准提供了一个线程库thread,通过创建一个thread对象来管理C++程序中的多线程. 本文简单聊一下C++多线程相关的一些概念及threa ...
- Day 15 文件打包与压缩
1.什么是文件压缩? 将多个文件或目录合并成为一个特殊的文件.比如: 搬家...脑补画面 img. 2.为什么要对文件进行压缩? 当我们在传输大量的文件时,通常都会选择将该文件进行压缩,然后在进行传输 ...
- MAC sublime常用快捷键(慢慢补)
1、 FN + 左方向键:向左选择一行 2、FN + 右方向键:向右选择一行 3、FN + 上方向键:跳到页头 4、FN + 下方向键:跳到页尾 5、FN + SHIFT + 左方向键|上方向键:从当 ...