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 ...
随机推荐
- mysql定时器,定时查询数据库,把查询结果插入到一张表中
1.有两张表order_repayment_detail,msg_sim ,需要把前者中的按时间过滤出来的信息插入到短信发送表中,可以每晚12点钟查询执行一次查询. 创建存储过程,这里的存储过程主要提 ...
- Rsync+inotify自动同步数据
一.简介 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足. 首先,rsync在同步数据时,需要扫描所有文件后进行比对,进行差量传 ...
- 阿里C++研发实习二面和三面面经
下午连着面了阿里爸爸的二面和三面,非常不明白别人的三面都是hr了,为什么我还是在技术面,难道面了个假阿里.不管怎么样,来篇面经攒攒人品. 二面 第一次遇到这么严肃的面试官,居然可以全程无表情的,面了这 ...
- sql文件导入时出错
使用Navicat 连接工具连接mysql数据库. mysql数据库建立后,导入sql文件报错: [Err] 1064 - You have an error in your SQL syntax; ...
- 【C++ STL】Stack
1.定义 class stack<> 实作出一个stack(也成为LIFO,后进先出),你可以使用push()将任意数量的元素置入stack中,也可以使用pop()将元素依次插入次序反序从 ...
- 原生js addclass,hasClass,removeClass,toggleClass的兼容
(function (window) { 'use strict'; // class helper functions from bonzo https://github.com/ded/bonzo ...
- bzoj 1036: [ZJOI2008]树的统计Count——树链剖分
Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...
- 【BZOJ】1731: [Usaco2005 dec]Layout 排队布局
[题意]给定按编号顺序站成一排的牛,给定一些约束条件如两牛距离不小于或不大于某个值,求1和n的最大距离.无解输出-1,无穷解输出-2. [算法]差分约束+最短路 [题解]图中有三个约束条件,依次分析: ...
- Python代码这样写更优雅(转)
1.变量交换 大部分编程语言中交换两个变量的值时,不得不引入一个临时变量: >>> a = 1>>> b = 2>>> tmp = a>&g ...
- JavaWeb使用Session防止表单重复提交
在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 1.什么是表单 ...