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

##### 代码

#include<algorithm>
#include<cmath>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define maxn 150010
#define maxm (maxn<<1)
#define view(u,k) for(int k=fir[u];k!=-1;k=nxt[k])
#define LL long long
#define ls son[u][0]
#define rs son[u][1]
#define mi (l+r>>1)
using namespace std;
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
void write(LL x)
{
if(x==0){putchar('0'),putchar('\n');return;}
int f=0;char ch[20];
if(x<0)putchar('-'),x=-x;
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
putchar('\n');
return;
}
struct node{int x,k;}s[maxn];
int n,q,ag[maxn],vis[maxm],siz[maxn],fir[maxn],v[maxm],nxt[maxm],tp,cnt,wt,mnsz,nowsiz,st[20][maxm];
int lg[maxm],dfn[maxn],rt[maxn<<3],nd,ql,qr,son[maxn<<5][2],num[maxn<<5],to[maxn<<3],tim,bac[maxn],trsiz[maxn<<5],tmp;
LL w[maxm],dep[maxn],sum[maxn<<5],key[maxn<<5],dis[maxn],A,ans;
bool cmp(node x,node y){return x.k<y.k;}
void ade(int u1,int v1,int w1){v[cnt]=v1,w[cnt]=w1,nxt[cnt]=fir[u1],fir[u1]=cnt++;}
int LCA(int x,int y)
{
x=dfn[x],y=dfn[y];
if(x>y)swap(x,y);
int len=y-x+1;
return dis[st[lg[len]][x]]<dis[st[lg[len]][y-(1<<lg[len])+1]]?st[lg[len]][x]:st[lg[len]][y-(1<<lg[len])+1];
}
LL dist(int x,int y)
{
int lca=LCA(x,y);
return dis[x]+dis[y]-dis[lca]-dis[lca];
}
void getsiz(int u,int fa)
{
siz[u]=1;s[++tp].k=ag[u],s[tp].x=dep[u];
view(u,k)if(!vis[k]&&!vis[k^1]&&v[k]!=fa)
{
dep[v[k]]=dep[u]+w[k],getsiz(v[k],u),siz[u]+=siz[v[k]];
if(max(siz[v[k]],nowsiz-siz[v[k]])<mnsz)wt=k,mnsz=max(siz[v[k]],nowsiz-siz[v[k]]);;
}
}
int build(int l,int r)
{
int u=++nd;
if(l==r){sum[u]=s[l].x,key[u]=s[l].k,num[u]=s[l].x,trsiz[u]=1;return u;}
if(l<=mi-1)ls=build(l,mi-1);
if(mi+1<=r)rs=build(mi+1,r);
sum[u]=sum[ls]+sum[rs]+s[mi].x,key[u]=s[mi].k,num[u]=s[mi].x,trsiz[u]=trsiz[ls]+trsiz[rs]+1;
return u;
}
void add(int u,int lim,LL f)
{
if(!u)return;
}
void getwt(int u,int sumsiz,int tr)
{
if(sumsiz==1){to[tr]=u,bac[u]=tr,rt[tr]=++nd,sum[nd]=num[nd]=0,trsiz[nd]=1,key[nd]=ag[u];return;}
dep[u]=0,tp=0,nowsiz=sumsiz,mnsz=n+1,getsiz(u,0);int now=wt,nxt1=v[now],nxt2=v[now^1];
if(tr!=1)
{
sort(s+1,s+tp+1,cmp),rt[tr]=build(1,tp),to[tr]=u;
}
vis[now]=vis[now^1]=tr;
if(siz[nxt1]>siz[nxt2])getwt(nxt1,nowsiz-siz[nxt2],tr<<1),getwt(nxt2,siz[nxt2],tr<<1|1);
else getwt(nxt2,nowsiz-siz[nxt1],tr<<1),getwt(nxt1,siz[nxt1],tr<<1|1);
}
void getans(int u)
{
int now=bac[u];ans=0;
while(now>>1)
{
tmp=0;
now>>=1;
}
}
void getdep(int u,int fa)
{
dfn[u]=++tim;st[0][tim]=u;
view(u,k)if(v[k]!=fa){dis[v[k]]=dis[u]+w[k],getdep(v[k],u),st[0][++tim]=u;}
}
int main()
{
memset(fir,-1,sizeof(fir));
rep(i,1,n-1)
{
}
getdep(1,0);lg[0]=-1;
rep(i,1,tim)lg[i]=lg[i>>1]+1;
rep(k,1,lg[tim])for(int i=1;i+(1<<k)-1<=tim;++i)
st[k][i]=dis[st[k-1][i]]<dis[st[k-1][i+(1<<(k-1))]]?st[k-1][i]:st[k-1][i+(1<<(k-1))];
getwt(1,n,1);
while(q--)
{
ql=min((a+ans)%A,(b+ans)%A),qr=max((a+ans)%A,(b+ans)%A);
getans(u),write(ans);
}
return 0;
}


