BZOJ 4999: This Problem Is Too Simple!
来源:互联网 发布:网络信息安全培训课件 编辑:程序博客网 时间:2024/05/22 12:47
不错的题啊
考虑可以离线 就可以把值相同的一起搞
以值为第一关键字 时间为第二关键字排序一下就好
记得要弄多一个删除操作 在这个位置的值被改的时候 就要给原来的值补一个删除操作
树上路径其实可以用dfs序+树状数组这个经典套路来算
时间复杂度就是 O(nlogn)了
我的代码明明那么好看 为什么这么慢呢(肯定是评测机嫉妒我的代码好看又短QAQ)
#include<bits/stdc++.h>using namespace std;const int N=1e5+5,M=450000;inline int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){if(ch=='-')f=-1; ch=getchar();} while(ch>='0' && ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=getchar();} return x*f;}struct G{ int p,x,y,u,id;}a[M];int Cmp1(G x1,G x2){ if(x1.u!=x2.u)return x1.u<x2.u; return x1.id<x2.id;}int Cmp2(G x1,G x2){return x1.id<x2.id;}struct edge{int y,nex;}e[N<<1]; int len,fir[N];void ins(int x,int y){e[++len]=(edge){y,fir[x]},fir[x]=len;}int p[N],tot,in[N],out[N],ans[M];int dep[N],f[N][17];void dfs(int x,int fa){ in[x]=++tot; f[x][0]=fa,dep[x]=dep[fa]+1; for(int i=1;i<17;++i) if(f[x][i-1])f[x][i]=f[f[x][i-1]][i-1]; else break; for(int k=fir[x];k;k=e[k].nex) if(e[k].y!=fa)dfs(e[k].y,x); out[x]=++tot;}int lca(int x,int y){ if(dep[x]<dep[y])x^=y^=x^=y; for(int i=16;~i;--i) if(dep[x]-dep[y]>=(1<<i))x=f[x][i]; if(x==y)return x; for(int i=16;~i;--i) if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i]; return f[x][0];}int t[N<<1],q[N];void add(int x,int u){for(;x<=tot;x+=x&-x)t[x]+=u;}int get(int x){int s=0;for(;x;x-=x&-x)s+=t[x]; return s;}int main(){ int n=read(),m=read(),i,l=n,j; for(i=1;i<=n;++i){ int x=read(); q[i]=x; a[i]=(G){0,i,0,x,i},p[i]=i; } for(i=1;i<n;++i){ int x=read(),y=read(); ins(x,y),ins(y,x); } dfs(1,0); for(i=1;i<=m;++i){ char ch; scanf("\n%c",&ch); if(ch=='Q'){ int x=read(),y=read(),u=read(); a[++l]=(G){1,x,y,u,l}; } else{ int x=read(),u=read(); a[++l]=(G){0,x,1,a[p[x]].u,l}; a[++l]=(G){0,x,0,u,l}; p[x]=l; } } for(i=1;i<=n;++i){ int x=q[i]; a[++l]=(G){0,i,1,a[p[i]].u,l}; } sort(a+1,a+1+l,Cmp1); for(i=1;i<=l;i=j){ for(j=i;a[j].u==a[i].u;++j) if(a[j].p){ int u=lca(a[j].x,a[j].y); ans[a[j].id]= get(in[a[j].x])-get(in[u]); ans[a[j].id]+=get(in[a[j].y])-get(in[u]-1); } else{ int u=a[j].y?1:-1; add(in[a[j].x],-u), add(out[a[j].x],u); } } sort(a+1,a+1+l,Cmp2); for(i=1;i<=l;++i)if(a[i].p)printf("%d\n",ans[i]); return 0;}
阅读全文
1 0
- bzoj 4999: This Problem Is Too Simple!
- bzoj 4999: This Problem Is Too Simple!
- BZOJ 4999: This Problem Is Too Simple!
- [BZOJ]4999: This Problem Is Too Simple! 树链剖分
- bzoj 4999: This Problem Is Too Simple! 树链剖分+线段树
- 4999: This Problem Is Too Simple!
- 4999: This Problem Is Too Simple!
- bzoj 4999 This problem is too simple 树链剖分+动态开点线段树
- 【bzoj4999】This Problem Is Too Simple!
- bzoj4999 This Problem Is Too Simple!(树链剖分+动态开点线段树)
- SVN update problem: This client is too old to work with working copy
- This is the first problem for test. Since all we know the ASCII code, your job is simple: Input numb
- Typically this problem occurs when you are putting in a data that is too long for the column. In t
- BZOJ 3489 A simple rmq problem
- 【31.94%】【BZOJ 3489】A simple rmq problem
- bzoj 3489: A simple rmq problem
- BZOJ 3489 A simple rmq problem
- BZOJ 3489: A simple rmq problem 树套树
- 原码,补码,反码详解:
- CLion最好用的编译器&Xcode
- ThreadPoolExcutor(线程池)
- 表触发器
- OpenCV简单操作-图像腐蚀
- BZOJ 4999: This Problem Is Too Simple!
- Linux基础使用
- 面试or笔试3——最大连续子序列和
- HIVE学习笔记:HiveServer2,调用HIVE的JavaAPI
- Git 服务器搭建
- Java中的static关键字解析
- 学生信息系统错误集(一)
- python六之列表生成式
- C语言.h文件的作用