BZOJ 3052 [wc2013]糖果公园 树上莫队
2015-08-06 16:46
399 查看
题意:链接
方法:莫队上树
解析:
首先先考虑个莫队的算法,树分块
然后怎么做呢?
可以设两个指针么,然后按一定的排序方式移动指针,使复杂度可过。
观察到这道题是100s的,n^2是GG的,但比n^2稍微小一点就不GG了,
所以排序的作用就体现了。
按照左端点所在块为第一关键字,右端点所在块为第二关键字出现时间为第三关键字排序。
一共有n的1/3次方个块,所以所有的询问的左右端点所在的块的取值最多之后n的2/3次方种,对于每一种,对时间的修改最多会从尾改到头,所以又多乘一个q,n,q同阶,所以总复杂度O(n^5/3),如果两个询问的两个端点分别都在同一个块内的话,复杂度就到了块内的转移里,每块的大小是n^2/3次,所以复杂度也是O(n^5/3)。强行不到O(n^2)。
之后修改呢?
绑定一下暴力改就好了。
统计每一个修改之前有多少个询问。之后每一次枚举询问的时候暴力修改修改操作。
当然要排序,这样就显得线性多了2333
然后就莫队上树啦~
不要看错题!
代码:
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 100100 using namespace std; typedef long long ll; int n,m,q,cnt; int head ; ll V ,W ; int C ; int sta ; int belong ; int deep ; int fa [21]; int s ; int v ; ll print ; int status ; int sqr23n; int tot,top,cntque,cntcha; struct node { int from,to,next; }edge[N<<1]; struct query { int l,r,blockl,blockr,appcha,lca,no,alltime; }que ; struct change { int pt,pre,sub,no,alltime; }cha ; void init() { memset(head,-1,sizeof(head)); cnt=1; } void edgeadd(int from,int to) { edge[cnt].to=to; edge[cnt].next=head[from]; head[from]=cnt++; } void dfs(int now,int depth,int fafa) { v[now]=1,fa[now][0]=fafa,deep[now]=depth; int bot=top; for(int i=head[now];i!=-1;i=edge[i].next) { int to=edge[i].to; if(v[to])continue; dfs(to,depth+1,now); if(top-bot>=sqr23n) { tot++; do { belong[sta[top--]]=tot; }while(top!=bot); } } sta[++top]=now; } int lca(int x,int y) { if(deep[x]<deep[y])swap(x,y); for(int i=20;i>=0;i--) { if(deep[fa[x][i]]>=deep[y]&&fa[x][i]!=0)x=fa[x][i]; } for(int i=20;i>=0;i--) { if(fa[x][i]!=fa[y][i]&&fa[x][i]!=0&&fa[y][i]!=0) x=fa[x][i],y=fa[y][i]; } if(x==y)return x; return fa[x][0]; } ll ans; void Upd(int x) { if(status[x]) { status[x]=0; ans-=W[s[C[x]]]*V[C[x]]; s[C[x]]--; }else { status[x]=1; s[C[x]]++; ans+=W[s[C[x]]]*V[C[x]]; } } void update(int u,int v,int lca) { while(u!=lca) { Upd(u); u=fa[u][0]; } while(v!=lca) { Upd(v); v=fa[v][0]; } } int cmp(query a,query b) { if(a.blockl==b.blockl) { if(a.blockr==b.blockr) { return a.appcha<b.appcha; }else return a.blockr<b.blockr; }else return a.blockl<b.blockl; } void trans(int chaid,int opt) { int flag=0; if(opt)flag=1,swap(cha[chaid].pre,cha[chaid].sub); C[cha[chaid].pt]=cha[chaid].sub; if(status[cha[chaid].pt]) { ans-=V[cha[chaid].pre]*W[s[cha[chaid].pre]]; s[cha[chaid].pre]--; s[cha[chaid].sub]++; ans+=V[cha[chaid].sub]*W[s[cha[chaid].sub]]; } if(flag)swap(cha[chaid].pre,cha[chaid].sub); } int main() { init(); scanf("%d%d%d",&n,&m,&q); sqr23n=pow(n,2.0/3.0); for(int i=1;i<=m;i++)scanf("%lld",&V[i]); for(int i=1;i<=n;i++)scanf("%lld",&W[i]); for(int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); edgeadd(x,y),edgeadd(y,x); } dfs(1,1,0); while(top>0) { belong[sta[top--]]=tot; } for(int i=1;i<=20;i++) { for(int j=1;j<=n;j++) { fa[j][i]=fa[fa[j][i-1]][i-1]; } } for(int i=1;i<=n;i++)scanf("%d",&C[i]); for(int i=1;i<=q;i++) { int jd,x,y; scanf("%d%d%d",&jd,&x,&y); if(jd) { if(belong[x]>belong[y])swap(x,y); que[++cntque].l=x,que[cntque].r=y; que[cntque].blockl=belong[x],que[cntque].blockr=belong[y]; que[cntque].appcha=cntcha; que[cntque].lca=lca(que[cntque].l,que[cntque].r); que[cntque].no=cntque; que[cntque].alltime=cntque; }else { cha[++cntcha].pt=x,cha[cntcha].pre=C[x],cha[cntcha].sub=C[x]=y,cha[cntcha].no=cntcha; cha[cntcha].alltime=cntque; } } sort(que+1,que+cntque+1,cmp); int cnntcha=cntcha; update(que[1].l,que[1].r,que[1].lca); Upd(que[1].lca); while(cnntcha>0) { if(cha[cnntcha].alltime<que[1].alltime)break; trans(cnntcha,1); cnntcha--; } while(cnntcha<cntcha) { if(cha[cnntcha+1].alltime>=que[1].alltime)break; trans(cnntcha+1,0); cnntcha++; } print[que[1].no]=ans; Upd(que[1].lca); int tl=que[1].l,tr=que[1].r; for(int i=2;i<=cntque;i++) { int tmp=lca(que[i].l,tl); update(tl,que[i].l,tmp); tmp=lca(que[i].r,tr); update(tr,que[i].r,tmp); Upd(que[i].lca); while(cnntcha>0) { if(cha[cnntcha].alltime<que[i].alltime)break; trans(cnntcha,1); cnntcha--; } while(cnntcha<cntcha) { if(cha[cnntcha+1].alltime>=que[i].alltime)break; trans(cnntcha+1,0); cnntcha++; } print[que[i].no]=ans; Upd(que[i].lca); tl=que[i].l,tr=que[i].r; } for(int i=1;i<=cntque;i++)printf("%lld\n",print[i]); }
相关文章推荐
- 打开都是“Smart Adobe CC Blocker v1.0”已损坏,打不开。 您应该将它移到废纸篓。
- ACM二分法分饼问题
- mysql 数据备份与还原
- 总结Linux下查看流量工具
- Nginx+Tomcat+Memcached集群Session共享
- thinkphp添加数据add方法
- public String toString(){ return 自定义字符串; }有什么意义
- Cocos2d-x 3.6 项目实战---贪吃蛇(4)
- hdoj 2717 Catch That Cow 【bfs】
- Android典型界面设计(4)——使用ActionBar+Fragment实现tab切换
- Kubernetes技术分析之监控
- 码农《四》
- 为什么浏览器User-agent总是有Mozilla字样
- 安装SAP Business One对软硬件有哪些要求
- 安卓 调整多个不同尺寸的组件平均分布
- fork()函数详解
- hdu 1247 Hat’s Words Trie树(+测试数据)
- Lua元表和元方法
- java使用Redis5--分布式存储
- Win10正式版怎么把图片打开方式恢复默认照片查看器?