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 ...
随机推荐
- oracle02--多表关联查询
1. 多表(关联)查询 多表查询也称之为关联查询.多表关联查询等,主要是指通过多个表的关联来获取数据的一种方式. 1.1. 多表映射关系 一对多:A表的一行数据,对应B表中的多条.如:一个部门可以对应 ...
- Ubuntu自定义终端窗口位置
方法一: 自定义终端启动快捷键 具体方法是自定义一个快速启动终端的快捷键,附带设置终端启动时的位置参数.首先获得需要放置窗口的目标位置信息,可以通过终端命令“ xwininfo ”来获得.步骤是首先打 ...
- 2016.07.13-map的使用(以leetcode1-Two Sum为例)
map的使用 1.unordered_map和map的区别 2.如何用 3.for (int a : nums1) 4.to_string() 5.map的应用 1.unordered_map和map ...
- vue中使用cookie记住用户上次选择(本次例子中为下拉框)
最近工作中碰到一个需求,添加一条数据时,自动记住上次选择的下拉框的数据,刚开始觉得没思路,后来请教了项目组长,组长直接一句,这不很简单吧,直接用cookie,我:....... 好吧,都王的差不多了, ...
- static, const 和 static const 变量的初始化问题
const 常量的在超出其作用域的时候会被释放,但是 static 静态变量在其作用域之外并没有释放,只是不能访问. static 修饰的是静态变量,静态函数.对于类来说,静态成员和静态函数是属于整个 ...
- php环境搭建 (window环境下 eclipse+Wampserver)
看了好多的环境搭建感觉好复杂呀,自己搞了一下简单的可以用了 php的手册 http://www.php.net/manual/zh/ 一,下载 1,下载eclipse http://www.ecl ...
- 【Android开发】之MediaPlayer的错误分析
最近在做媒体播放器,使用了Android自带的MediaPlayer,经常性会碰到MediaPlayer报错的情况,找过网上的,感觉总结的不是很好或者比较散.下面,我来总结一下使用MediaPlaye ...
- Python学习笔记:个税起征点上调至5000,算一算少交多少税?
一.旧税率表与新税率表比较 以前起征点是3500,2018年10月1日起起征点正式修改为5000,下面我们用Python来分别计算新旧个人所得税分别为多少? 二.旧的个人所得税 import sys ...
- 如何使用django+celery+RabbitMQ实现异步执行
1)安装需要安装RabbitMQ.Celery和Django-celeryCelery和Django-celery的安装直接pip就好 2)修改settings.py在INSTALLED_APPS中加 ...
- Writing a Kernel in C++
*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...