提交记录 4103


用户 题目 状态 得分 用时 内存 语言 代码长度
Iking noi18c. 【NOI2018】你的名字 Accepted 100 1.375 s 345656 KB C++ 3.72 KB
提交时间 评测时间
2018-07-18 20:56:00 2020-07-31 22:27:19
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 500000, maxc = 1000000, maxv = 30000000;
char s[maxn + 10];
int n, q, ch[maxc + 10][26], fa[maxc + 10], rt[maxn + 10];
int dfn[maxc + 10], ed[maxc + 10], dfscnt;
int maxl[maxc + 10], np = 1, ndcnt = 1, lim[maxc + 10];
bool islv[maxc + 10];
vector<int> g[maxc + 10];
__attribute__((optimize("-O3")))
void add(int c) {
	int p = np; np = ++ndcnt; islv[np] = 1;
	maxl[np] = maxl[p] + 1;
	while (p && !ch[p][c]) {
		ch[p][c] = np; p = fa[p];
	}
	if (p) {
		int q = ch[p][c];
		if (maxl[p] + 1 == maxl[q]) fa[np] = q;
		else {
			int nq = ++ndcnt; fa[nq] = fa[q];
			fa[q] = fa[np] = nq; maxl[nq] = maxl[p] + 1;
			for (int i = 0; i < 26; ++i) ch[nq][i] = ch[q][i];
			while (ch[p][c] == q) {
				ch[p][c] = nq; p = fa[p];
			}
		}
	} else fa[np] = 1;
}

int lc[maxv + 10], rc[maxv + 10], sz[maxv + 10], vcnt;
__attribute__((optimize("-O3")))
void build(int &p, int ls, int rs) {
	p = ++vcnt;
	if (ls != rs) {
		int mid = ls + rs >> 1;
		build(lc[p], ls, mid); build(rc[p], mid + 1, rs);
	}
}
__attribute__((optimize("-O3")))
void add(int &p, int cmp, int k, int ls, int rs) {
	p = ++vcnt; lc[p] = lc[cmp]; rc[p] = rc[cmp]; sz[p] = sz[cmp] + 1;
	if (ls != rs) {
		int mid = ls + rs >> 1;
		if (k <= mid) add(lc[p], lc[cmp], k, ls, mid);
		else add(rc[p], rc[cmp], k, mid + 1, rs);
	}
}
__attribute__((optimize("-O3")))
int query(int p, int l, int r, int ls, int rs) {
	if (l > r) return 0;
	if (l == ls && r == rs) return sz[p];
	else {
		int mid = ls + rs >> 1;
		if (r <= mid) return query(lc[p], l, r, ls, mid);
		else if (l > mid) return query(rc[p], l, r, mid + 1, rs);
		else return query(lc[p], l, mid, ls, mid) + query(rc[p], mid + 1, r, mid + 1, rs);
	}
}

__attribute__((optimize("-O3")))
void dfs(int p) {
	dfn[p] = dfscnt;
	if (islv[p]) {
		++dfscnt; add(rt[dfscnt], rt[dfscnt - 1], maxl[p], 1, n);
	}
	for (int i = 0; i < g[p].size(); ++i) dfs(g[p][i]);
	ed[p] = dfscnt;
}


namespace anothersam {

