题意:给串s,每次询问k个数a,l个数b,问a和b作为后缀的lcp的综合

题解:和bzoj3879类似,反向sam日神仙...lcp就是fail树上的lca.把点抠出来建虚树,然后在上面dp即可.(感觉之前写的svt什么玩意)

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000009
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=200000+10,maxn=1000000+10,inf=0x3f3f3f3f; char s[N];
struct SAM{
int last,cnt;
int ch[N<<1][26],fa[N<<1],l[N<<1];
int pos[N<<1],dep[N<<1],f[N<<1][20],dfn[N<<1],res;
vi v[N<<1];
void ins(int c){
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
if(!p)fa[np]=1;
else
{
int q=ch[p][c];
if(l[p]+1==l[q])fa[np]=q;
else
{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
}
}
}
void build()
{
last=cnt=1;
int len=strlen(s+1);
reverse(s+1,s+1+len);
for(int i=1;s[i];i++)ins(s[i]-'a'),pos[i]=last;
for(int i=1;i<=cnt;i++)v[fa[i]].pb(i);
dfs(1);
for(int i=1;i<20;i++)for(int j=1;j<=cnt;j++)
f[j][i]=f[f[j][i-1]][i-1];
}
void dfs(int u)
{
f[u][0]=fa[u];dfn[u]=++res;
for(int x:v[u])
{
// printf("%d %d\n",u,x);
dep[x]=dep[u]+1;
dfs(x);
}
}
int lca(int x,int y)
{
if(dep[x]>dep[y])swap(x,y);
for(int i=19;~i;i--)if(((dep[y]-dep[x])>>i)&1)y=f[y][i];
if(x==y)return x;
for(int i=19;~i;i--)if(f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
return f[x][0];
}
}sam;
vi v[N*2],in;
void add1(int a,int b){v[a].pb(b),in.pb(a);in.pb(b);}
int st[N*2],top,a[N*2];
ll dp[N*2][2],ans;
void ins(int x)
{
if(!top){st[++top]=x;return ;}
int lc=sam.lca(st[top],x);
while(top>1&&sam.dep[st[top-1]]>sam.dep[lc])
add1(st[top-1],st[top]),top--;
if(top>=1&&sam.dep[st[top]]>sam.dep[lc])
add1(lc,st[top]),top--;
if(!top||sam.dep[st[top]]<sam.dep[lc])st[++top]=lc;
st[++top]=x;
}
bool cmp(int a,int b){return sam.dfn[a]<sam.dfn[b];}
void dfs(int u)
{
ans+=1ll*sam.l[u]*dp[u][0]*dp[u][1];
for(int x:v[u])
{
dfs(x);
ans+=1ll*sam.l[u]*dp[u][0]*dp[x][1];
ans+=1ll*sam.l[u]*dp[x][0]*dp[u][1];
dp[u][0]+=dp[x][0],dp[u][1]+=dp[x][1];
}
}
int main()
{
int n,q;scanf("%d%d%s",&n,&q,s+1);
sam.build();
while(q--)
{
int k,l;scanf("%d%d",&k,&l);
for(int i=0;i<k;i++)
{
scanf("%d",&a[i]),a[i]=sam.pos[n+1-a[i]];
dp[a[i]][0]++;
}
for(int i=0;i<l;i++)
{
scanf("%d",&a[k+i]),a[k+i]=sam.pos[n+1-a[k+i]];
dp[a[k+i]][1]++;
}
sort(a,a+k+l,cmp);k+=l;k=unique(a,a+k)-a;
ans=top=0;ins(1);
for(int i=0;i<k;i++)if(a[i]!=1)ins(a[i]);
while(top>=2)add1(st[top-1],st[top]),top--;
dfs(1);
printf("%lld\n",ans);
for(int x:in)v[x].clear(),dp[x][0]=dp[x][1]=0;
in.clear();
}
return 0;
}
/******************** ********************/

Educational Codeforces Round 53 (Rated for Div. 2)G. Yet Another LCP Problem的更多相关文章

