题解:动态点分治

建立点分树

每个点维护点分树子树内节点到这个节点和父亲节点距离的前缀和

二分查找锁定合法区间

对每个祖先分治中心查询路径和然后减去不合法子树内的路径和

注意:求大量LCA时用树剖

不开O2时少用STL

相乘炸int

lower_bound和upper_bound返回值边界

注意常数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=200009;
const int oo=1000000000;
typedef long long Lint; int n,T,u;
int a[maxn]; int cntedge;
int head[maxn];
int to[maxn<<1],nex[maxn<<1],dist[maxn<<1];
void Addedge(int x,int y,int z){
nex[++cntedge]=head[x];
to[cntedge]=y;
dist[cntedge]=z;
head[x]=cntedge;
} int nowsiz,root;
int vis[maxn];
int siz[maxn],g[maxn];
void Getsiz(int x,int fa){
siz[x]=1;
for(int i=head[x];i;i=nex[i]){
if(vis[to[i]])continue;
if(to[i]==fa)continue;
Getsiz(to[i],x);
siz[x]+=siz[to[i]];
}
}
void Getroot(int x,int fa){
siz[x]=1;
g[x]=0;
for(int i=head[x];i;i=nex[i]){
if(vis[to[i]])continue;
if(to[i]==fa)continue;
Getroot(to[i],x);
siz[x]+=siz[to[i]];
g[x]=max(g[x],siz[to[i]]);
}
g[x]=max(g[x],nowsiz-siz[x]);
if(g[x]<g[root])root=x;
} int divfa[maxn];
void Sol(int x){
vis[x]=1;
Getsiz(x,0);
for(int i=head[x];i;i=nex[i]){
if(vis[to[i]])continue;
root=0;nowsiz=siz[to[i]];
Getroot(to[i],x);
divfa[root]=x;
Sol(root);
}
} int father[maxn],dep[maxn],d[maxn],top[maxn],hson[maxn];
void Dfs(int x,int fa){
father[x]=fa;
dep[x]=dep[fa]+1;
siz[x]=1;
for(int i=head[x];i;i=nex[i]){
if(to[i]==fa)continue;
d[to[i]]=d[x]+dist[i];
Dfs(to[i],x);
siz[x]+=siz[to[i]];
if(siz[to[i]]>siz[hson[x]])hson[x]=to[i];
}
}
void Dfs2(int x,int toppoint){
top[x]=toppoint;
if(!hson[x])return;
Dfs2(hson[x],toppoint);
for(int i=head[x];i;i=nex[i]){
if(to[i]==father[x])continue;
if(to[i]==hson[x])continue;
Dfs2(to[i],to[i]);
}
} int Getlca(int u,int v){
int tu=top[u];
int tv=top[v];
while(tu!=tv){
if(dep[tu]<dep[tv]){
swap(u,v);
swap(tu,tv);
}
u=father[tu];
tu=top[u];
}
if(dep[u]<dep[v])return u;
else return v;
} int Getdist(int x,int y){
if((x==0)||(y==0))return 0;
int lca=Getlca(x,y);
return d[x]+d[y]-d[lca]-d[lca];
} vector<int>G[maxn];
vector<Lint>S[maxn];
vector<Lint>S2[maxn]; int cmp(const int &rhs1,const int &rhs2){
return a[rhs1]<a[rhs2];
} int main(){
scanf("%d%d%d",&n,&T,&u);
for(int i=1;i<=n;++i)scanf("%d",&a[i]);
for(int i=1;i<n;++i){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
Addedge(x,y,z);
Addedge(y,x,z);
} Dfs(1,0);
Dfs2(1,1); g[0]=oo;root=0;nowsiz=n;
Getroot(1,0);
Sol(root); for(int i=1;i<=n;++i){
int x=i;
while(x){
G[x].push_back(i);
x=divfa[x];
}
} // for(int i=1;i<=n;++i){
// for(int j=1;j<=n;++j){
// cout<<Getdist(i,j)<<' ';
// }
// cout<<endl;
// }
for(int i=1;i<=n;++i){
sort(G[i].begin(),G[i].end(),cmp);
Lint now=0,now2=0;
for(int j=0;j<G[i].size();++j){
now=now+Getdist(i,G[i][j]);
now2=now2+Getdist(divfa[i],G[i][j]);
S[i].push_back(now);
S2[i].push_back(now2);
G[i][j]=a[G[i][j]];
}
// printf("=->%d %d %d\n",i,now,now2);
}
// for(int i=1;i<=n;++i)cout<<d[i]<<' ';
// cout<<endl;
Lint ans=0;
while(T--){
int x,y,Lold,Rold;
scanf("%d%d%d",&x,&Lold,&Rold);
y=x;
int t1=(Lold+ans)%u;
int t2=(Rold+ans)%u;
Lold=min(t1,t2);
Rold=max(t1,t2);
// cout<<Lold<<' '<<Rold<<endl;
ans=0;
int p=0;
// cout<<ans<<endl;
// for(int i=0;i<G[x].size();++i)cout<<G[x][i]<<' ';
// cout<<endl;
p=upper_bound(G[x].begin(),G[x].end(),Rold)-G[x].begin()-1;
if(p>-1)ans+=S[x][p];
// cout<<p<<endl;
p=lower_bound(G[x].begin(),G[x].end(),Lold)-G[x].begin();
if(p)ans-=S[x][p-1];
// cout<<p<<endl;
// cout<<ans<<endl;
while(divfa[x]){
Lint fdist=Getdist(divfa[x],y);
int fa=divfa[x]; p=upper_bound(G[fa].begin(),G[fa].end(),Rold)-G[fa].begin()-1;
if(p>-1)ans+=S[fa][p]+fdist*(p+1);
// cout<<p<<' ';
p=lower_bound(G[fa].begin(),G[fa].end(),Lold)-G[fa].begin();
if(p)ans-=S[fa][p-1]+fdist*p;
// cout<<p<<endl;
// cout<<ans<<endl;
p=upper_bound(G[x].begin(),G[x].end(),Rold)-G[x].begin()-1;
if(p>-1)ans-=S2[x][p]+fdist*(p+1);
p=lower_bound(G[x].begin(),G[x].end(),Lold)-G[x].begin();
if(p)ans+=S2[x][p-1]+fdist*p;
// cout<<ans<<endl;
x=divfa[x];
}
printf("%lld\n",ans);
}
return 0;
}

  

