QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#201075#7513. Palindromic Beadsmendicillin2WA 317ms70416kbC++177.5kb2023-10-05 10:12:342023-10-05 10:12:35

Judging History

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

  • [2024-03-27 16:34:54]
  • hack成功,自动添加数据
  • (/hack/584)
  • [2024-03-27 16:18:45]
  • hack成功,自动添加数据
  • (/hack/583)
  • [2023-10-05 10:12:35]
  • 评测
  • 测评结果:WA
  • 用时:317ms
  • 内存:70416kb
  • [2023-10-05 10:12:34]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define per(i, a, b) for (int i = int(b)-1; i >= int(a); i--)
#define trav(a, x) for (auto& a : x)
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()

template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;

using ll = int64_t;
using vi = vc<int>;
using pii = pair<int, int>;

mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());

template <class F>
struct ycr {
	F f;
	template <class T> explicit ycr(T&& ff) : f(forward<T>(ff)) {}
	template <class... A> decltype(auto) operator()(A&&... args) {
		return f(ref(*this), forward<A>(args)...);
	}
};

template <class F> decltype(auto) yc(F&& f) {
	return ycr<decay_t<F>>(forward<F>(f));
}

struct GC {
	char buf[1<<16];
	size_t s=0,e=0;
	char operator()(){
		if(s>=e){
			buf[0]=0;
			s=0;
			e=fread(buf,1,sizeof(buf),stdin);
		}
		return buf[s++];
	}
} gc;
int read() {
	char c;
	while((c=gc())<40);
	if(c==-'-')return -read();
	int a=c-'0';
	while(isdigit(c=gc()))a=a*10+c-'0';
	return a;
}

struct hldecomp {
	int n;
	vi ord;
	vc<array<int,2>> idx;
	vc<pair<int,int>> heavy;
	hldecomp():n(0){}
	hldecomp(const vi&par):n(sz(par)),ord(n),idx(n),heavy(n){
		vvc<int> ch(n);
		for (int i = 0; i < n; i++) {
			if (par[i] != -1) {
				ch[par[i]].push_back(i);
			}
		}
		vi s(n);
		int nidx = 0;
		for (int i = 0; i < n; i++) {
			if (par[i] != -1) continue;
			yc([&](auto self, int v) -> void {
				s[v] = 1;
				for (int w : ch[v]) {
					self(w);
					s[v] += s[w];
				}
				if (!ch[v].empty()) {
					auto mit = max_element(ch[v].begin(), ch[v].end(), [&](int a, int b) {
						return s[a] < s[b];
					});
					swap(*ch[v].begin(), *mit);
				}
			})(i);
			yc([&](auto self, int v, bool rt = true) -> void {
				ord[idx[v][0] = nidx++] = v;
				if (rt) {
					heavy[idx[v][0]] = {par[v] == -1 ? -1 : idx[par[v]][0], 1};
				} else {
					assert(idx[par[v]][0] == idx[v][0]-1);
					heavy[idx[v][0]] = heavy[idx[v][0]-1];
					heavy[idx[v][0]].second++;
				}
				bool crt = false;
				for (int w : ch[v]) {
					self(w, crt);
					crt = true;
				}
				idx[v][1] = nidx;
			})(i);
		}
		assert(n == nidx);
	}

	int lca(int a, int b) {
		a = idx[a][0], b = idx[b][0];
		while (true) {
			if (a > b) swap(a, b);
			assert(a <= b);
			if (a > b - heavy[b].second) {
				return ord[a];
			}
			b = heavy[b].first;
			if (b == -1) return -1;
		}
	}

	bool in_subtree(int a, int p) const {
		return idx[p][0] <= idx[a][0] &&
			idx[a][1] <= idx[p][1];
	}
};

template <class T> void setmax(T& a, const T& b) {
	if (a < b) a = b;
}

// Reference: https://qoj.ac/submission/190725
template <class T>
struct vec2d {
	int n, s, cur = 0;
	vc<T*> x;
	vc<int> nums;
	unique_ptr<T[]> p;
	vec2d() {}
	vec2d(int n_, int s_) : n(n_), s(s_), x(n, nullptr), nums(n), p(new T[s]) {}

	void init(int i, int num) {
		assert(nums[i] == 0);
		x[i] = p.get() + cur;
		nums[i] = num;
		cur += num;
		assert(cur <= s);
	}

	T* operator [] (int i) {
		assert(0 <= i && i < n);
		return x[i];
	}
};

