提交记录 3792


用户 题目 状态 得分 用时 内存 语言 代码长度
nqiiii noi18c. 【NOI2018】你的名字 Accepted 100 1.522 s 345648 KB C++ 3.44 KB
提交时间 评测时间
2018-07-18 17:45:52 2020-07-31 21:40:37
#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];

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;

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);
	}
}

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);
	}
}

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);
	}
}


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];

	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;
	}

	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;
	}

	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;
	}
}

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 #18.315 ms23 MB + 72 KBAcceptedScore: 4

Testcase #29.885 ms23 MB + 356 KBAcceptedScore: 4

Testcase #310.103 ms23 MB + 360 KBAcceptedScore: 4

Testcase #481.543 ms23 MB + 800 KBAcceptedScore: 4

Testcase #578.35 ms23 MB + 796 KBAcceptedScore: 4

Testcase #6802.675 ms337 MB + 560 KBAcceptedScore: 4

Testcase #7805.378 ms337 MB + 444 KBAcceptedScore: 4

Testcase #8126.656 ms68 MB + 420 KBAcceptedScore: 4

Testcase #9187.248 ms64 MB + 144 KBAcceptedScore: 4

Testcase #10284.885 ms112 MB + 884 KBAcceptedScore: 4

Testcase #11460.999 ms107 MB + 336 KBAcceptedScore: 4

Testcase #12482.653 ms157 MB + 936 KBAcceptedScore: 4

Testcase #13769.199 ms152 MB + 176 KBAcceptedScore: 4

Testcase #14672.316 ms204 MB + 636 KBAcceptedScore: 4

Testcase #151.107 s198 MB + 500 KBAcceptedScore: 4

Testcase #16871.473 ms251 MB + 484 KBAcceptedScore: 4

Testcase #171.446 s245 MB + 132 KBAcceptedScore: 4

Testcase #181.02 s121 MB + 96 KBAcceptedScore: 4

Testcase #191.179 s163 MB + 564 KBAcceptedScore: 4

Testcase #201.359 s207 MB + 200 KBAcceptedScore: 4

Testcase #211.418 s251 MB + 588 KBAcceptedScore: 4

Testcase #221.522 s251 MB + 524 KBAcceptedScore: 4

Testcase #231.521 s251 MB + 452 KBAcceptedScore: 4

Testcase #241.473 s251 MB + 560 KBAcceptedScore: 4

Testcase #251.434 s251 MB + 596 KBAcceptedScore: 4


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