P3241 [HNOI2015]开店的更多相关文章

  1. 洛谷 P3241 [HNOI2015]开店 解题报告

    P3241 [HNOI2015]开店 题目描述 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱. 这样的想法当然非 ...

  2. P3241 [HNOI2015]开店 动态点分治

    \(\color{#0066ff}{ 题目描述 }\) 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱. 这样的想 ...

  3. luogu P3241 [HNOI2015]开店

    传送门 (下面记年龄为\(a_x\))题目要求的是\[\sum_{x=1}^{n} [a_x\in [l,r]]*dis(x,u)=\sum_{x=1}^{n} [a_x\in [l,r]]*de_x ...

  4. 并不对劲的bzoj4012:loj2116:p3241: [HNOI2015]开店

    题目大意 有一棵\(n\)(\(n\leq1.5*10^5\))个节点的二叉树,有点权\(x\),边权\(w\),\(q\)(\(q\leq2*10^5\))组询问,每组询问给出\(u,l,r\),求 ...

  5. [HNOI2015]开店 树链剖分,主席树

    [HNOI2015]开店 LG传送门 蒟蒻表示不会动态淀粉质. 先把点按年龄排序, 设\(dis[i]\)表示\(i\)到根的距离. 把我们要算的东西稍微变下形:\(ans\) \[ = \sum \ ...

  6. [BZOJ4012][HNOI2015]开店(动态点分治,树链剖分)

    4012: [HNOI2015]开店 Time Limit: 70 Sec  Memory Limit: 512 MBSubmit: 2168  Solved: 947[Submit][Status] ...

  7. 【BZOJ4012】[HNOI2015]开店 动态树分治+二分

    [BZOJ4012][HNOI2015]开店 Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点 ...

  8. BZOJ4012 [HNOI2015]开店

    Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现 ...

  9. bzoj 4012: [HNOI2015]开店

    Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现 ...

随机推荐

  1. vue的MVVM

    Vue的相关知识有 字符串模板 MVVM 虚拟dom和domdiff,查看下一篇笔记 字符串模板 function render(template, data) { const reg = /\{\{ ...

  2. Centos 8下命令行界面支持鼠标

    yum install gpm* service gpm start systemctl enable gpm.service

  3. HDU - 6130 Kolakoski (打表)

    题意:由1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1,……合并可得1,22,11,2,1,22,1,22,11,2,11,22,1,再由每个数的位数可得新序列,推出新 ...

  4. C# OBJ模型解析的封装(网上看到的保留一份)

    /// <author>Lukas Eibensteiner</author> /// <date>19.02.2013</date> /// < ...

  5. ubuntu下pip的安装、升级和使用

    系统虽然自带了不同版本的python,但都没有安装pip,pyhton2.7下使用的是pip2,python3.5下使用的是pip3.下面是各自安装命令. 安装 pip2: sudo apt-get ...

  6. 对plotTree的解释

    1.>>>a = 1/2/2   >>>a >>>0.25 2.def plotMidText(cntrPt,parentPt,txtString ...

  7. JavaWeb开发校园二手平台项目 源码

    开发环境: Windows操作系统开发工具:MyEclipse/Eclipse + JDK+ Tomcat + MySQL 数据库 项目简介: JAVAWEB校园二手平台项目,基本功能包括:个人信息. ...

  8. 107-PHP类成员属性赋值

    <?php class mao{ //定义猫类 public $age; //定义多个成员属性 protected $weight; private $color; } $mao1=new ma ...

  9. eclipse 项目启动不了问题

    有可能是因为dubugger 打多了,所有启动不起来 解决方案:window——show view ——other....——breakpoints去除所有断点

  10. Node.js 发送Email

    章节 Node.js 介绍 Node.js 入门 Node.js 模块 Node.js HTTP模块 Node.js 文件系统模块 Node.js URL模块 Node.js NPM Node.js ...