Codeforces 1073G Yet Another LCP Problem $SA$+单调栈
题意
给出一个字符串\(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$+单调栈的更多相关文章
- Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)
原文链接https://www.cnblogs.com/zhouzhendong/p/9256033.html 题目传送门 - CF873F 题意 给定长度为 $n$ 的字符串 $s$,以及给定这个字 ...
- Codeforces 1156E Special Segments of Permutation(单调栈)
可以用单调栈直接维护出ai所能覆盖到的最大的左右范围是什么,然后我们可以用这个范围暴力的去查询这个区间的是否有满足的点对,一个小坑点,要对左右区间的大小进行判断,只需要去枚举距离i最近的一段区间去枚举 ...
- Codeforces Round #622 (Div. 2)C(单调栈,DP)
构造出的结果一定是一个单峰/\这种样子的 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ...
- 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 ...
- Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈
Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...
- Codeforces 802I Fake News (hard) (SA+单调栈) 或 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/9026184.html 题目传送门 - Codeforces 802I 题意 求一个串中,所有本质不同子串的出现次 ...
- CodeForces 548D 单调栈
Mike and Feet Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Subm ...
- Codeforces 1107G Vasya and Maximum Profit [单调栈]
洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...
- Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)
https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...
随机推荐
- 【算法】Base64编码
1.说明 Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法. 2.编码 ASCII码 -> 十六进制码 -> ...
- 我所知道的MVVM框架(转 司徒大大 )
RubyLouvre commented on 6 Sep 2014 avalon http://avalonjs.github.io/ (使用Object.defineProperties. V ...
- sicily 1459. The Dragon of Loowater
Time Limit: 1sec Memory Limit:32MB Description Once upon a time, in the Kingdom of Loowa ...
- 正则表达式基础->
描述:(grep) 正则表达式是一种字符模式,用于在查找过程中匹配指定的字符.在大多数程序里,正则表达式都被置于两个正斜杠之间,它匹配被查找的行中任何位置出现的相同模式 基础正则表达式 正则表达式 描 ...
- EPC摘抄
S6a MME – HSS 完成用户位置信息的交换和用户签约信息的管理,传送控制面信息 Diameter MME:主要负责信令处理及移动性管理,功能包括:NAS信令及其安全:跟踪区域(Tracking ...
- linux编译警告 will be initialized after
http://blog.chinaunix.net/uid-17019762-id-3152012.html 作为一个有强迫症的人,实在是受不了 warning 的存在 这个warning是由于初始化 ...
- yum命令安装软件时,出现--centos 7 安装apache 出现 Could not resolve host: mirrorlist.centos.org; 未知的错误"--CentOS网络设置 couldn't resolve host 'mirrorlist.centos.org问题解决
CentOS网络设置 couldn't resolve host 'mirrorlist.centos.org问题解决 今天在虚拟机上安装完CentOS6.5之后,首次使用时yum命令安装软件时,出现 ...
- git —— 远程仓库(操作)
运行目录:本地仓库目录 1.本地关联远程仓库 $ git remote add origin 你的远程库地址(SSH和HTTP都可以) 2.远程仓库为空,可选择合并远程仓库和本地仓库,远程库不为空时, ...
- 关于UrlEncode 一团乱麻的问题,后续彻底理解。Java中的 URLEncoder 与 URLDecoder无bug
很多开放平台都是小白开发的,对这个urlencode理解的不到位,他们总是认为java官方的urlencode有bug,需要 URLEncoder.encode("Hello World&q ...
- volatile 学习笔记
全面理解Java内存模型(JMM)及volatile关键字 正确使用 Volatile 变量 Java内存模型 在并发编程中,需要处理两个关键问题:线程之间如何通信及线程之间如何同步.通信是指线程之间 ...