QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#299558#7969. 套娃YaoBIGAC ✓104ms23476kbC++177.8kb2024-01-07 01:06:332024-01-07 01:06:33

Judging History

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

  • [2024-01-07 01:06:33]
  • 评测
  • 测评结果:AC
  • 用时:104ms
  • 内存:23476kb
  • [2024-01-07 01:06:33]
  • 提交

answer

#include "bits/stdc++.h"
#define rep(i, a, n) for (auto i = a; i <= (n); ++i)
#define revrep(i, a, n) for (auto i = n; i >= (a); --i)
#define all(a) a.begin(), a.end()
#define sz(a) (int)(a).size()
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, T b) { if (b < a) { a = b; return 1; } return 0; }
using namespace std;

template<class A, class B> string to_string(const pair<A, B> &p);
template<class A, class B, class C> string to_string(const tuple<A, B, C> &p);
template<class A, class B, class C, class D> string to_string(const tuple<A, B, C, D> &p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string) s); }
string to_string(char c) { return "'" + string(1, c) + "'"; }
string to_string(bool x) { return x ? "true" : "false"; }
template<class A, class T = typename A::value_type> string to_string(const A &v) {
    bool first = 1;
    string res = "{";
    for (const auto &x: v) {
        if (!first) res += ", ";
        first = 0;
        res += to_string(x);
    }
    res += "}";
    return res;
}
template<class A, class B> string to_string(const pair<A, B> &p) {
    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template<class A, class B, class C> string to_string(const tuple<A, B, C> &p) {
    return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template<class A, class B, class C, class D> string to_string(const tuple<A, B, C, D> &p) {
    return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }
template<class H, class... T> void debug_out(const H& h, const T&... t) {
    cerr << " " << to_string(h);
    debug_out(t...);
}
#ifndef ONLINE_JUDGE
    #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
    #define debug(...) if (0) puts("No effect.")
#endif

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

const int inf = 0x3f3f3f3f;

struct Tag {
	int val;
	Tag(int val = inf) : val(val) {}
	friend bool operator ==(Tag a, Tag b) { return a.val == b.val; }
	friend void tag_apply(Tag &cur, int l, int r, const Tag &x) {
		if (x.val != inf) cur = x;
	}
};

struct Info {
	int val;
	int id;
	Info(int val = inf, int id = -1): val(val), id(id) {}
	friend Info operator +(const Info &a, const Info &b) {
		return a.val < b.val ? a : b;
	}
	friend void info_apply(Info &cur, int l, int r, const Tag &x) {
		if (x.val != inf) cur.val = x.val;
	}
};

/**
 * Author: Yuhao Yao
 * Date: 24-01-07
 * Description: Segment tree with lazy propogation.
 * Usage: Always define functions \textbf{info\_apply} and \textbf{tag\_apply} to tell segment tree how you apply modification.
 *  Combine is set as plus so if you just let T be numerical type then you have range sum in the info and as range query result. To have something different, say rangeMin, define a struct with constructer and + operation.
 * Time: O(\log N) per operation.
 * Status: tested on https://codeforces.com/gym/103371/problem/M.
 */
template<class Info, class Tag>
class LazySegTree {
	#define ls i * 2
	#define rs i * 2 + 1

	int n;
	vector<Info> info;
	vector<Tag> tag;
public:
	LazySegTree(int n): n(n) {
		assert(n > 0);
		info.resize(4 << __lg(n));
		tag.resize(4 << __lg(n));
	}
	LazySegTree(const vector<Info> &init): LazySegTree(sz(init)) {
		auto build = [&](auto &dfs, int i, int l, int r) {
			if (l == r) {
				info[i] = init[l];
				return;
			}
			int mid = (l + r) >> 1;
			dfs(dfs, ls, l, mid);
			dfs(dfs, rs, mid + 1, r);
			pull(i);
		};
		build(build, 1, 0, n - 1);
	}
private:
	void pull(int i) { info[i] = info[ls] + info[rs]; }

	template<class... T>
	void apply(int i, int l, int r, const T&... val) {
		info_apply(info[i], l, r, val...);
		tag_apply(tag[i], l, r, val...);
	}

	void push(int i, int l, int r) {
		if (tag[i] == Tag{}) return;
		int mid = (l + r) >> 1;
		apply(ls, l, mid, tag[i]);
		apply(rs, mid + 1, r, tag[i]);
		tag[i] = {};
	}
public:
	template<class... T>
	void range_apply(int ql, int qr, const T&... val) {
		auto dfs = [&](auto &dfs, int i, int l, int r) {
			if (qr < l || r < ql) return;
			if (ql <= l && r <= qr) {
				apply(i, l, r, val...);
				return;
			}
			push(i, l, r);
			int mid = (l + r) >> 1;
			dfs(dfs, ls, l, mid);
			dfs(dfs, rs, mid + 1, r);
			pull(i);
		};
		dfs(dfs, 1, 0, n - 1);
	}

	Info range_ask(int ql, int qr) {
		Info res{};
		auto dfs = [&](auto &dfs, int i, int l, int r) {
			if (qr < l || r < ql) return;
			if (ql <= l && r <= qr) {
				res = res + info[i];
				return;
			}
			push(i, l, r);
			int mid = (l + r) >> 1;
			dfs(dfs, ls, l, mid);
			dfs(dfs, rs, mid + 1, r);
		};
		dfs(dfs, 1, 0, n - 1);
		return res;
	}

	int find_first(int ql, int qr, function<bool(const Info&, int, int)> func) {
		auto dfs = [&](auto &dfs, int i, int l, int r) {
			if (qr < l || r < ql || (ql <= l && r <= qr && func(info[i], l, r) == 0)) return -1;
			if (l == r) return l;

			push(i, l, r);
			int mid = (l + r) >> 1;
			int res = dfs(dfs, ls, l, mid);
			if (res != -1) return res;
			else return dfs(dfs, rs, mid + 1, r);
		};
		return dfs(dfs, 1, 0, n - 1);
	};

	int find_last(int ql, int qr, function<bool(const Info&, int, int)> func) {
		auto dfs = [&](auto &dfs, int i, int l, int r) {
			if (qr < l || r < ql || (ql <= l && r <= qr && func(info[i], l, r) == 0)) return -1;
			if (l == r) return l;

			push(i, l, r);
			int mid = (l + r) >> 1;
			int res = dfs(dfs, rs, mid + 1, r);
			if (res != -1) return res;
			else return dfs(dfs, ls, l, mid);
		};
		return dfs(dfs, 1, 0, n - 1);
	};

	#undef ls
	#undef rs
};

int main() {
    ios::sync_with_stdio(0);
    std::cin.tie(0); std::cout.tie(0);

	int n; cin >> n;
	vector<int> as(n);
	for (auto &x : as) cin >> x;

	vector<vector<int>> poss(n + 1);
	rep(i, 0, n - 1) poss[i].push_back(-1);
	rep(i, 0, n - 1) {
		int x = as[i];
		poss[x].push_back(i);
	}
	
	vector<Info> init(n, 1);
	LazySegTree<Info, Tag> seg(init);
	set<tuple<int, int, int>> S;
	rep(i, 0, n - 1) S.emplace(i, i, i);

	vector<vector<pii>> ops(n + 1);

	rep(x, 0, n - 1) {
		// debug(x, S);
		auto &pos = poss[x];
		reverse(all(pos));
		int last = n;

		for (auto p : pos) {
			auto it = S.lower_bound(make_tuple(p + 1, -1, -1));
			if (it != S.end()) {
				auto l = get<1>(*it);
				// debug(p, l, last);
				if (l < last) {
					auto mn = seg.range_ask(l, last - 1).val;
					ops[x + 1].emplace_back(mn, last - p - 1);
				}
			}			
			last = p;
		}
		last = n;
		for (auto p : pos) {
			int l = n, r = -1;
			while (1) {
				auto it = S.lower_bound(make_tuple(p, -1, -1));
				if (it == S.end()) break;
				else {
					auto [y, l2, r2] = *it;
					if (l2 >= last) break;
					if (r2 < last) {
						S.erase(it);
						chmin(l, l2);
						chmax(r, r2);
					} else {
						S.erase(it);
						S.emplace(y, last, r2);
						seg.range_apply(last, r2, last - y + 1);
						chmin(l, l2);
						chmax(r, last - 1);
					}
				}
			}
			if (l <= r) {
				S.emplace(p, l, r);
				seg.range_apply(l, r, l - p + 1);
			}
			last = p;
		}
	}
	
	set<int> rems;
	rep(i, 1, n) rems.insert(i);
	vector<int> ans(n + 1, n);
	rep(x, 1, n) {
		ops[x].emplace_back(n + 1, n + 1);
		sort(all(ops[x]));
		int last = -1;
		for (auto [l, r] : ops[x]) {
			while (1) {
				auto it = rems.upper_bound(last);
				if (it != rems.end() && *it < l) {
					ans[*it] = x - 1;
					rems.erase(it);
				} else {
					break;
				}
			}
			chmax(last, r);
		}
	}
	rep(i, 1, n) printf("%d%c", ans[i], " \n"[i == n]);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6
0 0 0 1 2 3

output:

2 3 4 0 0 0

result:

ok single line: '2 3 4 0 0 0'

Test #2:

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

input:

100
0 10 21 9 4 1 14 0 8 1 4 6 10 0 0 4 18 0 12 5 2 5 15 1 6 2 0 4 14 5 1 2 23 1 8 1 24 8 8 9 5 2 12 2 3 7 6 11 12 12 6 12 4 4 5 0 7 4 9 12 1 7 4 7 12 2 10 2 4 8 7 1 4 0 13 9 13 2 2 3 9 14 7 9 15 10 10 6 2 12 3 11 6 3 4 7 9 6 11 1

output:

2 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

result:

ok single line: '2 2 3 4 4 4 4 4 4 4 4 4 4 4 4 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'

Test #3:

score: 0
Accepted
time: 61ms
memory: 22760kb

input:

100000
127 145 474 139 36 135 75 670 76 433 206 214 283 56 214 440 147 280 244 190 181 565 31 550 261 93 526 404 125 390 17 552 5 364 53 337 52 506 277 279 15 248 46 61 826 69 166 297 171 289 150 175 111 151 317 342 166 13 199 152 308 19 156 347 205 166 45 115 177 235 422 425 109 4 658 232 347 370 4...

output:

2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 ...

result:

ok single line: '2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'

Test #4:

score: 0
Accepted
time: 65ms
memory: 22364kb

input:

100000
360 34 204 156 183 404 21 3 20 122 170 193 347 144 51 464 94 265 190 88 284 437 538 392 661 397 839 208 83 191 42 16 194 515 374 53 617 502 307 504 348 175 219 63 2 130 289 223 135 440 284 189 104 142 87 117 316 218 301 14 87 405 293 489 763 197 678 196 173 96 257 17 190 525 243 161 220 178 8...

output:

2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 ...

result:

ok single line: '2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'

Test #5:

score: 0
Accepted
time: 63ms
memory: 22056kb

input:

100000
322 408 29 271 168 161 265 412 230 177 325 60 331 226 298 143 343 83 274 706 47 234 287 46 329 248 174 351 235 38 172 171 251 355 274 307 468 11 222 309 666 137 18 440 1209 7 103 354 496 336 183 602 240 316 442 253 32 486 308 18 115 125 110 65 268 502 148 793 91 759 313 269 31 63 250 90 143 6...

output:

2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 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 0 0'

Test #7:

score: 0
Accepted
time: 90ms
memory: 23476kb

input:

100000
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100...

output:

2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

result:

ok single line: '2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0'

Test #8:

score: 0
Accepted
time: 100ms
memory: 21992kb

input:

100000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 10...

result:

ok single line: '2 3 4 5 6 7 8 9 10 11 12 13 14...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'