답안 #635711

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
635711 2022-08-26T17:33:57 Z ghostwriter Palindromi (COCI22_palindromi) C++14
30 / 110
1000 ms 52692 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#endif
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
    Tran The Bao
    CTL - Da Lat
    Cay ngay cay dem nhung deo duoc cong nhan
*/
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
const int oo = 1e9;
const int T = 4e5 + 5;
struct MinSegtree {
	int tr[T];
	void upd(int i, int l, int r, int q, int v) {
		if (r < q || l > q) return;
		if (l == r) {
			tr[i] = v;
			return;
		}
		int mid = l + (r - l) / 2;
		upd(i * 2, l, mid, q, v);
		upd(i * 2 + 1, mid + 1, r, q, v);
		tr[i] = min(tr[i * 2], tr[i * 2 + 1]);
	}
	int get(int i, int l, int r, int ql, int qr) {
		if (r < ql || l > qr) return oo;
		if (ql <= l && r <= qr) return tr[i];
		int mid = l + (r - l) / 2;
		return min(get(i * 2, l, mid, ql, qr), get(i * 2 + 1, mid + 1, r, ql, qr));
	}
};
struct Mergetree {
	vi tr[T];
	void add(int i, int l, int r, int q, int v) {
		if (r < q || l > q) return;
		tr[i].pb(v);
		if (l == r) return;
		int mid = l + (r - l) / 2;
		add(i * 2, l, mid, q, v);
		add(i * 2 + 1, mid + 1, r, q, v);
	}
	void dfs(int i, int l, int r) {
		sort(all(tr[i]));
		if (l == r) return;
		int mid = l + (r - l) / 2;
		dfs(i * 2, l, mid);
		dfs(i * 2 + 1, mid + 1, r);
	}
	int get(int i, int l, int r, int ql, int qr, int v) {
		if (r < ql || l > qr) return 0;
		if (ql <= l && r <= qr) {
			if (tr[i].empty() || tr[i].back() < v) return 0;
			int tmp = lb(all(tr[i]), v) - tr[i].begin();
			return sz(tr[i]) - tmp;
		}
		int mid = l + (r - l) / 2;
		return get(i * 2, l, mid, ql, qr, v) + get(i * 2 + 1, mid + 1, r, ql, qr, v);
	}
};
const pi M = {1e9 + 7, 1e9 + 9};
const pi base = {37, 127};
const int N = 1e5 + 5;
int n;
pi P[N];
str s;
pi q[N];
namespace subtask12 {
	int p[N];
	str a1[N];
	set<pi> a2[N];
	int getp(int i) { return i == p[i]? i : p[i] = getp(p[i]); }
	int join(int x, int y) {
		x = getp(x);
		y = getp(y);
		if (sz(a2[x]) < sz(a2[y])) swap(a2[x], a2[y]);
		EACH(i, a2[y]) a2[x].insert(i);
		int m1 = sz(a1[x]);
		int m2 = sz(a1[y]);
		vpi h(m1 + m2 + 2), h1(m1 + m2 + 2);
		a1[x] += a1[y];
		auto get = [&](int l, int r) -> pi {
			pi rs;
			rs.st = (h[r].st - 1LL * (l == 0? 0 : h[l - 1].st) * P[r - l + 1].st % M.st + M.st) % M.st;
			rs.nd = (h[r].nd - 1LL * (l == 0? 0 : h[l - 1].nd) * P[r - l + 1].nd % M.nd + M.nd) % M.nd;
			return rs;
		};
		auto get1 = [&](int l, int r) -> pi {
			pi rs;
			rs.st = (h1[l].st - 1LL * h1[r + 1].st * P[r - l + 1].st % M.st + M.st) % M.st;
			rs.nd = (h1[l].nd - 1LL * h1[r + 1].nd * P[r - l + 1].nd % M.nd + M.nd) % M.nd;
			return rs;
		};
		pi cur = {0, 0};
		FOR(i, 0, m1 + m2 - 1) {
			cur.st = (1LL * cur.st * base.st % M.st + a1[x][i] - '0' + 1) % M.st;
			cur.nd = (1LL * cur.nd * base.nd % M.nd + a1[x][i] - '0' + 1) % M.nd;
			h[i] = cur;
		}
		cur = {0, 0};
		FOS(i, m1 + m2 - 1, 0) {
			cur.st = (1LL * cur.st * base.st % M.st + a1[x][i] - '0' + 1) % M.st;
			cur.nd = (1LL * cur.nd * base.nd % M.nd + a1[x][i] - '0' + 1) % M.nd;
			h1[i] = cur;
		}
		FOR(i, 0, m1 - 1)
		FOR(j, m1, m1 + m2 - 1) {
			pi tmp = get(i, j);
			if (tmp == get1(i, j)) a2[x].insert(tmp);
		}
		p[y] = x;
		return sz(a2[x]);
	}
	void solve() {
		FOR(i, 1, n) {
			p[i] = i;
			a1[i].pb(s[i]);
			a2[i].insert({s[i] - '0' + 1, s[i] - '0' + 1});
		}
		FOR(i, 1, n - 1) {
			int a = q[i].st, b = q[i].nd;
			cout << join(a, b) << '\n';
		}
	}
}
namespace subtask3 {
	pi h[N], h1[N];
	int sa[N], d[N], d1[N];
	pi get(int l, int r) {
		pi rs;
		rs.st = (h[r].st - 1LL * h[l - 1].st * P[r - l + 1].st % M.st + M.st) % M.st;
		rs.nd = (h[r].nd - 1LL * h[l - 1].nd * P[r - l + 1].nd % M.nd + M.nd) % M.nd;
		return rs;
	}
	pi get1(int l, int r) {
		pi rs;
		rs.st = (h1[l].st - 1LL * h1[r + 1].st * P[r - l + 1].st % M.st + M.st) % M.st;
		rs.nd = (h1[l].nd - 1LL * h1[r + 1].nd * P[r - l + 1].nd % M.nd + M.nd) % M.nd;
		return rs;
	}
	int lcp(int a, int b) {
		int l = 1, r = min(n - a + 1, n - b + 1), ans = 0;
		WHILE(l <= r) {
			int mid = l + (r - l) / 2;
			if (get(a, a + mid - 1) == get(b, b + mid - 1)) {
				ans = mid;
				l = mid + 1;
			}
			else r = mid - 1;
		}
		return ans;
	}
	bool cmp(int a, int b) {
		int ans = lcp(a, b);
		if (ans == n - a + 1) return 1;
		if (ans == n - b + 1) return 0;
		return s[a + ans] < s[b + ans];
	}
	int LB(int a, int b) {
		int l = 1, r = n, ans = -1;
		WHILE(l <= r) {
			int mid = l + (r - l) / 2;
			int tmp = lcp(sa[mid], l);
			if (tmp < b - a + 1) {
				if (tmp == n - sa[mid] + 1) l = mid + 1;
				else if (s[sa[mid] + tmp] < s[a + mid]) l = mid + 1;
				else r = mid - 1;
			}
			else {
				ans = mid;
				r = mid - 1;
			}
		}
		return ans;
	}
	int UB(int a, int b) {
		int l = 1, r = n, ans = -1;
		WHILE(l <= r) {
			int mid = l + (r - l) / 2;
			int tmp = lcp(sa[mid], l);
			if (tmp < b - a + 1) {
				if (tmp == n - sa[mid] + 1) l = mid + 1;
				else if (s[sa[mid] + tmp] < s[a + mid]) l = mid + 1;
				else r = mid - 1;
			}
			else {
				ans = mid;
				l = mid + 1;
			}
		}
		return ans;
	}
	void solve() {
		FOR(i, 1, n) {
			sa[i] = i;
			h[i].st = (1LL * h[i - 1].st * base.st % M.st + s[i] - '0' + 1) % M.st;
			h[i].nd = (1LL * h[i - 1].nd * base.nd % M.nd + s[i] - '0' + 1) % M.nd;
		}
		FOS(i, n, 1) {
			sa[i] = i;
			h1[i].st = (1LL * h1[i + 1].st * base.st % M.st + s[i] - '0' + 1) % M.st;
			h1[i].nd = (1LL * h1[i + 1].nd * base.nd % M.nd + s[i] - '0' + 1) % M.nd;
		}
		FOR(i, 1, n) {
			int l = 1, r = min(i, n - i + 1), ans = 0;
			WHILE(l <= r) {
				int mid = l + (r - l) / 2;
				if (get(i - mid + 1, i + mid - 1) == get1(i - mid + 1, i + mid - 1)) {
					ans = mid;
					l = mid + 1;
				}
				else r = mid - 1;
			}
			d[i] = ans;
		}
		FOR(i, 1, n - 1) {
			int l = 1, r = min(i, n - i), ans = 0;
			WHILE(l <= r) {
				int mid = l + (r - l) / 2;
				if (get(i - mid + 1, i + mid) == get1(i - mid + 1, i + mid)) {
					ans = mid;
					l = mid + 1;
				}
				else r = mid - 1;
			}
			d1[i] = ans;
		}
		sort(sa + 1, sa + 1 + n, cmp);
		MinSegtree s;
		Mergetree s1, s2;
		FOR(i, 1, n) {
			s1.add(1, 1, n, i, i + d[i] - 1);
			if (d1[i]) s2.add(1, 1, n, i, i + d1[i]);	
		}
		s1.dfs(1, 1, n);
		s2.dfs(1, 1, n);
		FOR(i, 1, n) s.upd(1, 1, n, i, sa[i]);
		int l = 2;
		ll rs = 1;
		FOR(i, 2, n) {
			WHILE(l <= i) {
				int L = LB(l, i), R = UB(l, i);
				if (L == -1 || s.get(1, 1, n, L, R) >= l) {
					++l;
					continue;
				}
				break;
			}
			if (l > i) continue;
			int tmp = (l + i) / 2;
			if (tmp) {
				rs += s1.get(1, 1, n, 1, tmp, i);
				rs += s2.get(1, 1, n, 1, tmp, i);
			}
			cout << rs << '\n';
		}
	}
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> n >> s;
    P[0] = {1, 1};
    FOR(i, 1, n) {
    	P[i].st = 1LL * P[i - 1].st * base.st % M.st;
    	P[i].nd = 1LL * P[i - 1].nd * base.nd % M.nd;
    }
    s = "#" + s + "#";
    FOR(i, 1, n - 1) cin >> q[i].st >> q[i].nd;
    if (n <= 1000) {
    	subtask12::solve();
    	return 0;
    }
    subtask3::solve();
    return 0;
}
/*
3
010
1 2
2 3
*/