  1. Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)

    这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊). 比赛传送门:http://codeforces.com/contest/1073 A. Diverse Substring ...

  2. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  3. Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)

    题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...

  4. Educational Codeforces Round 53 (Rated for Div. 2)

    http://codeforces.com/contest/1073 A. Diverse Substring #include <bits/stdc++.h> using namespa ...

  5. [codeforces][Educational Codeforces Round 53 (Rated for Div. 2)D. Berland Fair]

    http://codeforces.com/problemset/problem/1073/D 题目大意:有n个物品(n<2e5)围成一个圈,你有t(t<1e18)元,每次经过物品i,如果 ...

  6. Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum

    https://codeforces.com/contest/1073/problem/E 题意 求出l到r之间的符合要求的数之和,结果取模998244353 要求:组成数的数位所用的数字种类不超过k ...

  7. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

    任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...

  8. Educational Codeforces Round 53 (Rated for Div. 2) D. Berland Fair

    题意:一个人  有T块钱 有一圈商店 分别出售 不同价格的东西  每次经过商店只能买一个  并且如果钱够就必须买 这个人一定是从1号店开始的!(比赛的时候读错了题,以为随意起点...)问可以买多少个 ...

  9. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot

    题意:给出一段操作序列 和目的地 问修改(只可以更改 不可以删除或添加)该序列使得最后到达终点时  所进行的修改代价最小是多少 其中代价的定义是  终点序号-起点序号-1 思路:因为代价是终点序号减去 ...

随机推荐

  1. HVP plan

    HVP,hier verification plan,建立整个验证的plan,在验证后期,通过vcs的coverage db可以直接进行反标, 包括反标code coverage,function c ...

  2. zw版足彩大数据&报价

    zw版足彩大数据&报价 ::zw增强版足彩大数据,文件名后缀是'.dat' ::文件格式是标准文本格式,逗号分隔 ::zw增强版,在标准版赔率基础上,增加了倒数.比率两组归一化数据 ::zw版 ...

  3. 运输计划NOIP2015Day2T3

    运输计划 题目描述 公元 2044 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条 航道连通了 L 国的所有星球. 小 P 掌管一 ...

  4. Flask实战-留言板-安装虚拟环境、使用包组织代码

    Flask实战 留言板 创建项目目录messageboard,从GreyLi的代码中把Pipfile和Pipfile.lock文件拷贝过来,这两个文件中定义了虚拟环境中需要安装的包的信息和位置,进入m ...

  5. mysql知识点汇总

    1. 数据库的安装 2. 数据库设计需要注意什么 3. SQL语句优化 4. 怎样处理慢查询? 5. 怎样更好的利用数据库索引? 6. 事务隔离级别有哪些?怎么实现的? 7. 数据库锁有哪些? 8. ...

  6. 关于linux系统CPU篇--->上下文切换

    1.什么是CPU上下文切换? linux是一个多任务操作系统,它支持远大于CPU数量的任务同时运行,当然这些任务实际上并不是真的同时在运行,而是因为系统在很短的时间内,将CPU轮流分配给它们,造成多任 ...

  7. linux windows 格式化一块大于2 TiB硬盘

    转自:https://help.aliyun.com/document_detail/34377.html?spm=a2c4g.11186623.2.10.17447386JrLBNR#concept ...

  8. java线程学习之volatile关键字

    volatile变量的主要作用:是使变量在多个线程间可见. 在java中每一个线程都会有一块工作内存区,其中存放着所有线程共享的主内存的变量值的拷贝.当线程执行时,它在自己的工作内存区操作这些变量,为 ...

  9. Gatling实战(二)

    在上一篇实战讲解了Gatling的用例,不过还没涉及到性能方面的内容,其实用例中的最后一句就和性能有关了 setUp(scn.inject(atOnceUsers(1)).protocols(http ...

  10. radhat6.6上安装oracle12c RAC (二)

    二.GI(Grid Infrastructure)安装 首先将安装包传到node1的目录 2.1 GI软件安装 2.1.1.解压安装包 #su - grid解压 GRID 到 GRID用户的$ORAC ...