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. SwiftUI(二)- 页面导航NavigationLink和Sheet窗口(模态视图)

    NavigationLink 官方文档对NavigationLink的定义: A button that triggers a navigation presentation when pressed ...

  2. 基于docker的cicd

    基于docker的cicd 飞书链接: https://dlk2qiw7lh.feishu.cn/docs/doccnyu14HTaamVzASIXreCSNjf 相关软件 链接:https://pa ...

  3. 009. gitlab备份和恢复

    gitlab备份 #1. 创建添加配置文件 vim /etc/gitlab/gitlab.rb 文件尾添加: gitlab_reils['backup_path'] = '/data/backup/g ...

  4. 面试必问:MySQL死锁 是什么,如何解决?(史上最全)

    MySQL死锁接触少,但面试又经常被问到怎么办? 最近有小伙伴在面试的时候,被问了MySQL死锁,如何解决? 虽然也回答出来了,但是不够全面体系化, 所以,小北给大家做一下系统化.体系化的梳理,帮助大 ...

  5. SELinux策略语法以及示例策略

    首发公号:Rand_cs SELinux策略语法以及示例策略 本文来讲述 SELinux 策略常用的语法,然后解读一下 SELinux 这个项目中给出的示例策略 安全上下文 首先来看一下安全上下文的格 ...

  6. ETL工具-nifi干货系列 第九讲 处理器EvaluateJsonPath,根据JsonPath提取字段

    1.其实这一节课本来按照计划一起学习RouteOnAttribute处理器(相当于java中的ifelse,switch case 控制语句),但是在学习的过程中遇到了一些问题.RouteOnAttr ...

  7. CompatTelRunner CPU 占用 22% win10 笔记本常常无故风扇狂转

    CompatTelRunner CPU 占用 22% win10 笔记本常常无故风扇狂转 CompatTelRunner.exe is also known as Windows Compatibil ...

  8. svn服务端安装和使用

    首先去官网下载安装包 点我下载 下载完了以后选择安装路径然后一直next就可以了 安装完了以后在开始菜单里面找到svn 打开  如何使用? 这里是创建代码管理的存储库 点击 repositories ...

  9. ServiceMesh、SideCar和Istio

    Service Mesh简介 Service Mesh直译过来就是服务网格,而他的架构就是一个个微服务组成的网络. Sidecar简介 Service Mesh中的节点就是Sidecar节点. sid ...

  10. Bike Sharing Analysis(二)- 假设检验方法

    假设检验 假设检验是推论统计学(inferential statistics)的一个分支,也就是对一个较小的.有代表性的数据组(例如样本集合)进行分析与评估,并依此推断出一个大型的数据组(例如人口)的 ...