题意

给出一个字符串\(s\)和\(q\)个询问。

每次询问给出两个长度分别为\(k,l\)的序列\(a\)和序列\(b\)。

求\(\sum_{i=1}^{k}\sum_{j=1}^{l}lcp(s[a_i…n],s[b_j…n])\)

Solution

\(SA\)练习题。

求出\(height\)数组后,每次询问相当于询问\(l*k\)个区间\(min\)之和。

岂不单调栈?

对没错,这个题解就是提供给你代码对拍的

#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define cross(i,k) for (register int i=first[k];i;i=last[i])
using namespace std;
typedef long long ll;
inline ll read(){
ll x=0;int ch=getchar(),f=1;
while (!isdigit(ch)&&(ch!='-')&&(ch!=EOF)) ch=getchar();
if (ch=='-'){f=-1;ch=getchar();}
while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int N = 2e5+10;
int n,Q,l[N],k[N];
char s[N];
int SA[N],height[N],rk[N],cnt[N],x[N],y[N];
inline void RadixSort(){
int Max=0;
For(i,1,n) cnt[x[i]]++,Max=max(Max,x[i]);
For(i,1,Max) cnt[i]+=cnt[i-1];
Dow(i,n,1) SA[cnt[x[y[i]]]--]=y[i];
For(i,1,Max) cnt[i]=0;
}
inline void GetSA(){
For(i,1,n) x[i]=s[i],y[i]=i;
RadixSort();
for (int i=1,p;p<n;i<<=1){
p=0;
For(j,n-i+1,n) y[++p]=j;
For(j,1,n) if (SA[j]>i) y[++p]=SA[j]-i;
RadixSort(),swap(x,y),x[SA[1]]=p=1;
For(j,2,n) x[SA[j]]=(y[SA[j]]==y[SA[j-1]]&&y[SA[j]+i]==y[SA[j-1]+i])?p:++p;
}
For(i,1,n) rk[SA[i]]=i;
int now=0;
For(i,1,n){
if (rk[i]==1) continue;now=max(now-1,0);
for (int j=SA[rk[i]-1];j+now<=n&&i+now<=n&&s[j+now]==s[i+now];now++);
height[rk[i]]=now;
}
}
int Min[N][20],Log[N];
inline void init(){
For(i,1,n) Log[i]=log(i)/log(2);
For(i,1,n) Min[i][0]=height[i];
For(j,1,Log[n]) For(i,1,n-(1<<j)+1) Min[i][j]=min(Min[i][j-1],Min[i+(1<<j-1)][j-1]);
}
inline int Query(int l,int r){
int L=Log[r-l+1];
return min(Min[l][L],Min[r-(1<<L)+1][L]);
}
struct node{
int x,y;
}a[N<<1];
inline bool cmp(node a,node b){return a.x==b.x?a.y<b.y:a.x<b.x;}
int top,q[N<<1],c[N<<1];
ll Sum,ans;
inline void solve(int m,int M){
int cnt=0;
For(i,1,M) a[++cnt]=(node){rk[read()],1};
For(i,1,m) a[++cnt]=(node){rk[read()],0};
sort(a+1,a+1+cnt,cmp),ans=top=Sum=0;
a[cnt+1]=(node){-1,0};
int r=cnt;while (a[r].y==1) r--;
Dow(i,r,1){
if (i!=r){
int x=Query(a[i].x+1,a[i+1].x),s=a[i+1].y^1;
while (x<=q[top]&&top) Sum-=c[top]*q[top],s+=c[top--];
q[++top]=x,c[top]=s,Sum+=1ll*s*x;
}
if (a[i].y) ans+=Sum;
else if (a[i+1].x==a[i].x) ans+=n-SA[a[i].x]+1;
}
For(i,2,cnt) if (a[i].x==a[i-1].x) swap(a[i],a[i-1]);
top=Sum=0;
r=1;while (a[r].y==1) r++;
For(i,r,cnt){
if (i!=r){
int x=Query(a[i-1].x+1,a[i].x),s=a[i-1].y^1;
while (x<=q[top]&&top) Sum-=c[top]*q[top],s+=c[top--];
q[++top]=x,c[top]=s,Sum+=1ll*s*x;
}
if (a[i].y) ans+=Sum;
}
}
ll Ans[N];
int main(){
n=read(),Q=read(),scanf("%s",s+1);
GetSA(),init();
For(i,1,Q) solve(read(),read()),Ans[i]=ans;
For(i,1,Q) printf("%lld\n",Ans[i]);
}

Codeforces 1073G Yet Another LCP Problem $SA$+单调栈的更多相关文章

  1. Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)

    原文链接https://www.cnblogs.com/zhouzhendong/p/9256033.html 题目传送门 - CF873F 题意 给定长度为 $n$ 的字符串 $s$,以及给定这个字 ...

  2. Codeforces 1156E Special Segments of Permutation(单调栈)

    可以用单调栈直接维护出ai所能覆盖到的最大的左右范围是什么,然后我们可以用这个范围暴力的去查询这个区间的是否有满足的点对,一个小坑点,要对左右区间的大小进行判断,只需要去枚举距离i最近的一段区间去枚举 ...

  3. Codeforces Round #622 (Div. 2)C(单调栈,DP)

    构造出的结果一定是一个单峰/\这种样子的 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ...

  4. Codeforces Round #305 (Div. 2) D 维护单调栈

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  5. Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈

    Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...

  6. Codeforces 802I Fake News (hard) (SA+单调栈) 或 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/9026184.html 题目传送门 - Codeforces 802I 题意 求一个串中,所有本质不同子串的出现次 ...

  7. CodeForces 548D 单调栈

    Mike and Feet Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Subm ...

  8. Codeforces 1107G Vasya and Maximum Profit [单调栈]

    洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...

  9. Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)

    https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...

