QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#307848#7876. Cyclic SubstringsconfesserCompile Error//C++177.0kb2024-01-19 10:42:022024-01-19 10:42:03

Judging History

你现在查看的是最新测评结果

  • [2024-01-19 10:42:03]
  • 评测
  • [2024-01-19 10:42:02]
  • 提交

answer

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2") 

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
using pi=pair<int,int>;
#define F(i,a,b) for(int i=(a);i<=(b);i++)
#define G(i,a,b) for(int i=(a);i>=(b);i--)
#define ms(a,b) memset(a,b,sizeof(a))
#define si(a) ((int)(a).size())
#define all(a) (a).begin(),(a).end()
#define fi first
#define se second
#define pb push_back

bool S;

const int mod=998244353;

const int N=3e6+3;
char s[N*2];

std::vector<int> sa_naive(const std::vector<int>& s) {
    int n = int(s.size());
    std::vector<int> sa(n);
    std::iota(sa.begin(), sa.end(), 0);
    std::sort(sa.begin(), sa.end(), [&](int l, int r) {
        if (l == r) return false;
        while (l < n && r < n) {
            if (s[l] != s[r]) return s[l] < s[r];
            l++;
            r++;
        }
        return l == n;
    });
    return sa;
}

std::vector<int> sa_doubling(const std::vector<int>& s) {
    int n = int(s.size());
    std::vector<int> sa(n), rnk = s, tmp(n);
    std::iota(sa.begin(), sa.end(), 0);
    for (int k = 1; k < n; k *= 2) {
        auto cmp = [&](int x, int y) {
            if (rnk[x] != rnk[y]) return rnk[x] < rnk[y];
            int rx = x + k < n ? rnk[x + k] : -1;
            int ry = y + k < n ? rnk[y + k] : -1;
            return rx < ry;
        };
        std::sort(sa.begin(), sa.end(), cmp);
        tmp[sa[0]] = 0;
        for (int i = 1; i < n; i++) {
            tmp[sa[i]] = tmp[sa[i - 1]] + (cmp(sa[i - 1], sa[i]) ? 1 : 0);
        }
        std::swap(tmp, rnk);
    }
    return sa;
}

// SA-IS, linear-time suffix array construction
// Reference:
// G. Nong, S. Zhang, and W. H. Chan,
// Two Efficient Algorithms for Linear Time Suffix Array Construction
template <int THRESHOLD_NAIVE = 10, int THRESHOLD_DOUBLING = 40>
std::vector<int> sa_is(const std::vector<int>& s, int upper) {
    int n = int(s.size());
    if (n == 0) return {};
    if (n == 1) return {0};
    if (n == 2) {
        if (s[0] < s[1]) {
            return {0, 1};
        } else {
            return {1, 0};
        }
    }
    if (n < THRESHOLD_NAIVE) {
        return sa_naive(s);
    }
    if (n < THRESHOLD_DOUBLING) {
        return sa_doubling(s);
    }

    std::vector<int> sa(n);
    std::vector<bool> ls(n);
    for (int i = n - 2; i >= 0; i--) {
        ls[i] = (s[i] == s[i + 1]) ? ls[i + 1] : (s[i] < s[i + 1]);
    }
    std::vector<int> sum_l(upper + 1), sum_s(upper + 1);
    for (int i = 0; i < n; i++) {
        if (!ls[i]) {
            sum_s[s[i]]++;
        } else {
            sum_l[s[i] + 1]++;
        }
    }
    for (int i = 0; i <= upper; i++) {
        sum_s[i] += sum_l[i];
        if (i < upper) sum_l[i + 1] += sum_s[i];
    }

    auto induce = [&](const std::vector<int>& lms) {
        std::fill(sa.begin(), sa.end(), -1);
        std::vector<int> buf(upper + 1);
        std::copy(sum_s.begin(), sum_s.end(), buf.begin());
        for (auto d : lms) {
            if (d == n) continue;
            sa[buf[s[d]]++] = d;
        }
        std::copy(sum_l.begin(), sum_l.end(), buf.begin());
        sa[buf[s[n - 1]]++] = n - 1;
        for (int i = 0; i < n; i++) {
            int v = sa[i];
            if (v >= 1 && !ls[v - 1]) {
                sa[buf[s[v - 1]]++] = v - 1;
            }
        }
        std::copy(sum_l.begin(), sum_l.end(), buf.begin());
        for (int i = n - 1; i >= 0; i--) {
            int v = sa[i];
            if (v >= 1 && ls[v - 1]) {
                sa[--buf[s[v - 1] + 1]] = v - 1;
            }
        }
    };

    std::vector<int> lms_map(n + 1, -1);
    int m = 0;
    for (int i = 1; i < n; i++) {
        if (!ls[i - 1] && ls[i]) {
            lms_map[i] = m++;
        }
    }
    std::vector<int> lms;
    lms.reserve(m);
    for (int i = 1; i < n; i++) {
        if (!ls[i - 1] && ls[i]) {
            lms.push_back(i);
        }
    }

    induce(lms);

    if (m) {
        std::vector<int> sorted_lms;
        sorted_lms.reserve(m);
        for (int v : sa) {
            if (lms_map[v] != -1) sorted_lms.push_back(v);
        }
        std::vector<int> rec_s(m);
        int rec_upper = 0;
        rec_s[lms_map[sorted_lms[0]]] = 0;
        for (int i = 1; i < m; i++) {
            int l = sorted_lms[i - 1], r = sorted_lms[i];
            int end_l = (lms_map[l] + 1 < m) ? lms[lms_map[l] + 1] : n;
            int end_r = (lms_map[r] + 1 < m) ? lms[lms_map[r] + 1] : n;
            bool same = true;
            if (end_l - l != end_r - r) {
                same = false;
            } else {
                while (l < end_l) {
                    if (s[l] != s[r]) {
                        break;
                    }
                    l++;
                    r++;
                }
                if (l == n || s[l] != s[r]) same = false;
            }
            if (!same) rec_upper++;
            rec_s[lms_map[sorted_lms[i]]] = rec_upper;
        }

        auto rec_sa =
            sa_is<THRESHOLD_NAIVE, THRESHOLD_DOUBLING>(rec_s, rec_upper);

        for (int i = 0; i < m; i++) {
            sorted_lms[i] = lms[rec_sa[i]];
        }
        induce(sorted_lms);
    }
    return sa;
}

