AC自动机,树上莫队,树状数组。。

  比赛的时候完全看不出来...赛后去膜了一波网上题解才明白= =

  离线,先把AC自动机建出来,把fail边反向建出fail树。

  发射言弹,就是将言弹结束节点的fail子树内点权+1;

  查询证言受的伤害,就是查询证言的每个前缀的结束节点的点权和。

  前缀的结束节点的点权和,也就是AC自动机上,根到证言结束位置路径上的点权和。

  每一个查询,就是查询在一段时间内,根到证言结束位置路径上的点权和。。。

  所以就树上莫队一波了。。前两维时间,第三维是AC自动机上的位置。。再来个树状数组维护一波...

  时间复杂度O(n^(3/5)logn)。。。。卡时过的。。。还要注意一下块的大小。。。

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=<<;
struct zs{int too,pre;}e[maxn];int tt,last[maxn];
struct ask{int x,tim,timl,id;}b[maxn];
int B[maxn];
int ch[maxn][],fail[maxn],dl[maxn],cnt;
int dfn[maxn],mp[maxn],TIME,L[maxn],R[maxn],TIM;
int pos[maxn],pos1[maxn],tim1[maxn],a[maxn];
int i,j,k,n,m;
ll sum,an[maxn];
bool u[maxn]; int ra;char rx;
inline int read(){
rx=getchar(),ra=;
while(rx<''||rx>'')rx=getchar();
while(rx>=''&&rx<='')ra*=,ra+=rx-,rx=getchar();return ra;
} void dfs(int x){
L[x]=++TIM;
for(int i=last[x];i;i=e[i].pre)
dfs(e[i].too);
R[x]=TIM;//printf(" x:%d l--r:%d %d\n",x,L[x],R[x]);
}
void dfs2(int x){
dfn[x]=++TIME,mp[TIME]=x;//printf("x:%d dfn:%d\n",x,dfn[x]);
for(int i=;i<;i++)if(ch[x][i])
dfs2(ch[x][i]);
mp[++TIME]=x;
}
inline void insert(int a,int b){e[++tt].too=b,e[tt].pre=last[a],last[a]=tt;/*printf("%d-->%d\n",a,b);*/} int t[maxn],t1[maxn];
inline void add1(int x){/*printf(" add1:%d\n",x);*/while(x<=cnt)t1[x]++,x+=x&-x;}
inline void del1(int x){/*printf(" del1:%d\n",x);*/while(x<=cnt)t1[x]--,x+=x&-x;}
inline int get1(int l,int r){
int sm=;//printf(" get1:%d--%d",l,r);
l--;while(l)sm-=t1[l],l-=l&-l;
while(r)sm+=t1[r],r-=r&-r;/*printf(": %d\n",sm);*/return sm;
} inline int get(int x){
// printf("get:%d",x);
int sm=;while(x)sm+=t[x],x-=x&-x;//printf(" %d\n",sm);
return sm;
}
inline void add(int l,int r){
if(!l)return;//printf("add: %d--%d\n",l,r);
sum+=get1(l,r);
while(l<=cnt)t[l]++,l+=l&-l;
r++;while(r<=cnt)t[r]--,r+=r&-r;
}
inline void del(int l,int r){
if(!l)return;//printf("del: %d--%d\n",l,r);
sum-=get1(l,r);
while(l<=cnt)t[l]--,l+=l&-l;
r++;while(r<=cnt)t[r]++,r+=r&-r;
} void getfail(){
int l=,r=,i,p,now,j;dl[]=;
while(l<r){
now=dl[++l];
for(i=;i<;i++)if(ch[now][i]){
dl[++r]=j=ch[now][i];
for(p=fail[now];p&&!ch[p][i];p=fail[p]);
fail[j]=!now?:ch[p][i],insert(fail[j],j);
}
}
} bool cmp(ask a,ask b){
return B[a.timl]==B[b.timl]?(B[a.tim]==B[b.tim]?a.x<b.x:B[a.tim]<B[b.tim]):B[a.timl]<B[b.timl];
}
int main(){
m=read();char id[],c[];int x,tm=,numb=,n=,n1=;
for(i=;i<=m;i++){
scanf("%s",id);
if(id[]=='I'){
n++,scanf("%s",c),c[]-='a',x=read();
if(!ch[pos[x]][c[]])ch[pos[x]][c[]]=++cnt;//printf(" .. %d %d\n",pos[x],c[0]);
pos[n]=ch[pos[x]][c[]];//,tim[n]=tm;
}
if(id[]=='A'){
n1++,scanf("%s",c),c[]-='a',x=read();
if(!ch[pos1[x]][c[]])ch[pos1[x]][c[]]=++cnt;//,printf(" %d %d\n",pos1[x],c[0]);
pos1[n1]=ch[pos1[x]][c[]],tim1[n1]=tm;
}
if(id[]=='S')
tm++,a[tm]=pos[read()];
if(id[]=='Q')
b[++numb].x=read(),b[numb].tim=tm,b[numb].timl=tim1[b[numb].x]+,b[numb].id=numb;
}
cnt++;
getfail();
dfs(),dfs2();int kuai=(int)pow(tm,1.8/)+;
for(i=;i<=tm+;i++)B[i]=(i+kuai-)/kuai;
for(i=;i<=numb;i++)b[i].x=dfn[pos1[b[i].x]];
sort(b+,b++numb,cmp); int l=,r=,pos=;L[]=R[]=,u[mp[]]=;
for(i=;i<=numb;i++){
// printf("ask: %d--%d %d--%d x:%d sum:%lld\n",b[i].timl,b[i].tim,B[b[i].timl],B[b[i].tim],b[i].x,sum);
while(l>b[i].timl)l--,add(L[a[l]],R[a[l]]);
while(r<b[i].tim)r++,add(L[a[r]],R[a[r]]);
while(l<b[i].timl)del(L[a[l]],R[a[l]]),l++;
while(r>b[i].tim)del(L[a[r]],R[a[r]]),r--;//printf("sum: %lld\n",sum);
while(pos>b[i].x){
if(u[mp[pos]])sum-=get(L[mp[pos]]),del1(L[mp[pos]]);else sum+=get(L[mp[pos]]),add1(L[mp[pos]]);
u[mp[pos]]^=,pos--;
}
while(pos<b[i].x){
pos++;//printf("x: %d dfn:%d L:%d\n",mp[pos],pos,L[mp[pos]]);
if(u[mp[pos]])sum-=get(L[mp[pos]]),del1(L[mp[pos]]);else sum+=get(L[mp[pos]]),add1(L[mp[pos]]);
u[mp[pos]]^=;
}
an[b[i].id]=sum;
}
for(i=;i<=numb;i++)printf("%lld\n",an[i]);
}

