链接

刚开始看出题人题解都吓蒙掉了,还以为是什么难题,结果就一板子题

思路:对每一个文件名开一棵线段树,然后树剖即可

  1. #include<bits/stdc++.h>
  2. #define REP(i,a,b) for(int i(a);i<=(b);++i)
  3. #define dbg(...) fprintf(stderr,__VA_ARGS__)
  4. using namespace std;
  5. typedef long long ll;
  6. typedef unsigned int uint;
  7. typedef unsigned long long ull;
  8. typedef pair<int,int>pii;
  9. template<typename T,typename U>inline char smin(T&x,const U&y){return x>y?x=y,1:0;}
  10. template<typename T,typename U>inline char smax(T&x,const U&y){return x<y?x=y,1:0;}
  11. int T;
  12. struct hash_table{
  13. static const int S=1e6+5;
  14. int head[S],to[S],ne[S];
  15. inline int get(char *s){
  16. int u=0;while(*s)u=u*27+*s++-'a'+1;int p=u%S;
  17. for(int i=head[p];i;i=ne[i])if(to[i]==u)return i;
  18. to[++T]=u,ne[T]=head[p],head[p]=T;
  19. return T;
  20. }
  21. }mp;
  22. const int N=1e5+5,MT=5e5+5;
  23. int n,m;
  24. vector<int>g[N];
  25. int fa[N],dep[N],top[N],in[N],son[N],siz[N],dfn;
  26. inline void go1(int x){
  27. siz[x]=1;
  28. for(int y:g[x])if(y!=fa[x])
  29. dep[y]=dep[x]+1,fa[y]=x,go1(y),siz[x]+=siz[y],siz[y]>siz[son[x]]&&(son[x]=y);
  30. }
  31. inline void go2(int x,int anc){
  32. in[x]=++dfn,top[x]=anc;
  33. if(!son[x])return;go2(son[x],anc);
  34. for(int y:g[x])if(y!=fa[x]&&y!=son[x])go2(y,y);
  35. }
  36. inline int dis(int x,int y){
  37. int ans=0;
  38. while(top[x]!=top[y]){
  39. if(dep[top[x]]<dep[top[y]])swap(x,y);
  40. ans+=dep[x]-dep[top[x]]+1;x=fa[top[x]];
  41. }
  42. return ans+abs(dep[x]-dep[y]);
  43. }
  44. int rt[MT],treecnt;
  45. struct tree{int ls,rs,w;bool del;}t[MT*25];
  46. inline void ins(int&o,int l,int r,int x){
  47. if(!o)o=++treecnt;++t[o].w;
  48. if(l==r)return;int mid=l+r>>1;
  49. x<=mid?ins(t[o].ls,l,mid,x):ins(t[o].rs,mid+1,r,x);
  50. }
  51. inline void pushdown(int o){
  52. if(t[o].del){
  53. if(t[o].ls)t[t[o].ls].w=0,t[t[o].ls].del=1;
  54. if(t[o].rs)t[t[o].rs].w=0,t[t[o].rs].del=1;
  55. t[o].del=0;
  56. }
  57. }
  58. inline int ask(int o,int l,int r,int x,int y){
  59. if(!o||x>r||y<l||x>y||t[o].del)return 0;
  60. if(x<=l&&r<=y)return t[o].w;pushdown(o);
  61. int mid=l+r>>1;
  62. return ask(t[o].ls,l,mid,x,y)+ask(t[o].rs,mid+1,r,x,y);
  63. }
  64. inline int update(int o,int l,int r,int x,int y){
  65. if(!o||x>y||t[o].del)return 0;
  66. if(x<=l&&r<=y){
  67. int res=t[o].w;t[o].w=0;t[o].del=1;return res;
  68. }
  69. int mid=l+r>>1,res=0;pushdown(o);
  70. if(x<=mid)res+=update(t[o].ls,l,mid,x,y);
  71. if(y>mid)res+=update(t[o].rs,mid+1,r,x,y);
  72. t[o].w=t[t[o].ls].w+t[t[o].rs].w;
  73. return res;
  74. }
  75. inline int ask(int o,int x,int y){
  76. int ans=0;
  77. while(top[x]!=top[y]){
  78. if(dep[top[x]]<dep[top[y]])swap(x,y);
  79. ans+=ask(o,1,n,in[top[x]],in[x]);x=fa[top[x]];
  80. }
  81. if(dep[x]>dep[y])swap(x,y);
  82. return ans+ask(o,1,n,in[x],in[y]);
  83. }
  84. inline int update(int o,int x,int y){
  85. int ans=0;
  86. while(top[x]!=top[y]){
  87. if(dep[top[x]]<dep[top[y]])swap(x,y);
  88. ans+=update(o,1,n,in[top[x]],in[x]);x=fa[top[x]];
  89. }
  90. if(dep[x]>dep[y])swap(x,y);
  91. return ans+update(o,1,n,in[x],in[y]);
  92. }
  93. int main(){
  94. scanf("%d%d",&n,&m);
  95. REP(i,2,n){
  96. #define pb push_back
  97. int x,y;scanf("%d%d",&x,&y);g[x].pb(y),g[y].pb(x);
  98. }
  99. go1(1),go2(1,1);
  100. static char s[10];
  101. REP(i,1,n){
  102. int t;scanf("%d",&t);
  103. while(t--){
  104. scanf("%s",s);
  105. ins(rt[mp.get(s)],1,n,in[i]);
  106. }
  107. }
  108. while(m--){
  109. scanf("%s",s);
  110. int x,y;
  111. if(s[0]=='q'){
  112. scanf("%s%d%d",s,&x,&y);
  113. if(s[1]=='p')printf("%d\n",dis(x,y));
  114. else{
  115. scanf(" *.%s",s);printf("%d\n",ask(rt[mp.get(s)],x,y));
  116. }
  117. }else{
  118. scanf("%*s%d%d *.%s",&x,&y,s);
  119. printf("%d\n",update(rt[mp.get(s)],x,y));
  120. }
  121. }
  122. return 0;
  123. }

