結果

問題 No.465 PPPPPPPPPPPPPPPPAPPPPPPPP
ユーザー nola_suznola_suz
提出日時 2016-12-16 18:21:35
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 7,662 bytes
コンパイル時間 1,170 ms
コンパイル使用メモリ 118,608 KB
実行使用メモリ 67,348 KB
最終ジャッジ日時 2023-08-20 09:06:34
合計ジャッジ時間 32,319 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 290 ms
11,668 KB
testcase_06 AC 1,681 ms
46,664 KB
testcase_07 AC 320 ms
12,068 KB
testcase_08 AC 1,698 ms
45,108 KB
testcase_09 TLE -
testcase_10 TLE -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 RE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 RE -
testcase_19 TLE -
testcase_20 AC 1,993 ms
47,000 KB
testcase_21 RE -
32_ratsliveonnoevilstar.txt RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

template<class T>
struct RollingHash {
	static const int NN = 2; // the number of hash table
	T base[NN] = {1009, 1007};
	T PMOD[NN] = {1000000007, 1000000009};
	vector<T> hash[NN];
	vector<T> mypow[NN];
	int sz;
	void init(string s) {
		sz = s.size();
		for(int i = 0; i < NN; i++) {
			hash[i].assign(sz + 1, 0);
			mypow[i].assign(sz + 1, 0);
			mypow[i][0] = 1;
		}
		for(int i = 0; i < NN; i++) {
			for(int j = 0; j < sz; j++) {
				hash[i][j + 1] = (hash[i][j] * base[i] + s[j]) % PMOD[i];
				mypow[i][j + 1] = mypow[i][j] * base[i] % PMOD[i];
			}
		}
	}
	RollingHash() {
	}
	// hash[t] value of s[l..r]
	T get(int l, int r, int t) {
		T ret = (hash[t][r + 1] - hash[t][l] * mypow[t][r - l + 1] % PMOD[t] + PMOD[t]) % PMOD[t];
		return ret;
	}
	// compare s[a..b] and s[c..d] O(1)
	bool comp(int a, int b, int c, int d) {
		if(a < 0 || sz <= b || c < 0 || sz <= d) return false;
		for(int i = 0; i < NN; i++) {
			if(get(a, b, i) != get(c, d, i)) return false;
		}
		return true;
	}
	// longest common extension s[l..LCE(l,r)] == s[r..LCE(l,r)] && s[l..LCE(l,r)+1] != s[r..LCE(l,r)+1]
	int LCE(int l,int r){
		tie(l,r)=minmax(l,r);
		if(l==r) return sz-r+1;
		int ll = 0;
		int rr = sz;
		while(rr-ll>1){
			int mid = (ll+rr)>>1;
			if(comp(l,l+mid-1,r,r+mid-1)) ll=mid;
			else rr=mid;
		}
		return ll;
	}
};

RollingHash<ll> rh;
string s;
int n;

int LCE(int l, int r){
	return rh.LCE(l, r);
}


static const int MAX_SIZE = 1 << 14; //segment tree のサイズ。 2^17 ≒ 1.3 * 10^5

ll all[2 * MAX_SIZE - 1], part[2 * MAX_SIZE - 1]; // segment tree

//区間[a, b)に値xを加算する.
void add(int a, int b, ll x, int k = 0, int l = 0, int r = MAX_SIZE)
{
    if (a <= l && r <= b){ //[l, r)が[a, b)に完全に内包されていれば
        all[k] += x; //[l, r)の全ての区間が持つ値としてxを足す.
    }
    else if (l < b && a < r){ //[l, r)と[a, b)が交差していれば
        part[k] += (min(b, r) - max(a, l)) * x;  //交差している分の値を, 部分的な和を持つノードに加算する.
        add(a, b, x, k * 2 + 1, l, (l + r) / 2); //子でも同じ処理を行う.
        add(a, b, x, k * 2 + 2, (l + r) / 2, r); //〃.
    }
}

ll sum(int a, int b, int k = 0, int l = 0, int r = MAX_SIZE)
{
    if (b <= l || r <= a){ //[a, b)と[l, r)が全く交差しない場合
        return (0);
    }
    else if (a <= l && r <= b){ //完全に内包されていれば
        return (all[k] * (r - l) + part[k]);
    }
    else { //[l, r)と[a, b)が交差していれば
        ll res;
        res = (min(b, r) - max(a, l)) * all[k]; //そのノードの全ての要素が持つ値のうち, [a, b)に属すものの分だけを加算する.
        res += sum(a, b, k * 2 + 1, l, (l + r) / 2); //子ノードで和を求める.
        res += sum(a, b, k * 2 + 2, (l + r) / 2, r); //〃
        return (res);
    }
}

