QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#856082#9970. Looping RPSideograph_advantage#WA 6ms4620kbC++203.8kb2025-01-13 15:52:042025-01-13 15:52:06

Judging History

你现在查看的是最新测评结果

  • [2025-01-13 15:52:06]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:4620kb
  • [2025-01-13 15:52:04]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define iter(v) v.begin(), v.end()
#define SZ(v) int(v.size())
#define pb emplace_back
#define ff first
#define ss second

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#ifdef zisk
void debug() { cerr << "\n"; }
template<class T, class ... U>
void debug(T a, U ... b){ cerr << a << " ", debug(b...); }
template<class T>
void pary(T l, T r) {
	while (l != r) cerr << *l << " ", l++;
	cerr << "\n";
}
#else
#define debug(...) void()
#define pary(...) void()
#endif

template<class A, class B>
ostream &operator<<(ostream &o, pair<A, B> p) {
	return o << '(' << p.ff << ',' << p.ss << ')';
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;

	vector<string> s(n);
	vector<pii> ord;
	const int sz = 40;
	vector rank(sz, vector<vector<int>>(n));
	for (int i = 0; i < n; i++) {
		cin >> s[i];
		for (int j = 0; j < sz; j++)
			rank[j][i].resize(SZ(s[i]));
		for (int j = 0; j < SZ(s[i]); j++)
			ord.pb(pii(i, j));
	}
	auto get_char = [&](pii p) {
		return s[p.ff][p.ss];
	};
	sort(iter(ord), [&](pii x, pii y) {
		return get_char(x) < get_char(y);
	});
	{
		char lst = 0;
		int rk = -1;
		for (int i = 0; i < SZ(ord); i++) {
			auto [x, y] = ord[i];
			if (get_char(ord[i]) != lst) {
				rk++;
				lst = get_char(ord[i]);
			}
			rank[0][x][y] = rk;
		}
	}
	//pary(iter(ord));

	for (int t = 0; t < sz - 1; t++) {
		vector<vector<pii>> bucket(SZ(ord));
		for (auto [x, y] : ord) {
			int ty = (y - (1LL << t)) % SZ(s[x]);
			ty = (ty + SZ(s[x])) % SZ(s[x]);
			//debug("QQ", x, y, ty);
			bucket[rank[t][x][ty]].pb(pii(x, ty));
		}
		auto get_pair = [&](pii p) {
			auto [x, y] = p;
			return pii(rank[t][x][y], rank[t][x][(y + (1LL << t)) % SZ(s[x])]);
		};
		/*sort(iter(ord), [&](pii x, pii y) {
			return get_pair(x) < get_pair(y);
		});*/
		ord.clear();
		for (int i = 0; i < SZ(bucket); i++)
			for (auto &p : bucket[i]) ord.pb(p);
		//pary(iter(ord));
		pii lst = pii(-1, -1);
		int rk = -1;
		for (int i = 0; i < SZ(ord); i++) {
			auto [x, y] = ord[i];
			if (get_pair(ord[i]) != lst) {
				rk++;
				lst = get_pair(ord[i]);
			}
			rank[t + 1][x][y] = rk;
		}
	}

	vector<ll> lcp(SZ(ord));
	for (int i = 0; i + 1 < SZ(ord); i++) {
		auto [x1, y1] = ord[i];
		auto [x2, y2] = ord[i + 1];
		for (int t = sz - 1; t >= 0; t--) {
			if (rank[t][x1][y1] != rank[t][x2][y2]) continue;
			y1 = (y1 + (1LL << t)) % SZ(s[x1]);
			y2 = (y2 + (1LL << t)) % SZ(s[x2]);
			lcp[i] += 1LL << t;
		}
	}

	vector<vector<ll>> tab(21, vector<ll>(SZ(ord)));
	tab[0] = lcp;
	for (int i = 1; i <= 20; i++) {
		for (int j = 0; j + (1 << i) - 1 < SZ(ord); j++) {
			tab[i][j] = min(tab[i - 1][j], tab[i - 1][j + (1 << (i - 1))]);
		}
	}
	auto query = [&](int l, int r) {
		int lg = __lg(r - l + 1);
		//debug("query", l, r, ":", min(tab[lg][l], tab[lg][r - (1 << lg) + 1]));
		return min(tab[lg][l], tab[lg][r - (1 << lg) + 1]);
	};

	vector<int> all;
	int id = 0;
	for (auto [x, y] : ord) {
		if (y == 0) all.pb(id)/*, debug("test", x)*/;
		id++;
	}

	auto find_next = [&](int x, ll low) {
		int l = x, r = n;
		while (l + 1 < r) {
			int mid = (l + r) / 2;
			if (query(all[l], all[mid] - 1) > low) l = mid;
			else r = mid;
		}
		return l;
	};
	/*pary(iter(ord));
	pary(iter(all));
	pary(iter(lcp));*/

	ll ans = 0;
	auto dfs1 = [&](auto dfs, int tl, int tr) -> void {
		if (tl >= tr) return;
		ll pos = query(all[tl], all[tr] - 1);
		//debug("try", tl, tr, ",", pos);
		int mid1 = find_next(tl, pos);
		if (mid1 >= tr) return;
		int mid2 = find_next(mid1 + 1, pos);
		//debug("dfs", tl, tr, ",", pos, ":", mid1, mid2);
		// [tl, mid1] [mid1 + 1, mid2] [mid2 + 1, tr]
		if (mid2 + 1 <= tr) {
			ans += (ll)(mid1 - tl + 1) * (mid2 - mid1)  * (tr - mid2);
		}
		dfs(dfs, tl, mid1);
		dfs(dfs, mid1 + 1, mid2);
		dfs(dfs, mid2 + 1, tr);
	};
	dfs1(dfs1, 0, n - 1);
	cout << ans << "\n";

}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3600kb

