编译超时,萌新求助
查看原帖
编译超时,萌新求助
123807
Rainybunny楼主2020/6/8 20:45

RT,CF 显示 Compilation process timed out.,提交三次都一样。最后一次状态
代码:

#include <cstdio>
#include <cstring>
#include <utility>
#include <algorithm>

#define pii std :: pair<int, int>

inline int min_ ( const int a, const int b ) { return a < b ? a : b; }

inline int max_ ( const int a, const int b ) { return b < a ? a : b; }

const int MAXN = 5e5;
int n, m, za[MAXN + 5], zb[MAXN + 5], ztmp[MAXN * 3 + 5];
char a[MAXN + 5], b[MAXN + 5], s[MAXN * 2 + 5], tmp[MAXN * 3 + 5];

class BinaryIndexTree {
private:
	pii val[MAXN * 3 + 5];
	inline int lowbit ( const int x ) { return x & -x; }

public:
	inline void update ( const int indx, const int k ) {
		for ( int i = max_ ( 1, indx ); i <= n; i += lowbit ( i ) ) {
			val[i].first += k;
			val[i].second += k >= 0 ? 1 : -1;
		}
	}
	inline pii sum ( const int indx ) {
		pii ret ( 0, 0 );
		for ( int i = indx; i; i -= lowbit ( i ) ) {
			ret.first += val[i].first;
			ret.second += val[i].second;
		}
		return ret;
	}
} bit;

inline void calcZ ( const char* str, int* z ) {
	int len = strlen ( str );
	for ( int i = 0; i <= len; ++ i ) z[i] = 0;
	for ( int i = 1, l = 0, r = 0; str[i]; ++ i ) {
		if ( i <= r ) z[i] = std :: min ( r - i + 1, z[i - l] );
		for ( ; i + z[i] < len && str[z[i]] == str[i + z[i]]; ++ z[i] );
		if ( i + z[i] - 1 > r ) r = i + z[l = i] - 1;
	}
}

int main () {
	scanf ( "%d %d %s %s %s", &n, &m, a + 1, b + 1, s + 1 );
	for ( int i = 1; i < m; ++ i ) tmp[i - 1] = s[i];
	tmp[m - 1] = '$';
	for ( int i = 1; i <= n; ++ i ) tmp[m + i - 1] = a[i];
	calcZ ( tmp, ztmp );
	for ( int i = 1; i <= n; ++ i ) za[i] = ztmp[m + i - 1];
	for ( int i = 1; i < m; ++ i ) tmp[i - 1] = s[m - i + 1];
	tmp[m - 1] = '$';
	for ( int i = 1; i <= n; ++ i ) tmp[m + i - 1] = b[n - i + 1];
	calcZ ( tmp, ztmp );
	for ( int i = 1; i <= n; ++ i ) zb[n - i + 1] = ztmp[m + i - 1];
	long long ans = 0;
	for ( int l1 = 1, r2 = 1; l1 <= n; ++ l1 ) {
		for ( ; r2 <= min_ ( l1 + m - 2, n ); ++ r2 ) {
			bit.update ( m - 1 - zb[r2], zb[r2] );
		}
		pii tmp ( bit.sum ( za[l1] ) );
		ans += tmp.second * ( za[l1] - m + 1ll ) + tmp.first;
		bit.update ( m - 1 - zb[l1], -zb[l1] );
	}
	printf ( "%lld\n", ans );
	return 0;
}
2020/6/8 20:45
加载中...