template <class N> struct segtree2d {
	int n, s, log;
	using P = pair<int, int>;
	vc<P> xy, yx;
	vec2d<N> v;
	vec2d<P> cut;
	segtree2d(const vc<P>& pts) {
		xy = pts;
		sort(begin(xy), end(xy));
		xy.erase(unique(begin(xy), end(xy)), end(xy));
		yx = xy;
		for (auto& [x, y] : yx) swap(x, y);
		sort(begin(yx), end(yx));

		n = sz(xy);
		log = 0;
		while ((1<<log) < n) log++;
		s = 1<<log;

		v = vec2d<N>(2*s, 2*s*(log+1));
		for (int i = 1; i < 2*s; i++) {
			int l = 31 - __builtin_clz(i);
			int w = s >> l;
			v.init(i, 2*w);
		}

		cut = vec2d<P>(s, log*n+s);
		for (int i = 1; i < s; i++) {
			int l = 31 - __builtin_clz(i);
			int w = s >> l;
			int st = w * (i & ((1<<l)-1));
			int en = st + w;
			cut.init(i, max(min(en, n)-st, 0) + 1);
			cut[i][0] = {0, 0};
		}

		vc<int> cnt(2*s);
		for (auto& [y, x] : yx) {
			int i = getidx(xy, P(x, y)) + s;
			while (i) {
				cnt[i]++;
				if (i < s) {
					cut[i][cnt[i]] = {cnt[2*i], cnt[2*i+1]};
				}
				i /= 2;
			}
		}
	}

	template <class F> void point_apply_single(int i, int w, int j, F f) {
		j += w;
		f(v[i][j]);
		while (j /= 2) {
			v[i][j] = N::merge(v[i][2*j], v[i][2*j+1]);
		}
	}

	template <class F> void point_apply(const P& p, F f) {
		int i = 1;
		int j = getidx(yx, {p.second, p.first});
		int l = 0, r = s;
		for (int k = 0; k <= log; k++) {
			point_apply_single(i, r-l, j, f);
			if (k < log) {
				int m = (l+r)/2;
				if (m <= n && xy[m-1] < p) {
					l = m;
					j = cut[i][j].second;
					i = 2*i+1;
				} else {
					r = m;
					j = cut[i][j].first;
					i = 2*i;
				}
			}
		}
	}

	N query_single(int i, int w, int l, int r) {
		N res;
		for (l += w, r += w; l < r; l /= 2, r /= 2) {
			if (l & 1) res = N::merge(res, v[i][l++]);
			if (r & 1) res = N::merge(res, v[i][--r]);
		}
		return res;
	}

	N query(int qxl, int qxr, int qyl, int qyr) {
		static const int inf = numeric_limits<int>::max();
		qxl = getidx(xy, {qxl, -inf});
		qxr = getidx(xy, {qxr, -inf});
		qyl = getidx(yx, {qyl, -inf});
		qyr = getidx(yx, {qyr, -inf});
		return yc([&](auto self, int i, int l, int r, int yl, int yr) -> N {
			if (qxr <= l || r <= qxl) {
				return N();
			} else if (qxl <= l && r <= qxr) {
				return query_single(i, r-l, yl, yr);
			} else {
				int m = (l+r)/2;
				return N::merge(
					self(2*i, l, m, cut[i][yl].first, cut[i][yr].first),
					self(2*i+1, m, r, cut[i][yl].second, cut[i][yr].second)
				);
			}
		})(1, 0, s, qyl, qyr);
	}

	int getidx(const vc<P>& c, const P& a) {
		return int(lower_bound(begin(c), end(c), a) - begin(c));
	}
};

struct max_node {
	int v = 0;
	max_node() {}

	static max_node merge(const max_node& l, const max_node& r) {
		max_node res;
		res.v = max(l.v, r.v);
		return res;
	}
};