[LuoguU41039]PION后缀自动机 树链剖分+动态开点线段树的更多相关文章

  1. 【bzoj4999】This Problem Is Too Simple! 树链剖分+动态开点线段树

    题目描述 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将i节点的值改为x. 2. Q i j x(0<=x<2^31) ...

  2. [bzoj 3531][SDOI2014]旅行(树链剖分+动态开点线段树)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3531 分析: 对于每个颜色(颜色<=10^5)都建立一颗线段树 什么!那么不是M ...

  3. 洛谷P3313 [SDOI2014]旅行(树链剖分 动态开节点线段树)

    题意 题目链接 Sol 树链剖分板子 + 动态开节点线段树板子 #include<bits/stdc++.h> #define Pair pair<int, int> #def ...

  4. BZOJ 3531 [Sdoi2014]旅行 树链剖分+动态开点线段树

    题意 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰. 为了方便,我们用 ...

  5. bzoj3531: [Sdoi2014]旅行 (树链剖分 && 动态开点线段树)

    感觉动态开点线段树空间复杂度好优秀呀 树剖裸题 把每个宗教都开一颗线段树就可以了 但是我一直TLE 然后调了一个小时 为什么呢 因为我 #define max(x, y) (x > y ? x ...

  6. [ZJOI2019]语言(树链剖分+动态开点线段树+启发式合并)

    首先,对于从每个点出发的路径,答案一定是过这个点的路径所覆盖的点数.然后可以做树上差分,对每个点记录路径产生总贡献,然后做一个树剖维护,对每个点维护一个动态开点线段树.最后再从根节点开始做一遍dfs, ...

  7. 【BZOJ3531】[Sdoi2014]旅行 树链剖分+动态开点线段树

    [BZOJ3531][Sdoi2014]旅行 Description S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天 ...

  8. BZOJ4999: This Problem Is Too Simple!树链剖分+动态开点线段树

    题目大意:将某个节点的颜色变为x,查询i,j路径上多少个颜色为x的点... 其实最开始一看就是主席树+树状数组+DFS序...但是过不去...MLE+TLE BY FCWWW 其实树剖裸的一批...只 ...

  9. bzoj3531——树链剖分+动态开点线段树

    3531: [Sdoi2014]旅行 Time Limit: 20 Sec  Memory Limit: 512 MB Description S国有N个城市,编号从1到N.城市间用N-1条双向道路连 ...

随机推荐

  1. unity 自动删除未引用的Assets下的资源

    随着时间的堆积,项目中Assets文件夹下的资源会变得越来越繁杂,有些贴图.材质啥的可能压根没有使用过,但是又不敢轻易去删除. 这里分享两个插件,用于管理这些资源. 一.ResourceChecker ...

  2. 利用socket模拟http的混合表单上传(在一个请求中提交表单并上传多个文件)

           在非常多企业级应用中,我们都没法直接通过开发语言sdk包封装的http工具来模拟http复合表单(multipart/form-data),特别是在跨语言跨平台的编程过程中.事实上实现方 ...

  3. 三:redis的List类型相关操作

    </pre><pre name="code" class="php" style="font-size: 14px;"&g ...

  4. POJ 3670 Eating Together(LIS)

    Description The cows are so very silly about their dinner partners. They have organized themselves i ...

  5. type &#39;simple Class&#39; does not conform to protocol &#39;Example Protocol&#39;错误

    在看swift教程中"接口和扩展"这小部分. 在编写时提示"type 'simple Class' does not conform to protocol 'Examp ...

  6. zico源代码分析(一) 数据接收和存储部分

    zorka和zico的代码地址:https://github.com/jitlogic 由于zico是zorka的collecter端,所以在介绍zico之前首先说一下zorka和数据结构化存储和传输 ...

  7. 学password学一定得学程序

    题目描写叙述 以前.ZYJ同学非常喜欢password学.有一天,他发现了一个非常长非常长的字符串S1.他非常好奇那代表着什么,于是奇妙的WL给了他还有一个字符串S2.可是非常不幸的是,WL忘记跟他说 ...

  8. Android Support Library 23.2用法简析

    写在前面的几句话 前几天谷歌发布了android-support-library-23.2支持库,这一次23.2版本增加了一些新的支持库以及新的功能.接下来这篇文章,就是对这些新功能部分做简单的用法介 ...

  9. lightoj--1008--Fibsieve`s Fantabulous Birthday(水题)

    Fibsieve`s Fantabulous Birthday Time Limit: 500MS   Memory Limit: 32768KB   64bit IO Format: %lld &a ...

  10. lightoj--1116--Ekka Dokka(水题)

    Ekka Dokka Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Stat ...