[51nod Round15 E ]Danganronpa的更多相关文章

  1. 【51Nod 1244】莫比乌斯函数之和

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 模板题... 杜教筛和基于质因子分解的筛法都写了一下模板. 杜教筛 ...

  2. 51Nod 1268 和为K的组合

    51Nod  1268  和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...

  3. 51Nod 1428 活动安排问题

    51Nod   1428  活动安排问题 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 1428 活 ...

  4. 51Nod 1278 相离的圆

    51Nod 1278 相离的圆 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1278 1278 相离的圆 基 ...

  5. 51nod算法马拉松15

    智力彻底没有了...看来再也拿不到奖金了QAQ... A B君的游戏 因为数据是9B1L,所以我们可以hash试一下数据... #include<cstdio> #include<c ...

  6. 【51Nod 1501】【算法马拉松 19D】石头剪刀布威力加强版

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1501 dp求出环状不连续的前缀和,剩下东西都可以算出来,比较繁琐. 时间 ...

  7. 【51Nod 1622】【算法马拉松 19C】集合对

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1622 简单题..直接暴力快速幂 #include<cstdio&g ...

  8. 【51Nod 1616】【算法马拉松 19B】最小集合

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1616 这道题主要是查询一个数是不是原有集合的一个子集的所有数的gcd. ...

  9. 【51Nod 1674】【算法马拉松 19A】区间的价值 V2

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1674 对区间分治,统计\([l,r]\)中经过mid的区间的答案. 我的 ...

随机推荐

  1. 通过C#来开启、关闭、重启Windows服务

    通过C#开启服务需要这个C#程序有相应权限,比如服务的账户是Local System的就必须以管理员权限运行C#程序才能开启或关闭. 这里只写重启的方式(就是先关闭,后开启): // Security ...

  2. 解决author波浪线Spellchecker inspection helps locate typos and misspelling in your code, comments and literals, and fix them in one click

    自从把默认的头注释的author改成自己的名字以后越看越顺眼,但是发现名字下面一直有个波浪线,强迫症简直不能忍. 然后当你把鼠标放上去,再点击提示上的"more",会看到下面的提示 ...

  3. Button的五种点击事件

    1.内部类方式 class MyOnClickListener implements View.OnClickListener{ /** * Called when a view has been c ...

  4. useradd 命令 及 相关文件解读

    翻译自 man useradd 名称: 创建新用户或更新默认的新用户信息 快照:useradd -D 描述: 在不加-D参数调用时,useradd命令使用指定的参数和系统的默认值来创建新账户. 取决于 ...

  5. [数据清洗]- Pandas 清洗“脏”数据(二)

    概要 了解数据 分析数据问题 清洗数据 整合代码 了解数据 在处理任何数据之前,我们的第一任务是理解数据以及数据是干什么用的.我们尝试去理解数据的列/行.记录.数据格式.语义错误.缺失的条目以及错误的 ...

  6. Mysql的硬件优化和配置优化

    mysql数据库的优化,算是一个老生常谈的问题了,网上也有很多关于各方面性能优化的例子,今天我们要谈的是MySQL硬件优化和系统参数的优化-即优化my.cnf文件 MySQL的优化我分为两个部分,一是 ...

  7. Python sort方法

    官方文档: sort(*, key=None, reverse=False) This method sorts the list in place, using only < comparis ...

  8. 如何高逼格读取Web.config中的AppSettings

    http://edi.wang/post/2015/4/22/how-to-read-webconfig-appsettings-with-bigiblity 先插句题外话,下版本的ASP.NET貌似 ...

  9. Ubuntu下LAMP环境配置

    接下来是搭建个人学习环境,之前的随笔介绍了个人的网络配置,简单记录一下. 1. 安装apache: apt-get install apache2 2.  安装php5:apt-get install ...

  10. 前端学习_02_vps、web服务器、域名申请

    vps申请 国内比较好用的服务器:阿里云,青云:在国内申请ip比较方便,但是必须要备案域名,否则马上就会被封禁掉. 话说我也有点自己的思路想做个网站,服务器还真的是个问题. 小型的网站只需要ECS服务 ...