input:

6
P
PN
KK
N
PKK
PN

output:

6

result:

ok 1 number(s): "6"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3868kb

input:

10
KKKNP
KNKPPKNK
KNKPP
KNKPPKN
KKKN
NNKNNNKNNNKNNNKNNNKNNNKNNNKNNPN
NNKN
NPPN
NNKNNNKNNNKNNNKNNNKNNNKNNNK
KKKNN

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: 0
Accepted
time: 1ms
memory: 3692kb

input:

10
NNNPNNNPNNNPNNNK
KKN
NNNP
KKP
NNNPNNNPNNNPN
KKNKKNKKPN
KNNPNPNKKKNPPKNKKKNKNKKNKPPPNKKPKP
KKPK
KKNKKNK
KKPKKN

output:

3

result:

ok 1 number(s): "3"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3704kb

input:

10
K
PPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPNNP
PPKP
PPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPNNPPPK
P
K
N
P
PPPNN
N

output:

25

result:

ok 1 number(s): "25"

Test #5:

score: 0
Accepted
time: 1ms
memory: 3860kb

input:

10
NPNKP
NNNNKNNNNPP
PPKPNNNNPNKKKN
NPNKPNP
NNNNKN
NNNNK
NKNPKKPNPKKNPNKN
NKNPKKPNPKKNPNK
NKNPKKPNPKKNP
NPNKPNPN

output:

30

result:

ok 1 number(s): "30"

Test #6:

score: 0
Accepted
time: 1ms
memory: 3936kb

input:

10
KPKKPKKPKKPKKP
KPKKPKKPKKPKKPKNK
PNPNP
KPK
PN
NPNPNNPNPNK
NKKPKKPKPPKKPKKKKPKNKPPKPPNKNP
NPNPNNP
PNPNPK
NPNPN

output:

39

result:

ok 1 number(s): "39"

Test #7:

score: 0
Accepted
time: 0ms
memory: 3680kb

input:

4
KKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPK
NN
KKP
KKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKPKKNK

output:

1

result:

ok 1 number(s): "1"

Test #8:

score: 0
Accepted
time: 1ms
memory: 3724kb

input:

7
KPKN
KPKNKPKNKPKNKPKK
NKPPNNNPKKNN
KPPKPKPPKPKPPKPKPPKPKPP
KPKNKPKNKPKNKP
KPPKP
KPPKPKPPKPKPPKPKPPKPKPPKPN

output:

2

result:

ok 1 number(s): "2"

Test #9:

score: 0
Accepted
time: 1ms
memory: 3932kb

input:

10
NKNNKNKN
KPKN
PKPN
PNNNNNNKKNNPNNKNPPKPPNPNPPKKKPNNNPNPKKNK
PKPNPKP
PKPNPK
KPKNKP
NKNNKNKNNKNPN
KPKNKPK
NKNNK

output:

39

result:

ok 1 number(s): "39"

Test #10:

score: 0
Accepted
time: 6ms
memory: 4620kb

input:

300
NKNPNK
NKKNKK
KPPNPN
KKPNKNK
PKKNPKP
KPKPPPN
NNKPPNN
NPKPPKN
KNNKKPK
PPPNPKK
NKPKNP
KPKNNPP
NNPKNP
PNPPPKN
PKKPNP
PPNNKK
PKNKNK
PKNPNK
NKNPNPP
KNKNNPN
NKPPPPK
NNPPKKN
KNKKNPK
KKNNPKN
PPPKNK
NPPPPPP
NKKPKPP
KNKNPPK
KPKPNNK
NPNNKN
PNPNKP
PNPKKP
KKKKPKN
NNNKNPK
NPNKPNK
NNNKNK
PPKKNKP
NNNKPPK
KPNKPP...

output:

1102940

result:

ok 1 number(s): "1102940"

Test #11:

score: 0
Accepted
time: 4ms
memory: 4276kb

input:

91
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKN
KKKKKKKKP
PNPKPPNP
KKKN
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKN
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKP
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKN
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKP
KKKKKKKKKKKKKKKKKKKKKKN
KKKKKKKN
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKP
KKKKKKKKKKKKP
...

output:

2151

result:

ok 1 number(s): "2151"

Test #12:

score: -100
Wrong Answer
time: 5ms
memory: 4348kb

input:

72
PKPPKPPKPPKPPKPPN
PKP
NNNNNK
NPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNPNNP
NNPNNPNNPNNPNNPNNK
NP
PPPPPPN
PKPPKPPKPPKPPKPP
PPPPKPP
PPK
NNNNNPP
NNNNPNNNNPNNNNPN
KPNNNKKPPKPKKNPPKKNNKPKPKPKPPPKPPKPNNKPPKPPPNNNKKNNPKKKKKN...

output:

14796

result:

wrong answer 1st numbers differ - expected: '14794', found: '14796'