Submission #22818

# Submission time Handle Problem Language Result Execution time Memory
22818 2017-04-30T07:40:48 Z AcornCkiGs14004Team(#953, gs14004, cki86201, dotorya) New Ocurrences (KRIII5_NO) C++11
7 / 7
446 ms 48980 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
typedef long long lint;
const int MAXN = 100005;

int n;

struct bit{
	lint tree[MAXN];
	lint sum;
	void add(int x, lint v){
		x++;
		sum += v;
		while(x <= n+1){
			tree[x] += v;
			x += x & -x;
		}
	}
	lint query(int x){
		if(x == n) return sum;
		x++;
		lint ret = 0;
		while(x){
			ret += tree[x];
			x -= x & -x;
		}
		return ret;
	}
}bl, br;

struct sfxarray{
	int ord[MAXN], nord[MAXN], cnt[MAXN], aux[MAXN];
	void solve(int n, char *str, int *sfx, int *rev, int *lcp){
		int p = 1;
		memset(ord, 0, sizeof(ord));
		for(int i=0; i<n; i++){
			sfx[i] = i;
			ord[i] = str[i];
		}
		int pnt = 1;
		while(1){
			memset(cnt, 0, sizeof(cnt));
			for(int i=0; i<n; i++){
				cnt[ord[min(i+p, n)]]++;
			}
			for(int i=1; i<=n || i<=255; i++){
				cnt[i] += cnt[i-1];
			}
			for(int i=n-1; i>=0; i--){
				aux[--cnt[ord[min(i+p, n)]]] = i;
			}
			memset(cnt, 0, sizeof(cnt));
			for(int i=0; i<n; i++){
				cnt[ord[i]]++;
			}
			for(int i=1; i<=n || i<=255; i++){
				cnt[i] += cnt[i-1];
			}
			for(int i=n-1; i>=0; i--){
				sfx[--cnt[ord[aux[i]]]] = aux[i];
			}
			if(pnt == n) break;
			pnt = 1;
			nord[sfx[0]] = 1;
			for(int i=1; i<n; i++){
				if(ord[sfx[i-1]] != ord[sfx[i]] || ord[sfx[i-1] + p] != ord[sfx[i] + p]){
					pnt++;
				}
				nord[sfx[i]] = pnt;
			}
			memcpy(ord, nord, sizeof(int) * n);
			p *= 2;
		}
		for(int i=0; i<n; i++){
			rev[sfx[i]] = i;
		}
		int h = 0;
		for(int i=0; i<n; i++){
			if(rev[i]){
				int prv = sfx[rev[i] - 1];
				while(str[prv + h] == str[i + h]) h++;
				lcp[rev[i]] = h;
			}
			h = max(h-1, 0);
		}
	}
}sfxarray;

char str[100005];
int sfx[100005], rev[100005], lcp[100005];
lint ans[100005];

vector<pi> q[100005];

void solve(int s, int e){
	if(s == e) return;
	int m = (s+e)/2;
	solve(s, m);
	solve(m+1, e);
	int l = lcp[m+1];
	int pl = m, pr = m+1;
	ans[min(sfx[m], sfx[m+1])] += l;
	bl.add(sfx[m], 1);
	br.add(sfx[m+1], 1);
	while((pl > s || pr < e) && l > 0){
		if((pr < e && min(l, lcp[pl]) < min(l, lcp[pr+1])) || pl == s){
			l = min(l, lcp[pr+1]);
			int cnt = bl.query(n) - bl.query(sfx[pr+1]);
			if(pl < m+1){
			q[pl].push_back(pi(sfx[pr+1], l));
			q[m+1].push_back(pi(sfx[pr+1], -l));
			}
			ans[sfx[pr+1]] += 1ll * cnt * l;
			br.add(sfx[pr+1], 1);
			pr++;
		}
		else{
			l = min(l, lcp[pl]);
			int cnt = br.query(n) - br.query(sfx[pl-1]);
			if(m < pr){
			q[m+1].push_back(pi(sfx[pl-1], l));
			q[pr+1].push_back(pi(sfx[pl-1], -l));
			}
			ans[sfx[pl-1]] += 1ll * cnt * l;
			bl.add(sfx[pl-1], 1);
			pl--;
		}
	}
	for(int i=pl; i<=m; i++) bl.add(sfx[i], -1);
	for(int i=m+1; i<=pr; i++) br.add(sfx[i], -1);
}

int main(){
	scanf("%s", str);
	n = strlen(str);
	reverse(str, str + n);
	sfxarray.solve(n, str, sfx, rev, lcp);
	solve(0, n-1);
	for(int i=0; i<n; i++){
		for(auto &j : q[i]){
			bl.add(j.first, j.second);
		}
		ans[sfx[i]] += bl.query(n) - bl.query(sfx[i]);
	}
	for(int i=n-1; i>=0; i--){
		ans[i] <<= 1;
		ans[i] += ans[i+1] + (n - i);
		printf("%lld\n", ans[i]);
	}
}

Compilation message

NO.cpp: In function 'int main()':
NO.cpp:135:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", str);
                  ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 9544 KB Output is correct
2 Correct 0 ms 9544 KB Output is correct
3 Correct 3 ms 9544 KB Output is correct
4 Correct 3 ms 10336 KB Output is correct
5 Correct 9 ms 10768 KB Output is correct
6 Correct 3 ms 10632 KB Output is correct
7 Correct 9 ms 10764 KB Output is correct
8 Correct 0 ms 9544 KB Output is correct
9 Correct 3 ms 9808 KB Output is correct
10 Correct 9 ms 10600 KB Output is correct
11 Correct 6 ms 10608 KB Output is correct
12 Correct 9 ms 10796 KB Output is correct
13 Correct 3 ms 10752 KB Output is correct
14 Correct 6 ms 10876 KB Output is correct
15 Correct 9 ms 10808 KB Output is correct
16 Correct 9 ms 10772 KB Output is correct
17 Correct 13 ms 10752 KB Output is correct
18 Correct 6 ms 10884 KB Output is correct
19 Correct 13 ms 10748 KB Output is correct
20 Correct 9 ms 10612 KB Output is correct
21 Correct 13 ms 10752 KB Output is correct
22 Correct 9 ms 10740 KB Output is correct
23 Correct 9 ms 10604 KB Output is correct
24 Correct 6 ms 10612 KB Output is correct
25 Correct 13 ms 10616 KB Output is correct
26 Correct 3 ms 10616 KB Output is correct
27 Correct 9 ms 10616 KB Output is correct
28 Correct 6 ms 10604 KB Output is correct
29 Correct 9 ms 10612 KB Output is correct
30 Correct 13 ms 10604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 11260 KB Output is correct
2 Correct 309 ms 34828 KB Output is correct
3 Correct 146 ms 23740 KB Output is correct
4 Correct 379 ms 41548 KB Output is correct
5 Correct 386 ms 45276 KB Output is correct
6 Correct 419 ms 42068 KB Output is correct
7 Correct 446 ms 43152 KB Output is correct
8 Correct 249 ms 48712 KB Output is correct
9 Correct 246 ms 48752 KB Output is correct
10 Correct 289 ms 48980 KB Output is correct
11 Correct 296 ms 48580 KB Output is correct
12 Correct 333 ms 48152 KB Output is correct
13 Correct 373 ms 48692 KB Output is correct
14 Correct 393 ms 47716 KB Output is correct
15 Correct 389 ms 45664 KB Output is correct
16 Correct 389 ms 44724 KB Output is correct
17 Correct 369 ms 43464 KB Output is correct
18 Correct 359 ms 46120 KB Output is correct
19 Correct 329 ms 46848 KB Output is correct
20 Correct 296 ms 44956 KB Output is correct
21 Correct 313 ms 34768 KB Output is correct
22 Correct 409 ms 40424 KB Output is correct
23 Correct 426 ms 40732 KB Output is correct
24 Correct 403 ms 42092 KB Output is correct
25 Correct 413 ms 40944 KB Output is correct
26 Correct 386 ms 42388 KB Output is correct
27 Correct 409 ms 42792 KB Output is correct
28 Correct 389 ms 41528 KB Output is correct
29 Correct 379 ms 40084 KB Output is correct
30 Correct 383 ms 41668 KB Output is correct
31 Correct 423 ms 41448 KB Output is correct
32 Correct 403 ms 42220 KB Output is correct
33 Correct 383 ms 41912 KB Output is correct
34 Correct 369 ms 42192 KB Output is correct
35 Correct 409 ms 42672 KB Output is correct
36 Correct 396 ms 41016 KB Output is correct
37 Correct 389 ms 40520 KB Output is correct
38 Correct 386 ms 40244 KB Output is correct
39 Correct 399 ms 42352 KB Output is correct
40 Correct 423 ms 40776 KB Output is correct
41 Correct 0 ms 9544 KB Output is correct
42 Correct 0 ms 9544 KB Output is correct
43 Correct 3 ms 9544 KB Output is correct
44 Correct 3 ms 10336 KB Output is correct
45 Correct 9 ms 10768 KB Output is correct
46 Correct 3 ms 10632 KB Output is correct
47 Correct 9 ms 10764 KB Output is correct
48 Correct 0 ms 9544 KB Output is correct
49 Correct 3 ms 9808 KB Output is correct
50 Correct 9 ms 10600 KB Output is correct
51 Correct 6 ms 10608 KB Output is correct
52 Correct 9 ms 10796 KB Output is correct
53 Correct 3 ms 10752 KB Output is correct
54 Correct 6 ms 10876 KB Output is correct
55 Correct 9 ms 10808 KB Output is correct
56 Correct 9 ms 10772 KB Output is correct
57 Correct 13 ms 10752 KB Output is correct
58 Correct 6 ms 10884 KB Output is correct
59 Correct 13 ms 10748 KB Output is correct
60 Correct 9 ms 10612 KB Output is correct
61 Correct 13 ms 10752 KB Output is correct
62 Correct 9 ms 10740 KB Output is correct
63 Correct 9 ms 10604 KB Output is correct
64 Correct 6 ms 10612 KB Output is correct
65 Correct 13 ms 10616 KB Output is correct
66 Correct 3 ms 10616 KB Output is correct
67 Correct 9 ms 10616 KB Output is correct
68 Correct 6 ms 10604 KB Output is correct
69 Correct 9 ms 10612 KB Output is correct
70 Correct 13 ms 10604 KB Output is correct