Compilation message

Main.cpp:37: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   37 | #pragma GCC optimization ("O3")
      | 
Main.cpp:38: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   38 | #pragma GCC optimization ("unroll-loops")
      | 
Main.cpp: In function 'int subtask12::join(int, int)':
Main.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   26 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
Main.cpp:105:3: note: in expansion of macro 'EACH'
  105 |   EACH(i, a2[y]) a2[x].insert(i);
      |   ^~~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
Main.cpp:123:3: note: in expansion of macro 'FOR'
  123 |   FOR(i, 0, m1 + m2 - 1) {
      |   ^~~
Main.cpp:25:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   25 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
Main.cpp:129:3: note: in expansion of macro 'FOS'
  129 |   FOS(i, m1 + m2 - 1, 0) {
      |   ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
Main.cpp:134:3: note: in expansion of macro 'FOR'
  134 |   FOR(i, 0, m1 - 1)
      |   ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
Main.cpp:135:3: note: in expansion of macro 'FOR'
  135 |   FOR(j, m1, m1 + m2 - 1) {
      |   ^~~
Main.cpp: In function 'void subtask12::solve()':
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
Main.cpp:143:3: note: in expansion of macro 'FOR'
  143 |   FOR(i, 1, n) {
      |   ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
Main.cpp:148:3: note: in expansion of macro 'FOR'
  148 |   FOR(i, 1, n - 1) {
      |   ^~~
Main.cpp: In function 'void subtask3::solve()':
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
Main.cpp:222:3: note: in expansion of macro 'FOR'
  222 |   FOR(i, 1, n) {
      |   ^~~
Main.cpp:25:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   25 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
Main.cpp:227:3: note: in expansion of macro 'FOS'
  227 |   FOS(i, n, 1) {
      |   ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
Main.cpp:232:3: note: in expansion of macro 'FOR'
  232 |   FOR(i, 1, n) {
      |   ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
Main.cpp:244:3: note: in expansion of macro 'FOR'
  244 |   FOR(i, 1, n - 1) {
      |   ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
Main.cpp:259:3: note: in expansion of macro 'FOR'
  259 |   FOR(i, 1, n) {
      |   ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
Main.cpp:265:3: note: in expansion of macro 'FOR'
  265 |   FOR(i, 1, n) s.upd(1, 1, n, i, sa[i]);
      |   ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
Main.cpp:268:3: note: in expansion of macro 'FOR'
  268 |   FOR(i, 2, n) {
      |   ^~~
Main.cpp: In function 'int main()':
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
Main.cpp:293:5: note: in expansion of macro 'FOR'
  293 |     FOR(i, 1, n) {
      |     ^~~
Main.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
Main.cpp:298:5: note: in expansion of macro 'FOR'
  298 |     FOR(i, 1, n - 1) cin >> q[i].st >> q[i].nd;
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 5 ms 8148 KB Output is correct
3 Correct 6 ms 8200 KB Output is correct
4 Correct 6 ms 8148 KB Output is correct
5 Correct 5 ms 8148 KB Output is correct
6 Correct 5 ms 8148 KB Output is correct
7 Correct 6 ms 8148 KB Output is correct
8 Correct 5 ms 8148 KB Output is correct
9 Correct 4 ms 8148 KB Output is correct
10 Correct 5 ms 8148 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 4 ms 8148 KB Output is correct
13 Correct 5 ms 8196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 5 ms 8148 KB Output is correct
3 Correct 6 ms 8200 KB Output is correct
4 Correct 6 ms 8148 KB Output is correct
5 Correct 5 ms 8148 KB Output is correct
6 Correct 5 ms 8148 KB Output is correct
7 Correct 6 ms 8148 KB Output is correct
8 Correct 5 ms 8148 KB Output is correct
9 Correct 4 ms 8148 KB Output is correct
10 Correct 5 ms 8148 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 4 ms 8148 KB Output is correct
13 Correct 5 ms 8196 KB Output is correct
14 Correct 4 ms 8144 KB Output is correct
15 Correct 34 ms 8444 KB Output is correct
16 Correct 13 ms 8332 KB Output is correct
17 Correct 39 ms 8448 KB Output is correct
18 Correct 14 ms 8404 KB Output is correct
19 Correct 46 ms 8576 KB Output is correct
20 Correct 20 ms 8660 KB Output is correct
21 Correct 39 ms 8276 KB Output is correct
22 Correct 26 ms 8344 KB Output is correct
23 Correct 39 ms 8600 KB Output is correct
24 Correct 20 ms 8692 KB Output is correct
25 Correct 27 ms 8404 KB Output is correct
26 Correct 10 ms 8336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1089 ms 52692 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 5 ms 8148 KB Output is correct
3 Correct 6 ms 8200 KB Output is correct
4 Correct 6 ms 8148 KB Output is correct
5 Correct 5 ms 8148 KB Output is correct
6 Correct 5 ms 8148 KB Output is correct
7 Correct 6 ms 8148 KB Output is correct
8 Correct 5 ms 8148 KB Output is correct
9 Correct 4 ms 8148 KB Output is correct
10 Correct 5 ms 8148 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 4 ms 8148 KB Output is correct
13 Correct 5 ms 8196 KB Output is correct
14 Correct 4 ms 8144 KB Output is correct
15 Correct 34 ms 8444 KB Output is correct
16 Correct 13 ms 8332 KB Output is correct
17 Correct 39 ms 8448 KB Output is correct
18 Correct 14 ms 8404 KB Output is correct
19 Correct 46 ms 8576 KB Output is correct
20 Correct 20 ms 8660 KB Output is correct
21 Correct 39 ms 8276 KB Output is correct
22 Correct 26 ms 8344 KB Output is correct
23 Correct 39 ms 8600 KB Output is correct
24 Correct 20 ms 8692 KB Output is correct
25 Correct 27 ms 8404 KB Output is correct
26 Correct 10 ms 8336 KB Output is correct
27 Execution timed out 1089 ms 52692 KB Time limit exceeded
28 Halted 0 ms 0 KB -