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的更多相关文章

  1. AC日记——[HNOI2014]世界树 bzoj 3572

    3572 思路: 虚树+乱搞: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 #define ...

  2. AC日记——[Sdoi2013]森林 bzoj 3123

    3123: [Sdoi2013]森林 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 3216  Solved: 944[Submit][Status] ...

  3. AC日记——[Hnoi2017]影魔 bzoj 4826

    4826 思路: 主席树矩阵加减+单调栈预处理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 ...

  4. AC日记——[ZJOI2012]网络 bzoj 2816

    2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define l ...

  5. AC日记——[SCOI2009]游戏 bzoj 1025

    [SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...

  6. AC日记——NOI2016区间 bzoj 4653

    4653 思路: 线段树,指针滑动: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1000005 #def ...

  7. AC日记——Rmq Problem bzoj 3339

    3339 思路: 恶心: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...

  8. AC日记——[HNOI2008]越狱 bzoj 1008

    1008 思路: 越狱情况=总情况-不越狱情况: 代码: #include <cstdio> #include <cstring> #include <iostream& ...

  9. AC日记——[FJOI2007]轮状病毒 bzoj 1002

    1002 思路: 打表找规律: dp[i]=dp[i-1]*3-dp[i-2]+2; 套个高精就a了: 代码: #include <cstdio> #include <cstring ...

随机推荐

  1. BZOJ1878: [SDOI2009]HH的项链 (离线查询+树状数组)

    1878: [SDOI2009]HH的项链 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1878 Description: HH有一串由 ...

  2. Codeforces Round #539 (Div. 2) 题解

    Codeforces Round #539 (Div. 2) 题目链接:https://codeforces.com/contest/1113 A. Sasha and His Trip 题意: n个 ...

  3. Android数据库资料

    一.联系人和通话记录: 数据库文件/data/data/com.android.providers.contacts/databases/contacts2.db  通话记录的数据存在calls表中; ...

  4. 【题解】Matrix BZOJ 4128 矩阵求逆 离散对数 大步小步算法

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4128 大水题一道 使用大步小步算法,把数字的运算换成矩阵的运算就好了 矩阵求逆?这么基础的线 ...

  5. bzoj 1135 [POI2009]Lyz 线段树+hall定理

    1135: [POI2009]Lyz Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 573  Solved: 280[Submit][Status][ ...

  6. HDU1384 差分约束

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. ios开关按钮

    .al-toggle-button{ appearance: none; -webkit-appearance: none; position: relative; width: 52px; heig ...

  8. Reduce Side Join实现

    关于reduce边join,其最重要的是使用MultipleInputs.addInputPath这个api对不同的表使用不同的Map,然后在每个Map里做一下该表的标识,最后到了Reduce端再根据 ...

  9. Caused by: java.io.IOException: Filesystem closed的处理

    org.apache.hadoop.hive.ql.metadata.HiveException: Unable to rename output from: hdfs://nameservice/u ...

  10. 编辑器vi命令

    代码: # vi + 文件名 //将光标放在文档最下面 进入编辑器后: i:插入 x:删除 w:保存 q:退出不保存 q!:强制退出不保存 wq:保存并退出