#include<algorithm>
#include<cmath>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define maxn 150010
#define maxm (maxn<<1)
#define view(u,k) for(int k=fir[u];k!=-1;k=nxt[k])
#define LL long long
#define ls son[u][0]
#define rs son[u][1]
#define mi (l+r>>1)
#define ls2 (u2<<1)
#define rs2 (u2<<1|1)
using namespace std;
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
void write(LL x)
{
if(x==0){putchar('0'),putchar('\n');return;}
int f=0;char ch[20];
if(x<0)putchar('-'),x=-x;
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
putchar('\n');
return;
}
struct node{int id,ag;}s[maxn];
int n,q,ag[maxn],siz[maxn],fir[maxn],v[maxm],nxt[maxm],cnt;
int fa[maxn],dfn[maxn],top[maxn],rt[maxn],nd,ql,qr,son[maxn<<6][2],wson[maxn],tim,bac[maxn],ver[maxn<<6],rnk[maxn];
void ade(int u1,int v1,int w1){v[cnt]=v1,w[cnt]=w1,nxt[cnt]=fir[u1],fir[u1]=cnt++;}
bool cmp1(node x,node y){return x.ag<y.ag;}
void getson(int u)
{
siz[u]=1;
view(u,k)if(v[k]!=fa[u])
{
fa[v[k]]=u,dep[v[k]]=dep[u]+w[k],tofa[v[k]]=w[k],getson(v[k]),siz[u]+=siz[v[k]];
if(!wson[u]||siz[wson[u]]<siz[v[k]])wson[u]=v[k];
}
}
void gettop(int u,int anc)
{
dfn[u]=++tim,bac[tim]=u,top[u]=anc;
if(wson[u])gettop(wson[u],anc);
view(u,k)if(v[k]!=wson[u]&&v[k]!=fa[u])gettop(v[k],v[k]);
}
int add(int u,int u2,int l,int r,int x,int y,int vers)
{
int nu;
if(vers==ver[u]&&u)nu=u;
else nu=++nd,ver[nu]=vers;
mk[nu]=mk[u];son[nu][0]=ls,son[nu][1]=rs;
return nu;
}
LL ask(int u,int u2,int l,int r,int x,int y,LL admk)
{
LL res=0;
return res;
}
void addrd(int u,int t1,int t2)
{
}
int getrnk(int x)
{
int l=1,r=n,ans=0;
while(l<=r){if(s[mi].ag<=x)ans=max(ans,mi),l=mi+1;else r=mi-1;}
return ans;
}
void getans(int u)
{
int pl=getrnk(ql-1),pr=getrnk(qr);ans=sum[pr]-sum[pl]+dep[u]*(LL)(pr-pl);
return;
}
int main()
{
memset(fir,-1,sizeof(fir));
sort(s+1,s+n+1,cmp1),ver[0]=-1,getson(1),gettop(1,1),build(1,1,n);
rep(i,1,n)
{
sum[i]=sum[i-1]+dep[s[i].id];