CF741E Arpa’s abnormal DNA and Mehrdad’s deep interest

记 \(R_{i}\) 表示把 \(T\) 插入在 \(S\) 的第 \(i\) 位后组成的字符串。有 \(q\) 组询问,给定 \((x,y,l,r)\),求 \(\min_{i} R_{i},({i\in[l,r],i\%k\in[x,y]})\)。

一个暴力的想法是先把 \(R_{i}\) 的排名求出来,这显然可以 SA 或者 二分 + Hash 求 lcp。考虑根号分治:对于 \(k>\sqrt n\),显然不会有超过 \(\sqrt n\) 个连续区间,暴力查 ST 表即可。对于 \(k\le \sqrt n\),离线处理,对于每个 \(k\),我们把 \(\%k\) 相同的点拿出来,发现对于一个询问仍然是区间查询,且一个询问只会被拆分成 \(k\) 个。仍然可以暴力建立 ST 表,因为数组大小之和是调和级数。询问的个数复杂度是 \(O(n\sqrt n)\)。

故总的复杂度为 \(O(n\sqrt n+n\log^2 n+n\lg\log n)\)。实现时需注意常数。

#include<bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn=2e5+5, mo=998244353, B=335; int n,m,q;
char s[maxn], t[maxn];
ll f[maxn], g[maxn], pw[maxn];
int sa[maxn], rk[maxn], lg[maxn], w[maxn][20]; int calc(int x,int len) {
if(len<=x) return f[len];
if(len<=x+m) return (f[x]*pw[len-x]%mo+g[len-x])%mo;
return ((f[x]*pw[m]%mo+g[m])%mo*pw[len-x-m]%mo+f[len-m]-f[x]*pw[len-x-m]%mo+mo)%mo;
} bool cmp(int x,int y) {
int l=1, r=n+m, pos=0;
while(l<=r) {
int mid=l+r>>1;
if(calc(x,mid)==calc(y,mid)) pos=mid, l=mid+1;
else r=mid-1;
}
if(pos==n+m) return x<y;
++pos;
char c1,c2;
if(pos<=x) c1=s[pos];
else if(pos<=x+m) c1=t[pos-x];
else c1=s[pos-m];
if(pos<=y) c2=s[pos];
else if(pos<=y+m) c2=t[pos-y];
else c2=s[pos-m];
return c1<c2;
} struct node {
int x,y,l,r,id;
};
vector<node> e[B];
int ans[maxn]; int ask(int l,int r) {
if(l>r) return n+1;
int k=lg[r-l+1];
return min(w[l][k],w[r-(1<<k)+1][k]);
} int ww[maxn][20]; int ask2(int l,int r) {
if(l>r) return n+1;
int k=lg[r-l+1];
return min(ww[l][k],ww[r-(1<<k)+1][k]);
} int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin>>s+1>>t+1>>q;
n=strlen(s+1), m=strlen(t+1);
pw[0]=1; lg[1]=0;
for(int i=2;i<=n+1;i++) lg[i]=lg[i>>1]+1;
for(int i=1;i<=n+m;i++) pw[i]=pw[i-1]*131%mo;
for(int i=1;i<=n;i++) f[i]=(f[i-1]*131+s[i]-'a')%mo;
for(int i=1;i<=m;i++) g[i]=(g[i-1]*131+t[i]-'a')%mo;
for(int i=0;i<=n;i++) sa[i]=i;
stable_sort(sa,sa+n+1,cmp);
/*
for(int i=0;i<=n;i++) {
cout<<sa[i]<<'\n';
for(int j=1;j<=sa[i];j++) cout<<s[j];
for(int j=1;j<=m;j++) cout<<t[j];
for(int j=sa[i]+1;j<=n;j++) cout<<s[j];
cout<<'\n';
}
*/
for(int i=0;i<=n;i++) rk[sa[i]]=i;
// for(int i=0;i<=n;i++) w[i][0]=rk[i];
for(int j=1;(1<<j)<=n+1;j++) {
for(int i=0;i+(1<<j)-1<=n;i++) {
w[i][j]=min(w[i][j-1],w[i+(1<<j-1)][j-1]);
}
}
sa[n+1]=-1;
for(int i=1;i<=q;i++) ans[i]=n+1;
for(int i=1;i<=q;i++) {
int l,r,k,x,y; cin>>l>>r>>k>>x>>y;
if(k>=B) {
int ml=l%k, res=n+1;
if(ml<x) l+=x-ml;
else if(ml>y) l+=(x+k-ml);
else {
res=min(ask(l,min(l+y-ml,r)),res);
l+=(x+k-ml);
}
int mr=r%k;
if(mr>y) r-=mr-y;
else if(mr<x) r-=(mr+k-y);
else {
res=min(ask(max(l,r-(mr-x)),r),res);
r-=(mr+k-y);
}
while(l<=r) {
res=min(res,ask(l,l+y-x));
l+=k;
}
ans[i]=res;
}else {
e[k].push_back((node){x,y,l,r,i});
}
}
for(int k=1;k<B;k++) {
for(int v=0;v<k;v++) {
int cnt=0;
for(int d=v;d<=n;d+=k) ww[++cnt][0]=rk[d];
for(int j=1;(1<<j)<=cnt;j++) {
for(int i=1;i+(1<<j)-1<=cnt;i++) {
ww[i][j]=min(ww[i][j-1],ww[i+(1<<j-1)][j-1]);
}
}
for(auto p:e[k]) {
if(p.x>v||p.y<v) continue;
int l=p.l, r=p.r;
l=l+(v-(l%k)+k)%k;
r=r-((r%k)-v+k)%k;
l=(l-v)/k+1, r=(r-v)/k+1;
ans[p.id]=min(ans[p.id],ask2(l,r));
}
}
}
for(int i=1;i<=q;i++) cout<<sa[ans[i]]<<' '; cout<<'\n';
return 0;
}