	int n, q, ch[maxc * 2 + 10][26], fa[maxc * 2 + 10], maxl[maxc * 2 + 10], np, ndcnt;
	int pos[maxc * 2 + 10];
	__attribute__((optimize("-O3")))
	void init() {
		for (int i = 1; i <= ndcnt; ++i) {
			for (int j = 0; j < 26; ++j) ch[i][j] = 0;
			fa[i] = maxl[i] = pos[i] = 0;
		}
		np = ndcnt = 1;
	}
	__attribute__((optimize("-O3")))
	void add(int c) {
		int p = np; np = ++ndcnt; 
		maxl[np] = pos[np] = maxl[p] + 1;
		while (p && !ch[p][c]) {
			ch[p][c] = np; p = fa[p];
		}
		if (p) {
			int q = ch[p][c];
			if (maxl[p] + 1 == maxl[q]) fa[np] = q;
			else {
				int nq = ++ndcnt; fa[nq] = fa[q]; pos[nq] = pos[q];
				fa[q] = fa[np] = nq; maxl[nq] = maxl[p] + 1;
				for (int i = 0; i < 26; ++i) ch[nq][i] = ch[q][i];
				while (ch[p][c] == q) {
					ch[p][c] = nq; p = fa[p];
				}
			}
		} else fa[np] = 1;
	}
	__attribute__((optimize("-O3")))
	ll calc() {
		ll ans = 0;
		for (int i = 2; i <= ndcnt; ++i)
			ans += max(0, maxl[i] - max(maxl[fa[i]], lim[pos[i]]));
		return ans;
	}
}
__attribute__((optimize("-O3")))
int main() {
	scanf("%s", s + 1); n = strlen(s + 1);
	for (int i = 1; i <= n; ++i) add(s[i] - 'a');
	for (int i = 2; i <= ndcnt; ++i) g[fa[i]].push_back(i);
	build(rt[0], 1, n);
	dfs(1);
	scanf("%d", &q);
	while (q--) {
		int l, r, p = 1, nowl = 0;
		scanf("%s%d%d", s + 1, &l, &r); int len = strlen(s + 1);
		anothersam::init();
		for (int i = 1; i <= len; ++i) {
			int c = s[i] - 'a';
			anothersam::add(c);
			while (1) {
				if (ch[p][c]) {
					int e = ch[p][c];
					if (query(rt[ed[e]], l + nowl, r, 1, n) - query(rt[dfn[e]], l + nowl, r, 1, n)) {
						++nowl; p = e; break;
					}
				}
				if (nowl == 0) break;
				--nowl; if (nowl == maxl[fa[p]]) p = fa[p];
			}
			lim[i] = nowl;
		}
		printf("%lld\n", anothersam::calc());
	}
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #16.956 ms23 MB + 72 KBAcceptedScore: 4

Testcase #28.501 ms23 MB + 356 KBAcceptedScore: 4

Testcase #38.688 ms23 MB + 360 KBAcceptedScore: 4

Testcase #463.054 ms23 MB + 800 KBAcceptedScore: 4

Testcase #560.416 ms23 MB + 796 KBAcceptedScore: 4

Testcase #6763.81 ms337 MB + 568 KBAcceptedScore: 4

Testcase #7763.987 ms337 MB + 452 KBAcceptedScore: 4

Testcase #8116.747 ms68 MB + 428 KBAcceptedScore: 4

Testcase #9176.173 ms64 MB + 144 KBAcceptedScore: 4

Testcase #10262.629 ms112 MB + 892 KBAcceptedScore: 4

Testcase #11436.693 ms107 MB + 336 KBAcceptedScore: 4

Testcase #12446.123 ms157 MB + 944 KBAcceptedScore: 4

Testcase #13724.249 ms152 MB + 176 KBAcceptedScore: 4

Testcase #14620.851 ms204 MB + 644 KBAcceptedScore: 4

Testcase #151.036 s198 MB + 500 KBAcceptedScore: 4

Testcase #16807.209 ms251 MB + 492 KBAcceptedScore: 4

Testcase #171.355 s245 MB + 132 KBAcceptedScore: 4

Testcase #18901.183 ms121 MB + 104 KBAcceptedScore: 4

Testcase #191.056 s163 MB + 572 KBAcceptedScore: 4

Testcase #201.222 s207 MB + 208 KBAcceptedScore: 4

Testcase #211.285 s251 MB + 596 KBAcceptedScore: 4

Testcase #221.375 s251 MB + 532 KBAcceptedScore: 4

Testcase #231.373 s251 MB + 460 KBAcceptedScore: 4

Testcase #241.335 s251 MB + 568 KBAcceptedScore: 4

Testcase #251.303 s251 MB + 604 KBAcceptedScore: 4


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:11:14 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