int n,sa[N*2],rk[N*2],ht[N*2],mn[22][N*2];
void isa(int n){
	static int h[N*2];
	vector<int>a=sa_is(vector<int>(s+1,s+n+1),255);
	F(i,1,n) sa[i]=a[i-1]+1;
	F(i,1,n) rk[sa[i]]=i;
	F(i,1,n){
		int j=sa[rk[i]-1],k=max(0,h[i-1]-1);
		while(i+k<=n&&j+k<=n&&s[i+k]==s[j+k]) k++;
		h[i]=ht[rk[i]]=k; 
	}
	F(i,1,n) mn[0][i]=ht[i];
	F(j,1,21) F(i,1,n-(1<<j)+1) mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1<<(j-1))]);
}
inline int qry(int l,int r){
	int k=__lg(r-l+1);
	return min(mn[k][l],mn[k][r-(1<<k)+1]);
}
inline int gl(int x,int y){
	int l=1,r=rk[x]-1,ans=rk[x];
	while(l<=r){
		int mid=(l+r)>>1;
		if(qry(mid+1,rk[x])>=y){
			ans=mid;
			r=mid-1;
		}else l=mid+1;
	}
	return ans;
}
inline int gr(int x,int y){
	int l=rk[x]+1,r=n,ans=rk[x];
	while(l<=r){
		int mid=(l+r)>>1;
		if(qry(rk[x]+1,mid)>=y){
			ans=mid;
			l=mid+1;
		}else r=mid-1;
	}
	return ans;
}

int sum[N*2];

char t[N*4];
int p[N*4];
void init(int n){
	int m=n;
	G(i,n,1) t[i*2]=s[i];
	n=n*2+1;
	F(i,1,n) if(i&1) t[i]='#';
	int mx=0,ans=0;
	F(i,1,n){
		if(mx+p[mx]>i) p[i]=min(p[mx*2-i],mx+p[mx]-i);
		while(i-p[i]>=1&&i+p[i]<=n&&t[i-p[i]]==t[i+p[i]]){
			if(t[i+p[i]]!='#'){
				int l=(i-p[i])/2,r=(i+p[i])/2;
				if(r-l+1<=m/2){
					int p=gl(l,r-l+1),q=gr(l,r-l+1);
					ans+=(__int128)(sum[q]-sum[p-1])*(sum[q]-sum[p-1])*(r-l+1)%mod;
					if(ans>=mod) ans-=mod;
				}
			}
			p[i]++;
		}
		if(i+p[i]>mx+p[mx]) mx=i;
	}
	cout<<ans;
}

bool T;

int main(){
//	cerr<<(&S-&T)/1024./1024.;
	cin.tie(0)->ios::sync_with_stdio(0);
	cin>>n>>(s+1);
//	n=3e6; F(i,1,n) s[i]='a';
	F(i,1,n) s[i+n]=s[i];
	n*=2;
	isa(n);
	F(i,1,n) sum[i]=sum[i-1]+(sa[i]<=n/2);
	init(n);
}

詳細信息

In file included from /usr/include/c++/13/string:43,
                 from /usr/include/c++/13/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:52,
                 from answer.code:5:
/usr/include/c++/13/bits/allocator.h: In destructor ‘std::_Vector_base<int, std::allocator<int> >::_Vector_impl::~_Vector_impl()’:
/usr/include/c++/13/bits/allocator.h:184:7: error: inlining failed in call to ‘always_inline’ ‘std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = int]’: target specific option mismatch
  184 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/13/vector:66,
                 from /usr/include/c++/13/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/13/bits/stdc++.h:53:
/usr/include/c++/13/bits/stl_vector.h:133:14: note: called from here
  133 |       struct _Vector_impl
      |              ^~~~~~~~~~~~