题解 CF741E Arpa’s abnormal DNA and Mehrdad’s deep interest的更多相关文章

  1. 【题解】Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths Codeforces 741D DSU on Tree

    Prelude 很好的模板题. 传送到Codeforces:(* ̄3 ̄)╭ Solution 首先要会DSU on Tree,不会的看这里:(❤ ω ❤). 众所周知DSU on Tree是可以用来处 ...

  2. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...

  3. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)

    codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  4. Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环

    题目链接:http://codeforces.com/contest/742/problem/C C. Arpa's loud Owf and Mehrdad's evil plan time lim ...

  5. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)

    题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...

  6. Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution —— 异或

    题目链接:http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible so ...

  7. code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  8. Arpa's weak amphitheater and Mehrdad's valuable Hoses

    Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit per ...

  9. Arpa's loud Owf and Mehrdad's evil plan

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  10. Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan

    C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...

随机推荐

  1. 春松客服入驻Rainbond开源应用商店

    "做好开源客服系统" 春松客服是拥有坐席管理.渠道管理.机器人客服.数据分析.CRM 等功能于一身的新一代客服系统.将智能机器人与人工客服完美融合,同时整合了多种渠道,结合 CRM ...

  2. yum install 出错

    ``` yum -y install ansible ``` 出错 一直在刷屏,刚开始以为在安装但是最后没有安装成功 百度发现是这是redhat7和8的yum源混用 1.先确认本机器系统信息 cat ...

  3. 【技巧】JS代码这么写,前端小姐姐都会爱上你

    前言 缘由 JS代码小技巧,教你如何守株待妹 你想听的故事: 顶着『前端小王子』的称号,却无法施展自己的才能. 想当年本狗赤手空拳打入前端阵地,就是想通过技术的制高点来带动前端妹子.奈何时不待我,前端 ...

  4. VisionPro学习笔记(7)——FitLineTool

    如果需要了解其他图像处理的文章,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice ...

  5. .NET5 IIS ASP.NET CORE 部署时 HTTP Error 502.5 - ANCM Out-Of-Process Startup Failure

    .NET5 IIS ASP.NET CORE 部署时 HTTP Error 502.5 - ANCM Out-Of-Process Startup Failure 部署机器只安装了dotnet-hos ...

  6. SRE 排障利器,接口请求超时试试 httpstat

    夜莺资深用户群有人推荐的一个工具,看了一下真挺好的,也推荐给大家. 需求场景 A 服务调用 B 服务的 HTTP 接口,发现 B 服务返回超时,不确定是网络的问题还是 B 服务的问题,需要排查. 工具 ...

  7. Lecture4

    Smiling & Weeping ---- 行于山水之间 权且停留 无所谓风起叶落,浮光敛形 此刻   身即自由 第四章 Git 工具 Author: Martin 本章主要介绍 Git 常 ...

  8. Linux unset命令用法

    Linux unset命令用于删除变量或函数. unset为shell内建指令,可删除变量或函数 参数: -f 仅删除函数 -v 仅删除变量 [root@localhost ~]# yangzc=&q ...

  9. Mac修改文件名的颜色

    文章目录 前言 文件类型 LSCOLORS介绍 颜色 如何设置LSCOLORS环境变量 前言 Mac中修改文件名颜色是通过LSCOLORS这个环境变量来控制的 文件类型 11种文件类型信息如下所示 序 ...

  10. 国产化率100%!全志科技A40i工业核心板规格书资料分享

    1.核心板简介 创龙科技SOM-TLA40i是一款基于全志科技A40i处理器设计的4核ARM Cortex-A7国产工业核心板,每核主频高达1.2GHz. 核心板通过邮票孔连接方式引出CSI.TVIN ...