Codeforces Round #383 (Div. 1), problem: (E) Arpa’s abnormal DNA and Mehrdad’s deep interest Solution in C/C++

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <string.h>
#define rank _ran 

using namespace std;
typedef long long LL;
const int maxn = 200005;
const int inf = 1<<29;
const int tre = 120;

char S[maxn],T[maxn];
char str[maxn];int len,bit[maxn];
int bac[maxn],sa[maxn],SA[maxn];
int rank[maxn],Rank[maxn],h[maxn];

int ord[maxn],val[maxn];
int n,m,q,ans[maxn],id[maxn],idx;
int l[maxn],r[maxn],K[maxn],x[maxn],y[maxn];

struct RMQ {
	int rmq[17][maxn];
	void build(int len) {
		for (int j=1;j<17;j++)
		for (int i=0;i<=len;i++)
			rmq[j][i]=min(rmq[j-1][i],rmq[j-1][i-(1<<j-1)]);
	}
	int query(int l,int r) {
		if (l>r) return inf;
		int j=bit[r-l+1];
		return min(rmq[j][r],rmq[j][l+(1<<j)-1]);
	}
}suf,rmq,rmqj;
void upd(int &x,int y) {if (x>y) x=y;}

void suffix(int alp) {
	for (int i=0;i<=alp;i++) bac[i]=0;
	for (int i=1;i<=len;i++) bac[str[i]]++;
	for (int i=1;i<=alp;i++) bac[i]+=bac[i-1];
	for (int i=1;i<=len;i++) sa[bac[str[i]]--]=i;
	for (int i=1;i<=len;i++) rank[sa[i]]=rank[sa[i-1]]+(str[sa[i]]!=str[sa[i-1]]);
	for (int p=1;p<=len;p<<=1) {
		for (int i=1;i<=len;i++) bac[rank[sa[i]]]=i;
		for (int i=len;i>=1;i--) if (sa[i]>p) SA[bac[rank[sa[i]-p]]--]=sa[i]-p;
		for (int i=len-p+1;i<=len;i++) SA[bac[rank[i]]--]=i;
		for (int i=1;i<=len;i++) Rank[SA[i]]=Rank[SA[i-1]]+(rank[SA[i]]!=rank[SA[i-1]]||rank[SA[i]+p]!=rank[SA[i-1]+p]);
		swap(SA,sa);swap(rank,Rank);
	}
	for (int i=1;i<=len;i++) {
		int j=sa[rank[i]-1],k=max(0,h[i-1]-1);
		while (str[i+k]==str[j+k]) ++k;
		h[i]=k;suf.rmq[0][rank[i]]=k;
	}
	suf.build(len);
}
int lcp(int u,int v) {
	u=rank[u];v=rank[v];
	if (u>v) swap(u,v);
	return suf.query(u+1,v);
}
bool comp(int i,int j) {
	if (i==j) return false;
	bool sw=false;
	if (i>j) swap(i,j),sw=true;
	if (j-i>=m) {
		int d=(j-i)-m,w=j-i;
		int lcp1=lcp(n+1,i+1);
		if (lcp1<m) return (rank[n+1]<rank[i+1])^sw;
		int lcp2=lcp(i+1,i+1+m);
		if (lcp2<d) return (rank[i+1]<rank[i+1+m])^sw;
		int lcp3=lcp(i+1+d,n+1);
		if (lcp3<m) return (rank[i+1+d]<rank[n+1])^sw;
	}
	else {
		int d=m-(j-i),w=j-i;
		int lcp1=lcp(n+1,i+1);
		if (lcp1<w) return (rank[n+1]<rank[i+1])^sw;
		int lcp2=lcp(n+1+w,n+1);
		if (lcp2<d) return (rank[n+1+w]<rank[n+1])^sw;
		int lcp3=lcp(i+1,n+1+d);
		if (lcp3<w) return (rank[i+1]<rank[n+1+d])^sw;
	}
	return !sw;
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("interest.in","r",stdin);
		freopen("interest.out","w",stdout);
	#endif
	scanf("%s",S+1);n=strlen(S+1);
	scanf("%s",T+1);m=strlen(T+1);
	for (int i=1;i<=n;i++) str[++len]=S[i];
	for (int i=1;i<=m;i++) str[++len]=T[i];
	for (int i=2;i<=len;i++) bit[i]=bit[i>>1]+1;
	suffix(128);
	
	for (int i=0;i<=n;i++) ord[i]=i;
	sort(ord,ord+n+1,comp);
	for (int i=0;i<=n;i++) val[ord[i]]=i;
	for (int i=0;i<=n;i++) rmq.rmq[0][i]=val[i];
	rmq.build(n);
	
	scanf("%d",&q);
	for (int i=0;i<=q;i++) ans[i]=-1;
	for (int i=1;i<=q;i++)
		scanf("%d %d %d %d %d",&l[i],&r[i],&K[i],&x[i],&y[i]); 
	for (int i=1;i<=q;i++)
	if (ans[i]<0) {
		if (K[i]>tre) {
			ans[i]=inf;
			for (int j=0;j*K[i]<=r[i];j++)
				upd(ans[i],rmq.query(max(l[i],j*K[i]+x[i]),min(r[i],j*K[i]+y[i])));
		}
		else {
			idx=0;
			for (int j=0;j<K[i];j++)
			for (int k=0;k*K[i]+j<=n;k++)
				rmqj.rmq[0][idx]=val[k*K[i]+j],id[k*K[i]+j]=idx++;
			rmqj.build(idx);
			for (int j=i;j<=q;j++)
			if (K[j]==K[i]) {
				ans[j]=inf;
				for (int k=x[j];k<=y[j];k++) {
					int L=l[j]/K[j]*K[j]+k;
					int R=r[j]/K[j]*K[j]+k;
					while (L<l[j]) L+=K[j];
					while (R>r[j]) R-=K[j];
					if (L<=R) upd(ans[j],rmqj.query(id[L],id[R]));
				}
			}
		}
	}
	for (int i=1;i<=q;i++)
		printf("%d ",ans[i]!=inf?ord[ans[i]]:-1);
	return 0;
}

