【codechef】Fibonacci Numbers on Tree
来源:互联网 发布:图片html源码怎么看 编辑:程序博客网 时间:2024/05/20 15:59
此题有两种做法。维护类Fibonacci前两项或者变为4个等比数列。
我用后一种,死活过不去。跟miaom前一种AC程序拍不出错。
2017.8.8:在wlc大佬帮助下,发现过不了居然是因为交的程序跟对拍的不是同一个!!!!
现在这个是AC程序。
#include <bits/stdc++.h>#define gc getchar()#define ll long long#define mid (l+r>>1)#define N 400009#define M 30000009#define mod 1000000009using namespace std;const ll IS5=276601605ll;const ll Q1=691504013ll;const ll Q2=308495997ll;ll n,m,first[N],number,last_ans,root[N<<1];ll size[N],fa[N],Mson[N],top[N],dfn[N],cnt,deep[N],id[N],last_root,ed[N];ll A[M][2][2],lson[M],rson[M],qry_pos[N],root_now,sum[M];ll g[N][6];struct edge{ ll to,next; void add(ll x,ll y) { to=y,next=first[x],first[x]=number; }}e[N<<1];ll read(){ ll x=1; char ch; while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1; ll s=ch-'0'; while (ch=gc,ch<='9'&&ch>='0') s=s*10+ch-'0'; return s*x;}void dfs(ll x){ size[x]=1; deep[x]=deep[fa[x]]+1; for (ll i=first[x];i;i=e[i].next) if (e[i].to!=fa[x]) { fa[e[i].to]=x; dfs(e[i].to); size[x]+=size[e[i].to]; if (size[e[i].to]>size[Mson[x]]) Mson[x]=e[i].to; }}void Dfs(ll x,ll y){ id[dfn[x]=++cnt]=x; top[x]=y; if (Mson[x]) Dfs(Mson[x],y); for (ll i=first[x];i;i=e[i].next) if (e[i].to!=fa[x]&&e[i].to!=Mson[x]) Dfs(e[i].to,e[i].to); ed[x]=cnt;}void ins(ll &cur,ll l,ll r,ll L,ll R,ll a1,ll a2,ll last,ll k){ bool flag=!cur; if (flag) { cur=++cnt; A[cur][0][0]=A[last][0][0],A[cur][1][0]=A[last][1][0]; A[cur][0][1]=A[last][0][1],A[cur][1][1]=A[last][1][1]; sum[cur]=sum[last]; } ll ar1=a1*g[max(L,l)-L][0]%mod,ar2=a2*g[max(L,l)-L][1]%mod; if (k==1&&(max(L,l)-L)&1) ar1=mod-ar1,ar2=mod-ar2; ll tmp=sum[cur]; sum[cur]=(sum[cur]+g[min(R,r)-max(L,l)][2+k*2]*ar1%mod+mod)%mod; sum[cur]=(sum[cur]+g[min(R,r)-max(L,l)][3+k*2]*ar2%mod+mod)%mod; if (L<=l&&R>=r) { A[cur][0][k]=(A[cur][0][k]+ar1)%mod; A[cur][1][k]=(A[cur][1][k]+ar2)%mod; if (flag) lson[cur]=lson[last],rson[cur]=rson[last]; return; } if (L>mid&&flag) lson[cur]=lson[last]; if (R<=mid&&flag) rson[cur]=rson[last]; if (L<=mid) ins(lson[cur],l,mid,L,R,a1,a2,lson[last],k); if (R>mid) ins(rson[cur],mid+1,r,L,R,a1,a2,rson[last],k);}ll qry(ll cur,ll l,ll r,ll L,ll R){ if (L<=l&&R>=r) return sum[cur]; ll ans=(g[min(R,r)-l][2]-((L>l)?g[L-l-1][2]:0))*A[cur][0][0]%mod; ans=(ans+(g[min(R,r)-l][3]-((L>l)?g[L-l-1][3]:0))*A[cur][1][0]%mod)%mod; ans=(ans+(g[min(R,r)-l][4]-((L>l)?g[L-l-1][4]:0))*A[cur][0][1]%mod)%mod; ans=(ans+(g[min(R,r)-l][5]-((L>l)?g[L-l-1][5]:0))*A[cur][1][1]%mod)%mod; if (L<=mid) ans=(ans+qry(lson[cur],l,mid,L,R))%mod; if (R>mid) ans=(ans+qry(rson[cur],mid+1,r,L,R))%mod; return (ans%mod+mod)%mod;}void build(ll &cur,ll l,ll r){ cur=++cnt; if (l==r) return; build(lson[cur],l,mid); build(rson[cur],mid+1,r);}ll lca(ll x,ll y){ for (;top[x]!=top[y];x=fa[top[x]]) if (deep[top[x]]<deep[top[y]]) swap(x,y); return deep[x]<deep[y]?x:y;}void ins(ll x,ll y){ ll LCA=lca(x,y),len=deep[x]+deep[y]-2*deep[LCA]+1; ll a1=IS5*Q1%mod,a2=IS5*Q2%mod; ll a3=IS5*g[len+1][0]%mod,a4=IS5*g[len+1][1]%mod; bool flag=1; root[root_now]=root[last_root]; ll tmp; for (;top[x]!=top[y];x=fa[top[x]]) { if (deep[top[x]]<deep[top[y]]) swap(x,y),flag^=1; if (flag) { ins(tmp=0,1,n,dfn[top[x]],dfn[x],mod-a2*g[dfn[x]-dfn[top[x]]][1]%mod,a1*g[dfn[x]-dfn[top[x]]][0]%mod,root[root_now],1); a1=a1*g[dfn[x]-dfn[top[x]]+1][0]%mod;//fa[top] 1 a2=a2*g[dfn[x]-dfn[top[x]]+1][1]%mod; root[root_now]=tmp; } else { a3=a3*g[dfn[x]-dfn[top[x]]+1][1]%mod;//top 0 a4=a4*g[dfn[x]-dfn[top[x]]+1][0]%mod; if ((dfn[x]-dfn[top[x]]+1)&1) a3=mod-a3,a4=mod-a4; ins(tmp=0,1,n,dfn[top[x]],dfn[x],a3,mod-a4,root[root_now],0); root[root_now]=tmp; } } if (deep[x]>deep[y]) swap(x,y),flag^=1; if (flag) ins(tmp=0,1,n,dfn[x],dfn[y],a1,mod-a2,root[root_now],0);//0 else ins(tmp=0,1,n,dfn[x],dfn[y],a4*Q1%mod,mod-a3*Q2%mod,root[root_now],1);//1 root[root_now]=tmp;}ll qry(ll x,ll y,ll ret=0){ for (;top[x]!=top[y];x=fa[top[x]]) { if (deep[top[x]]<deep[top[y]]) swap(x,y); ret=(ret+qry(root[last_root],1,n,dfn[top[x]],dfn[x]))%mod; } if (deep[x]>deep[y]) swap(x,y); ret=(ret+qry(root[last_root],1,n,dfn[x],dfn[y]))%mod; return (ret%mod+mod)%mod;}ll get(ll x,ll y){ while (deep[fa[top[y]]]>deep[x]) y=fa[top[y]]; if (deep[fa[top[y]]]==deep[x]) return top[y]; return id[dfn[x]+1];}int main(){ //freopen("1.in","r",stdin); //freopen("2.out","w",stdout); g[0][0]=g[0][1]=g[0][2]=g[0][3]=g[0][4]=g[0][5]=1; for (ll i=1;i<N;i++) { g[i][0]=g[i-1][0]*Q1%mod; g[i][1]=g[i-1][1]*Q2%mod; g[i][2]=(g[i-1][2]+g[i][0])%mod; g[i][3]=(g[i-1][3]+g[i][1])%mod; g[i][4]=(g[i-1][4]+((i&1)?(-g[i][0]):g[i][0])+mod)%mod; g[i][5]=(g[i-1][5]+((i&1)?(-g[i][1]):g[i][1])+mod)%mod; } n=read(),m=read(); for (ll i=1;i<n;i++) { ll x=read(),y=read(); e[++number].add(x,y),e[++number].add(y,x); } dfs(1),Dfs(1,1); cnt=0; build(root[0],1,n); for (ll i=1;i<=m;i++) { //last_ans=0; char str[3]; scanf("%s",&str); ll x=read()^last_ans,y; if (str[0]=='A') { root_now++; y=read(); ins(x,y); last_root=root_now; qry_pos[i]=root_now; } if (str[0]=='Q') { if (str[1]=='S') { y=read(); swap(x,y); if (x==y) printf("%lld\n",last_ans=qry(root[last_root],1,n,1,n)); else if (dfn[x]<dfn[y]&&dfn[y]<=ed[x]) { ll anc=get(x,y); last_ans=qry(root[last_root],1,n,1,n)-qry(root[last_root],1,n,dfn[anc],ed[anc]); last_ans=(last_ans%mod+mod)%mod; printf("%lld\n",last_ans); } else printf("%lld\n",last_ans=qry(root[last_root],1,n,dfn[x],ed[x])); } else { y=read(); printf("%lld\n",last_ans=qry(x,y)); } qry_pos[i]=last_root; } if (str[0]=='R') qry_pos[i]=last_root=qry_pos[x]; } return 0;}
阅读全文
0 0
- 【codechef】Fibonacci Numbers on Tree
- Fibonacci Numbers on Tree
- Hackerrank Fibonacci Numbers Tree
- [CODECHEF]Prime Distance On Tree
- CodeChef PRIMEDSTPrime Distance On Tree
- 【CodeChef PRIMEDST】Prime Distance On Tree
- codechef Prime Distance On Tree FFT
- Codechef Prime Distance On Tree(点分治+FFT)
- codechef Prime Distance On Tree(树分治+fft)
- Codechef Aug2017 #Walks on the binary tree -- 主席树+Hash
- [数学] Codechef September Challenge 2017 Weasel does Xor on Tree
- Fibonacci numbers
- Fibonacci Numbers
- Fibonacci Numbers
- Fibonacci Numbers
- Fibonacci numbers
- Create pseudo-random numbers based on Fibonacci sequence
- [二进制分组 dsu on tree 二次函数] Codechef KILLER Painting Tree
- VMware虚拟机过卡-解决办法(Win10系统)
- 关系型数据库与非关系型数据库的区别?
- 使用阿里云ecs搭建网站
- mysql 下计算两点经纬度之间的距离
- hdu 1231 最大连续子序列
- 【codechef】Fibonacci Numbers on Tree
- 前端技能树
- POJ 1065 && HDU 1051 Wooden Sticks 贪心
- SDUT 2622 最短路径
- HTTP学习(二)
- Oracle创建用户并授权
- AD设置覆铜的过孔连接方式
- 最简单了解Linux中su和sudo的区别
- WebRTC56版本SDP详细解析