int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	cout << fixed << setprecision(20);

	int N;
	cin >> N;
	vector<vector<int>> locs(N);
	for (int i = 0; i < N; i++) {
		int c;
		cin >> c;
		c--;
		locs[c].push_back(i);
	}

	vector<vector<int>> adj(N);
	for (int e = 0; e < N-1; e++) {
		int a, b;
		cin >> a >> b;
		a--, b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}

	vector<int> par(N, -1);
	vector<int> depth(N, 0);
	yc([&](auto self, int cur, int prv, int d) -> void {
		par[cur] = prv;
		depth[cur] = d;
		for (int nxt : adj[cur]) {
			if (nxt == prv) continue;
			self(nxt, cur, d+1);
		}
	})(0, -1, 0);
	auto hld = hldecomp(par);

	vector<tuple<int, int, int>> stuff;
	stuff.reserve(N/2);
	for (int c = 0; c < N; c++) {
		if (locs[c].size() <= 1) continue;
		assert(locs[c].size() == 2);
		int x = locs[c][0];
		int y = locs[c][1];
		int a = hld.lca(x, y);
		int d = depth[x] + depth[y] - 2 * depth[a];
		stuff.emplace_back(d, x, y);
	}

	const auto& idx = hld.idx;

	sort(stuff.begin(), stuff.end());
	reverse(stuff.begin(), stuff.end());
	int ans = 1;

	vc<pair<int, int>> pts;
	pts.reserve(sz(stuff));
	for (auto [d, x, y] : stuff) {
		pts.emplace_back(x, y);
	}
	segtree2d<max_node> st(pts);

	for (auto [d, x, y] : stuff) {
		assert(x != y);

		int v;
		if (idx[x][0] > idx[y][0]) swap(x, y);
		int a = hld.lca(x, y);
		if (a != x) {
			v = st.query(idx[x][0], idx[x][1], idx[y][0], idx[y][1]).v;
		} else {
			int c = -1;
			for (int z : adj[x]) {
				if (z == par[x]) continue;
				if (hld.in_subtree(y, z)) {
					c = z;
					break;
				}
			}
			assert(c != -1);
			assert(hld.in_subtree(c, x) && hld.in_subtree(y, c));
			v = st.query(0, idx[c][0], idx[y][0], idx[y][1]).v;
			setmax(v, st.query(idx[y][0], idx[y][1], idx[c][1], N).v);
		}
		v += 2;

		setmax(ans, v + int(d >= 2));

		st.point_apply({idx[x][0], idx[y][0]}, [&](max_node& n) -> void {
			n.v = v;
		});
	}
	cout << ans << '\n';
}

詳細信息

Test #1:

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

input:

4
1 1 2 2
1 2
2 3
2 4

output:

3

result:

ok single line: '3'

Test #2:

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

input:

5
1 3 2 2 1
1 2
2 3
3 4
4 5

output:

4

result:

ok single line: '4'

Test #3:

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

input:

6
1 1 2 2 3 3
1 2
2 3
3 4
4 5
5 6

output:

2

result:

ok single line: '2'

Test #4:

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

input:

6
1 2 3 4 5 6
1 2
2 3
3 4
4 5
5 6

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 2ms
memory: 3960kb

input:

2000
845 1171 345 282 1181 625 754 289 681 493 423 840 1494 318 266 1267 967 379 135 14 39 191 60 972 116 1216 1205 19 194 185 1360 861 379 430 1262 1151 756 65 389 488 277 53 1283 1438 101 1465 195 714 737 980 80 298 961 1326 163 1163 1317 1152 992 35 334 802 1502 486 710 234 555 88 1278 146 46 696...

output:

5

result:

ok single line: '5'

Test #6:

score: 0
Accepted
time: 317ms
memory: 70228kb

input:

200000
48015 47923 20609 71806 43752 68214 95683 89449 25809 58110 19878 52931 7845 45206 86245 82945 62977 37876 12456 105915 10509 92943 66950 88545 26442 26545 42278 66977 3970 9631 21524 43638 7979 58240 25719 56260 276 89721 9553 16550 52161 30307 82748 108443 36676 48581 59069 57412 62453 7965...

output:

5

result:

ok single line: '5'

Test #7:

score: 0
Accepted
time: 294ms
memory: 70152kb

input:

200000
13011 51198 65374 107045 66506 14385 35784 94265 71449 41817 24646 60714 53382 68358 9354 840 3139 71282 72215 69550 2121 41498 13675 76444 67690 40513 56439 12832 51976 35333 47208 59602 98993 9383 77866 10464 41517 89125 58804 91741 66160 74208 70991 63865 84870 14282 2441 78046 73372 36311...

output:

7

result:

ok single line: '7'

Test #8:

score: -100
Wrong Answer
time: 304ms
memory: 70416kb

input:

200000
38715 33241 65919 39407 27500 36200 2259 42301 79147 57505 20 81399 69499 23658 14534 86934 14352 69558 59763 43318 35360 3281 38188 40058 40571 103709 75625 8434 53802 87159 98628 69421 53711 47986 18350 6079 37362 39377 71936 89573 25983 66882 48999 58918 66432 17453 82515 9588 95375 87287 ...

output:

55

result:

wrong answer 1st lines differ - expected: '45', found: '55'