链接

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

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

#include<bits/stdc++.h>
#define REP(i,a,b) for(int i(a);i<=(b);++i)
#define dbg(...) fprintf(stderr,__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int>pii;
template<typename T,typename U>inline char smin(T&x,const U&y){return x>y?x=y,1:0;}
template<typename T,typename U>inline char smax(T&x,const U&y){return x<y?x=y,1:0;}
int T;
struct hash_table{
static const int S=1e6+5;
int head[S],to[S],ne[S];
inline int get(char *s){
int u=0;while(*s)u=u*27+*s++-'a'+1;int p=u%S;
for(int i=head[p];i;i=ne[i])if(to[i]==u)return i;
to[++T]=u,ne[T]=head[p],head[p]=T;
return T;
}
}mp;
const int N=1e5+5,MT=5e5+5;
int n,m;
vector<int>g[N];
int fa[N],dep[N],top[N],in[N],son[N],siz[N],dfn;
inline void go1(int x){
siz[x]=1;
for(int y:g[x])if(y!=fa[x])
dep[y]=dep[x]+1,fa[y]=x,go1(y),siz[x]+=siz[y],siz[y]>siz[son[x]]&&(son[x]=y);
}
inline void go2(int x,int anc){
in[x]=++dfn,top[x]=anc;
if(!son[x])return;go2(son[x],anc);
for(int y:g[x])if(y!=fa[x]&&y!=son[x])go2(y,y);
}
inline int dis(int x,int y){
int ans=0;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
ans+=dep[x]-dep[top[x]]+1;x=fa[top[x]];
}
return ans+abs(dep[x]-dep[y]);
}
int rt[MT],treecnt;
struct tree{int ls,rs,w;bool del;}t[MT*25];
inline void ins(int&o,int l,int r,int x){
if(!o)o=++treecnt;++t[o].w;
if(l==r)return;int mid=l+r>>1;
x<=mid?ins(t[o].ls,l,mid,x):ins(t[o].rs,mid+1,r,x);
}
inline void pushdown(int o){
if(t[o].del){
if(t[o].ls)t[t[o].ls].w=0,t[t[o].ls].del=1;
if(t[o].rs)t[t[o].rs].w=0,t[t[o].rs].del=1;
t[o].del=0;
}
}
inline int ask(int o,int l,int r,int x,int y){
if(!o||x>r||y<l||x>y||t[o].del)return 0;
if(x<=l&&r<=y)return t[o].w;pushdown(o);
int mid=l+r>>1;
return ask(t[o].ls,l,mid,x,y)+ask(t[o].rs,mid+1,r,x,y);
}
inline int update(int o,int l,int r,int x,int y){
if(!o||x>y||t[o].del)return 0;
if(x<=l&&r<=y){
int res=t[o].w;t[o].w=0;t[o].del=1;return res;
}
int mid=l+r>>1,res=0;pushdown(o);
if(x<=mid)res+=update(t[o].ls,l,mid,x,y);
if(y>mid)res+=update(t[o].rs,mid+1,r,x,y);
t[o].w=t[t[o].ls].w+t[t[o].rs].w;
return res;
}
inline int ask(int o,int x,int y){
int ans=0;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
ans+=ask(o,1,n,in[top[x]],in[x]);x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
return ans+ask(o,1,n,in[x],in[y]);
}
inline int update(int o,int x,int y){
int ans=0;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
ans+=update(o,1,n,in[top[x]],in[x]);x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
return ans+update(o,1,n,in[x],in[y]);
}
int main(){
scanf("%d%d",&n,&m);
REP(i,2,n){
#define pb push_back
int x,y;scanf("%d%d",&x,&y);g[x].pb(y),g[y].pb(x);
}
go1(1),go2(1,1);
static char s[10];
REP(i,1,n){
int t;scanf("%d",&t);
while(t--){
scanf("%s",s);
ins(rt[mp.get(s)],1,n,in[i]);
}
}
while(m--){
scanf("%s",s);
int x,y;
if(s[0]=='q'){
scanf("%s%d%d",s,&x,&y);
if(s[1]=='p')printf("%d\n",dis(x,y));
else{
scanf(" *.%s",s);printf("%d\n",ask(rt[mp.get(s)],x,y));
}
}else{
scanf("%*s%d%d *.%s",&x,&y,s);
printf("%d\n",update(rt[mp.get(s)],x,y));
}
}
return 0;
}

[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. 05-数据类型转换(bool转换)

    05-数据类型转换(bool转换)

  2. ECNUOJ 2143 端午节快乐

    端午节快乐 Time Limit:1000MS Memory Limit:65536KBTotal Submit:1720 Accepted:868 Description  有一段有趣的传说.公元前 ...

  3. ArcGIS api for javascript——查询,然后单击显示信息窗口

    描述 本例展示如何配置查询任务为示例的工作流程: 1.用户单击一个要素来加亮显示. 2.用户再一次单击要素来查看属性信息的 InfoWindow. 本例查询USA州,因此ESRI_StateCityH ...

  4. [Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream

    Rather than using Components to push streams into other Components, mapPropsStream allows you to cre ...

  5. [React] Implement a Higher Order Component with Render Props

    When making a reusable component, you'll find that people often like to have the API they're most fa ...

  6. 记一次httpclient Connection reset问题定位

    问题:某业务系统在运行一段时间后,某个API一定概率偶现Connection reset现象. 问题定位: 首先想到的是要本地复现出这个问题,但一直复现不出来. 1.根据线上问题相关日志判断应该是有部 ...

  7. 初识activiti

    Activity工作流学习要点 1. 1个插件 在Eclipse中安装Activity插件,让你可以在Eclipse中绘制Activity工作流图 2. 1个引擎 ProcessEngine对象,Ac ...

  8. orm 通用方法——QueryModelById 主键查询

    方法定义: /** * 描述:根据主键查询 * 作者:Tianqi * 日期:2014-09-15 * param:model 对象实例,包含主键 * return:对象 * */ func Quer ...

  9. Docker安装配置教程

    Docker公开课 1 Docker介绍 1.1 Docker是什么 云计算\云服务 IAAS(基础设施即服务).PAAS(平台即服务).SAAS(软件即服务) Docker到底是什么呢? Docke ...

  10. Android自定义控件简单实现ratingbar效果

    先上图: 一开始让我自定义控件我是拒绝的,因为android很早以前就有一个控件ratingbar,但是设置样式的时候我发现把图片设置小一点就显示不全,一直找不到解办法!(可以设置系统的自带的小样式) ...