AC日记——[LNOI2014]LCA bzoj 3626
思路:
离线操作+树剖;
代码:
#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
#define maxm maxn<<2
#define ll long long
#define mod 201314
struct QueryType {
int now,id,pos,z;
bool operator<(const QueryType pos)const
{
return now<pos.now;
}
};
struct QueryType qu[maxn<<];
int n,m,f[maxn],top[maxn],lar[maxn],size[maxn],id[maxn],deep[maxn];
int L[maxm],R[maxm],mid[maxm],head[maxn],E[maxn],V[maxn],cnt;
ll dis[maxm],ans[maxn],flag[maxm],Qes;
inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'')Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
}
void dfs1(int now)
{
size[now]=,deep[now]=deep[f[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;
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]);
}
}
void build(int now,int l,int r)
{
L[now]=l,R[now]=r;if(l==r) return;mid[now]=l+r>>;
build(now<<,l,mid[now]),build(now<<|,mid[now]+,r);
}
void downdata(int now)
{
flag[now<<]+=flag[now],flag[now<<|]+=flag[now];
dis[now<<]+=flag[now]*(R[now<<]-L[now<<]+);
dis[now<<|]+=flag[now]*(R[now<<|]-L[now<<|]+);
flag[now]=;
}
void add(int now,int l,int r)
{
if(L[now]>=l&&R[now]<=r)
{
dis[now]+=R[now]-L[now]+,flag[now]+=;
return;
}
if(flag[now]) downdata(now);
if(l<=mid[now]) add(now<<,l,r);
if(r>mid[now]) add(now<<|,l,r);
dis[now]=dis[now<<]+dis[now<<|];
}
void query(int now,int l,int r)
{
if(L[now]>=l&&R[now]<=r)
{
Qes+=dis[now];return;
}
if(flag[now]) downdata(now);
if(l<=mid[now]) query(now<<,l,r);
if(r>mid[now]) query(now<<|,l,r);
}
void add(int x,int y)
{
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) add(,id[top[y]],id[y]),y=f[top[y]];
else add(,id[top[x]],id[x]),x=f[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);add(,id[x],id[y]);
}
ll query(int x,int y)
{
ll res=;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]])
{
Qes=,query(,id[top[y]],id[y]);
res+=Qes,y=f[top[y]];
}
else
{
Qes=,query(,id[top[x]],id[x]);
res+=Qes,x=f[top[x]];
}
}
if(deep[x]>deep[y]) swap(x,y);
Qes=,query(,id[x],id[y]);
return res+Qes;
}
int main()
{
freopen("data.txt","r",stdin);
in(n),in(m);int u,v,w;
for(int i=;i<=n;i++)
{
in(u),u++,f[i]=u;
E[++cnt]=head[u],V[cnt]=i,head[u]=cnt;
}
cnt=,dfs1(),dfs2(,),build(,,n),cnt=;
for(int i=;i<=m;i++)
{
in(u),in(v),in(w),w++;
qu[++cnt].now=u,qu[cnt].id=i,qu[cnt].pos=-,qu[cnt].z=w;
qu[++cnt].now=v,qu[cnt].id=i,qu[cnt].pos=,qu[cnt].now++,qu[cnt].z=w;
}
sort(qu+,qu+cnt+);int tot=;
for(int i=;i<=cnt;i++)
{
if(qu[i].now==) continue;
while(tot<qu[i].now) add(++tot,);
ans[qu[i].id]+=query(qu[i].z,)*qu[i].pos;
}
for(int i=;i<=m;i++) printf("%lld\n",ans[i]%mod);
return ;
}
AC日记——[LNOI2014]LCA bzoj 3626的更多相关文章
- AC日记——[HNOI2014]世界树 bzoj 3572
3572 思路: 虚树+乱搞: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 #define ...
- AC日记——[Sdoi2013]森林 bzoj 3123
3123: [Sdoi2013]森林 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 3216 Solved: 944[Submit][Status] ...
- AC日记——[Hnoi2017]影魔 bzoj 4826
4826 思路: 主席树矩阵加减+单调栈预处理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 ...
- AC日记——[ZJOI2012]网络 bzoj 2816
2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define l ...
- AC日记——[SCOI2009]游戏 bzoj 1025
[SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...
- AC日记——NOI2016区间 bzoj 4653
4653 思路: 线段树,指针滑动: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1000005 #def ...
- AC日记——Rmq Problem bzoj 3339
3339 思路: 恶心: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...
- AC日记——[HNOI2008]越狱 bzoj 1008
1008 思路: 越狱情况=总情况-不越狱情况: 代码: #include <cstdio> #include <cstring> #include <iostream& ...
- AC日记——[FJOI2007]轮状病毒 bzoj 1002
1002 思路: 打表找规律: dp[i]=dp[i-1]*3-dp[i-2]+2; 套个高精就a了: 代码: #include <cstdio> #include <cstring ...
随机推荐
- LVM分区
使用LVM对磁盘进行初始化 pvcreate /dev/vdd 创建卷组 vgcreate vg /dev/vdd 备注:vg是卷组的名称,可改变. 查看卷组的详细信息 vgdisplay 下图是我执 ...
- JNA的用法
JNA(Java Native Access):建立在JNI之上的Java开源框架,SUN主导开发,用来调用C.C++代码,尤其是底层库文件(windows中叫dll文件,linux下是so[shar ...
- 搭建openresty需要注意到的地方
openresty的完整包放在百度云盘linux目录下 一键安装openresty ./install.sh 安装好后,修改nginx.conf配置文件 cd /usr/local/openresty ...
- STL之五:set/multiset用法详解
集合 转载于:http://blog.csdn.net/longshengguoji/article/details/8546286 使用set或multiset之前,必须加入头文件<set&g ...
- Codeforces Round #340 (Div. 2)B
B. Chocolate time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- [mysql]数据库引擎查看
1.查看数据库引擎 全局下,show engines; 2.察看数据库引擎 show variables like '%engine%'; 或者show create table xxx\G 会显示默 ...
- solr集群安装部署
一.安装部署zookeeper集群 zookeeper集群 二.solr集群部署 集群配置 IP | 节点名称 | 环境 --- | --- | --- 192.168.137.128 | 192.1 ...
- uboot下的命令行
1.典型嵌入式linux系统启动过程: 嵌入式系统上电后先执行uboot.然后uboot负责初始化DDR,初始化Flash,然后将OS从Flash中读取到DDR中,然后启动OS(OS启动后uboot就 ...
- notify()与notifyAll()
notify() :随机唤醒一个线程. notifyAll():唤醒等待某个锁的所有任务. 在技术上,可能会有多个任务在所创建的任务上处于wait()状态,调用notifyAll()比只调用notif ...
- centos6.5 配置mongodb3
下载地址 http://www.mongodb.org/downloads 下载 curl -O -L https://fastdl.mongodb.org/linux/mongodb-linux-i ...