马拉车板子

#include<bits/stdc++.h>
#define int long long
using namespace std;
std::vector<int> manacher(std::string s) {
    std::string t = "#";
    for (auto c : s) {
        t += c;
        t += '#';
    }
    cout<<t<<"\n";
    int n = t.size();
    std::vector<int> r(n);
    for (int i = 0, j = 0; i < n; i++) {
        if (2 * j - i >= 0 && j + r[j] > i) {
            r[i] = std::min(r[2 * j - i], j + r[j] - i);
        }
        while (i - r[i] >= 0 && i + r[i] < n && t[i - r[i]] == t[i + r[i]]) {
            r[i] += 1;
        }
        if (i + r[i] > j + r[j]) {
            j = i;
        }
    }
    return r;
}
void solve(){
	int n,q;
	cin>>n>>q;
	string s;
	cin>>s;
	vector<int> f1(n), f2(n);
    for(int i=n-1;i>=0;i--)
    {
        f1[i]=i+1<n&&s[i]==s[i+1]?f1[i+1]:i;
        f2[i]=i+2<n&&s[i]==s[i+2]?f2[i+2]:i;
    }
	auto rad=manacher(s);
	for(auto c:rad){
		cout<<c<<" ";
	}
	while(q--){
		int l,r;
		cin>>l>>r;
		l--;r--;
		int len=r-l+1;
		int ans=0;
		if(f1[l]<r)
        {
            int m=(len-1)-(len-1)%2;
            ans+=(2+m)*(m/2)/2;
        }
        if(f2[l]+1<r||f2[l+1]+1<r)
        {
            int m=len-1-len%2;
            ans+=(3+m)*((m-1)/2)/2;
        }
         if(rad[l+r+1]<len) //说明并不对称 
        {
            ans+=len;
        }
        cout<<ans<<"\n";
	}
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t=1;
	cin>>t;
	for(int i=1;i<=t;i++)solve();
	return 0;
} 
posted @ 2024-03-22 20:47  yufan1102  阅读(3)  评论(0编辑  收藏  举报