bool isPal(string a){
	string b = a;
	reverse(ALL(b));
	return a == b;
}

int func(string a){
	int ret = 0;
	rep(i,a.size()-1){
		if(isPal(a.substr(0,i+1))&&isPal(a.substr(i+1))) {
			ret++;
		}
	}
	return ret;
}

// int ff(string a){
// 	int n = a.size();
// 	rep(i,n){
// 		rep(j,i){
// 			for(int k=i+2)
// 		}
// 	}
// }

set<pair<pii,int>> se;

void mainmain(){
	cin>>s;
	n = s.size();
	string r = s;
	reverse(ALL(r));
	rh.init(s+'$'+r);
	vll a(n);
	rep(i,n-1){
		int le = i+1;
		if(le&1){
			int tl = le/2;
			int c = i-tl;
			if(tl == LCE(c+1, 2*n-(c-1))) a[i+1]=1;
		}
		else{
			int tl = le/2;
			int c = i-tl;
			if(tl == LCE(c+1, 2*n-c)) a[i+1]=1;
		}
	}
	// rep(i,n){
	// 	cout<<a[i];
	// }cout<<endl;
	rep(i,n){
		int l=i;
		while(i<n&&a[l]==a[i]) i++;
		i--;
		// cout<<l<<" "<<i<<" "<<a[l]<<endl;
		se.insert({{l,i}, a[l]});
	}
	se.insert({{-INF,-1}, -INF});
	se.insert({{n,INF}, INF});
	assert(se.size()<1000);
	rep(i,n){
		// cout<<i<<endl;
		if(i+1<n){
			// even pal
			int le = LCE(i+1, 2*n-i);
			if(le){
				int l = i-le+1;
				int r = i;
				// if(i==5) cout<<l<<" "<<r<<endl;
				auto it = se.lower_bound({{r, INF}, 1});
				it--;
				while(1){
					// cout<<"hoge"<<endl;
					int ll,rr,t;
					ll=it->F.F,rr=it->F.S,t=it->S;
					// if(i==5) cout<<ll<<" "<<rr<<" "<<t<<endl;
					if(rr<l) break;
					if(t){
						int tl = max(l, ll);
						int tr = min(r, rr);
						int tle = i-tr;
						// if(i==5){
						// 	cout<<i+tle+1<<" "<<i+tle+1+(tr-tl) << endl;
						// }
						add(i+tle+1, i+tle+2+(tr-tl), 1);
					}
					// cout<<"hoge"<<endl;
					if(it == se.begin()) break;
					it--;
				}
			}
		}
		// odd pal
		int le = (i==0?0:LCE(i+1, 2*n-(i-1)));
		int l = i-le;
		int r = i;
		auto it = se.lower_bound({{r,INF},1});
		it--;
		while(1){
			int ll,rr,t;
			ll=it->F.F,rr=it->F.S,t=it->S;
			// if(i==1) cout<<ll<<" "<<rr<<" "<<t<<endl;
			if(rr<l) break;
			if(t){
				int tl = max(l, ll);
				int tr = min(r, rr);
				int tle = i-tr;
				add(i+tle, i+tle+1+(tr-tl), 1);
			}
			if(it == se.begin()) break;
			it--;
		}
	}
	vll b(n);
	rep(i,n-1){
		b[i] = sum(i,i+1);
	}
	vll c(n);
	rep(i,n-1){
		int le = i+1;
		if(le&1){
			int tl = le/2;
			int ce = n-1-tl;
			if(tl == LCE(ce+1, 2*n-(ce-1))) c[n-1-i]=1;
		}
		else{
			int tl = le/2;
			int ce = n-1-tl;
			if(tl == LCE(ce+1, 2*n-ce)) c[n-1-i]=1;
		}
	}
	// rep(i,n){
	// 	cout<<c[i];
	// }cout<<endl;
	// rep(i,n){
	// 	if(b[i]){
	// 		int t = func(s.substr(0,i+1));
	// 		if(t != b[i]){
	// 			cout<<"error "<<i<<" "<<t<<endl;
	// 		}
	// 	}
	// }
	for(int i=n-2;i>=0;i--){
		c[i] += c[i+1];
	}
	ll ans = 0;
	rep(i,n-2){
		ans += b[i] * c[i+2];
	}
	cout << ans << endl;
	// cout<<ff(s)<<endl;
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    mainmain();
}
0