Related Reading: Explore our comprehensive guide on Google Trends & technology research strategies.

Advertisement

spot_img

I Tried Viral Kitchen...

📢 As an Amazon Associate, I earn from qualifying...

How Viral Kitchen Gadgets...

📢 As an Amazon Associate, I earn from qualifying...

Avengers: Armageddon #5 Preview:...

Tension Reaches a Breaking Point in Avengers: Armageddon #5 Marvel's...

7 Reasons This Viral...

📢 As an Amazon Associate, I earn from qualifying...

Can baby deer walk...

Witnessing a newborn animal take its very first steps...

Is the Anker MagGo...

The Long-Term Durability Assessment: 6 Months as a Daily...

I Tried Viral Kitchen Gadgets For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell...

How Viral Kitchen Gadgets Is Quietly Changing the Game

📢 As an Amazon Associate, I earn from qualifying purchases.Most products in this category disappoint within weeks. Cheap materials, vague instructions, and results that...

Avengers: Armageddon #5 Preview: Trust Issues Intensify

Tension Reaches a Breaking Point in Avengers: Armageddon #5 Marvel's mightiest heroes are once again staring down total catastrophe, but this time, the greatest danger...

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.I stumbled across this while desperately trying to fix a recurring problem in my routine....

Can baby deer walk right away?

Witnessing a newborn animal take its very first steps is one of the most heartwarming sights in the natural world. In the wild, however,...

Is the Anker MagGo 3-in-1 Worth $100? Long-Term Durability Review

The Long-Term Durability Assessment: 6 Months as a Daily Driver Spending $100 on a smartphone charging accessory is a significant consumer investment. When budget 3-in-1...

Anker MagGo 3-in-1 vs Belkin BoostCharge Pro 3-in-1: Which Is Better?

The Heavyweight Showdown: Anker MagGo vs. Belkin BoostCharge Pro For the past four years, if an Apple user wanted the absolute best 3-in-1 wireless charging...

Best Protective Travel Pouches and Cases for Anker MagGo 3-in-1

Protecting Your Premium Travel Power Investment The Anker MagGo 3-in-1 Foldable Charging Station is engineered for life on the move. With its collapsible form factor,...

How Fast Does Anker MagGo Charge iPhone 15 and 16 from 0 to 100%?

Putting Qi2 15W Speed Claims to the Test When wireless charging standards make technological leaps, consumer skepticism is natural. For years, marketing promises of "fast...