随机推荐

  1. js 禁用右键菜单、拖拽、选中、复制

    //禁用拖拽 document.ondragstart = function () { return false; }; /** * 禁用右键菜单 */ document.oncontextmenu ...

  2. 《区块链100问》第85集:资产代币化之对标美元USDT

    USDT是Tether公司推出的对标美元(USD)的代币Tether USD.1USDT=1美元,用户可以随时使用USDT与USD进行1:1兑换.Tether公司执行1:1准备金保证制度,即每个USD ...

  3. 2016.6.19——C++杂记

    C++杂记 补充的小知识点: 1.while(n--)和while(--n)区别: while(n--)即使不满足也执行一次循环后跳出. while(--n)不满足直接跳出循环,不执行语句. 用cou ...

  4. iframe内部刷新后跳转锚点

    开发过程中需要在iframe内容页中点击刷新按钮刷新页面并跳转至页面底部,编写js函数在url后面加上锚点名称#mao,但发现并未达到预期效果,通过测试发现锚点只有在第一次访问页面的时候才会生效,所有 ...

  5. docker stack 部署 mysql 5.6

    =============================================== 2018/7/1_第1次修改                       ccb_warlock === ...

  6. Vue项目之IE下打开页面是空白

    原因是:Babel 默认只转换新的 JavaScript 句法(syntax),而不转换新的 API ,比如 Iterator.Generator.Set.Maps.Proxy.Reflect.Sym ...

  7. sizeof求结构体大小

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  8. C++ "multiple definition of .. first defined here"

    C++ "multiple definition of .. first defined here" 在C++中,有时候需要在不同文件中使用同一个变量.对于这类变量如果处理不当,很 ...

  9. PHP性能调优---PHP调试工具Xdebug安装配置教程

    说到PHP代码调试,对于有经验的PHPer,通过echo.print_r.var_dump函数,或PHP开发工具zend studio.editplus可解决大部分问题,但是对于PHP入门学习的童鞋来 ...

  10. day9--回顾

    线程  vs   进程 进程:一堆资源集的集合.线程:操作系统能够调度的最小单位. 进程和线程的谁快是误区,进程至少包含一个线程,是没有可比性的. 线程:共享内存,两个线程同时操作一个数据,要加锁.全 ...