#2011. 「SCOI2015」情报传递

思路:

  可持久化树状数组模板;

代码:

#include <bits/stdc++.h>
using namespace std;
#define maxn 200005
#define maxm maxn*100
int deep[maxn],f[maxn],id[maxn],top[maxn],cnt,soot;
int head[maxn],V[maxn],E[maxn],lar[maxn],size[maxn];
int val[maxm],root[maxn],ch[maxm][],tot,Tl,Tr,n,m,Ans;
inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'')Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
}
void build(int &now,int l,int r)
{
now=++tot;
if(l==r) return;int mid=l+r>>;
build(ch[now][],l,mid);
build(ch[now][],mid+,r);
}
void add(int &now,int pre,int l,int r,int to,int x)
{
now=++tot,val[now]=val[pre]+x;
if(l==r) return;int mid=l+r>>;
if(to<=mid) add(ch[now][],ch[pre][],l,mid,to,x),ch[now][]=ch[pre][];
else add(ch[now][],ch[pre][],mid+,r,to,x),ch[now][]=ch[pre][];
}
int lowbit(int x)
{
return x&(-x);
}
void add(int to1,int to2,int x)
{
while(to1<=n) add(root[to1],root[to1],,m,to2,x),to1+=lowbit(to1);
}
void dfs1(int now)
{
deep[now]=deep[f[now]]+,size[now]=;
for(int i=head[now];i;i=E[i])
{
dfs1(V[i]),size[now]+=size[V[i]];
if(size[lar[now]]<size[V[i]]) lar[now]=V[i];
}
}
void dfs2(int now,int chain)
{
top[now]=chain,id[now]=++cnt;
root[id[now]]=root[];
if(lar[now])
{
dfs2(lar[now],chain);
for(int i=head[now];i;i=E[i])
{
if(V[i]==lar[now]) continue;
dfs2(V[i],V[i]);
}
}
}
int query(int now,int l,int r)
{
if(l>=Tl&&r<=Tr) return val[now];
int mid=l+r>>,res=;
if(Tl<=mid) res+=query(ch[now][],l,mid);
if(Tr>mid) res+=query(ch[now][],mid+,r);
return res;
}
int query(int l,int r)
{
l--;int res=;
while(r) res+=query(root[r],,m),r-=lowbit(r);
while(l) res-=query(root[l],,m),l-=lowbit(l);
return res;
}
int query(int x,int y,int tl,int tr)
{
int res=;Tl=tl,Tr=tr;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) res+=query(id[top[y]],id[y]),y=f[top[y]];
else res+=query(id[top[x]],id[x]),x=f[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
res+=query(id[x],id[y]);
return res;
}
int lca(int x,int y)
{
while(top[x]!=top[y])
{
if(deep[top[x]]>deep[top[y]]) x=f[top[x]];
else y=f[top[y]];
}
return deep[x]<deep[y]?deep[x]:deep[y];
}
int main()
{
freopen("data.txt","r",stdin);
freopen("data.out","w",stdout);
in(n);int op,u,v,c;
for(int i=;i<=n;i++)
{
in(f[i]);
if(!f[i]) soot=i;
else E[i]=head[f[i]],V[i]=i,head[f[i]]=i;
}
in(m);
build(root[],,m),dfs1(soot),dfs2(soot,soot);
for(int i=;i<=m;i++)
{
in(op);
if(op==)in(u),add(id[u],i,);
else
{
in(u),in(v),in(c),Ans=deep[u]+deep[v]-*lca(u,v)+;
printf("%d ",Ans);
if(i-c->=) printf("%d\n",query(u,v,,i-c-));
else printf("0\n");
}
}
return ;
}

AC日记——「SCOI2015」情报传递 LiBreOJ 2011的更多相关文章

  1. AC日记——「SCOI2015」国旗计划 LiBreOJ 2007

    #2007. 「SCOI2015」国旗计划 思路: 跪烂Claris 代码: #include <cstdio> #include <algorithm> #define ma ...

  2. AC日记——「SCOI2015」小凸玩矩阵 LiBreOJ 2006

    「SCOI2015」小凸玩矩阵 思路: 二分+最大流: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300 ...

  3. AC日记——「HNOI2017」礼物 LiBreOJ 2020

    #2020. 「HNOI2017」礼物 思路: A题进程: 一眼出式子->各种超时过不去->看题解明白还有fft这个东西->百度文库学习fft->学习dft->学习fft ...

  4. AC日记——「SCOI2016」美味 LiBreOJ 2016

    #2016. 「SCOI2016」美味 思路: 主席树: 代码: #include <bits/stdc++.h> using namespace std; #define maxa 26 ...

  5. 「SCOI2015」情报传递

    「SCOI2015」情报传递 题目描述 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有 \(n\) 名情报员.每名情报员可能有若干名(可能没有)下线,除 \(1\) 名大头目外其余 ...

  6. AC日记——「HNOI2017」单旋 LiBreOJ 2018

    #2018. 「HNOI2017」单旋 思路: set+线段树: 代码: #include <bits/stdc++.h> using namespace std; #define max ...

  7. AC日记——「SCOI2016」幸运数字 LiBreOJ 2013

    「SCOI2016」幸运数字 思路: 线性基: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 20005 # ...

  8. AC日记——「SCOI2016」背单词 LiBreOJ 2012

    #2012. 「SCOI2016」背单词 思路: Orz: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1 ...

  9. AC日记——「SDOI2017」序列计数 LibreOJ 2002

    「SDOI2017」序列计数 思路: 矩阵快速幂: 代码: #include <bits/stdc++.h> using namespace std; #define mod 201704 ...

随机推荐

  1. Better Linux Disk Caching & Performance with vm.dirty_ratio & vm.dirty_background_ratio

    In previous posts on vm.swappiness and using RAM disks we talked about how the memory on a Linux gue ...

  2. JQuery学习六

    <JQuery cookie>插件 cookie是保存在浏览器上的内容,用户在这次浏览页面的时候向cookie中保存文本内容.下次再访问页面的时侯就可以取出来上次保存的内容.这样可以得到上 ...

  3. Leetcode 557. 反转字符串中的单词 III

    1.题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" ...

  4. session 超时设置

    Java Web开发Session超时设置 博客分类: Java Web 在Java Web开发中,Session为我们提供了很多方便,Session是由浏览器和服务器之间维护的.Session超时理 ...

  5. windows下codeblocks报错undefined reference to `WSAStartup@8'|

    Windows下C++Socket编程,调用WSAStartup函数报错:undefined reference to `WSAStartup@8'| 本人使用的是Codeblocks MinGW M ...

  6. HDU 4609 FFT模板

    http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:给你n个数,问任意取三边能够,构成三角形的概率为多少. 思路:使用FFT对所有长度的个数进行卷积(\ ...

  7. sqlserver 个人整理

    1.创建临时表(临时表的生命周期是当前回话中) //创建临时表是使用#+表名 //判断临时表是否存在 if object_id('tempdb..#ttt1') is not null Begin d ...

  8. c# 自定义排序Compare

    .net FrameWork 框架博大精深,用着忘着,计划对自己能够想到知识点梳理一下,此篇是对自定义排序的理解: class Program { static void Main(string[] ...

  9. Robot Framework Change chrome language

    由于open browser的参数只有一个ff_profile_dir,所以不能指定chrome profile. 只能通过python 传递lang这个参数去改变语言. python: from s ...

  10. ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to javax.websocket.server.ServerContainer

    21:09:22.221 [MessageBroker-3] INFO c.t.s.s.impl.StockNewsServiceImpl - [2017-12-16 21:09:22] execut ...