Submission #547787

# Submission time Handle Problem Language Result Execution time Memory
547787 2022-04-11T18:27:54 Z vovamr Werewolf (IOI18_werewolf) C++14
100 / 100
848 ms 353928 KB
#include "werewolf.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) 	(x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }

const int N = 6e6 + 200;

ve<int> gr1[N]; int p1[N], w1 = 0;
inline int par1(int v) { if (p1[v] == v) { return v; } return (p1[v] = par1(p1[v])); }
inline void add1(int a, int b) {
	a = par1(a), b = par1(b);
	if (a == b) return;
	int c = w1++;
	p1[a] = p1[b] = p1[c] = c;
	gr1[c].pb(a);
	if (a ^ b) gr1[c].pb(b);
} int ti1 = 0;
int in1[N], out1[N];
inline void dfs1(int v) {
	in1[v] = ti1++;
	for (auto &to : gr1[v]) dfs1(to);
	out1[v] = ti1 - 1;
}

ve<int> gr2[N]; int p2[N], w2 = 0;
inline int par2(int v) { if (p2[v] == v) { return v; } return (p2[v] = par2(p2[v])); }
inline void add2(int a, int b) {
	a = par2(a), b = par2(b);
	if (a == b) return;
	int c = w2++;
	p2[a] = p2[b] = p2[c] = c;
	gr2[c].pb(a);
	if (a ^ b) gr2[c].pb(b);
} int ti2 = 0;
int in2[N], out2[N];
inline void dfs2(int v) {
	in2[v] = ti2++;
	for (auto &to : gr2[v]) dfs2(to);
	out2[v] = ti2 - 1;
}

int fe[N];
inline void upd(int i, int x) { i += 3; for (; i < N; i += i & -i) fe[i] += x; }
inline int get(int i) { int ans = 0; i += 3; for (; i; i -= i & -i) { ans += fe[i]; } return ans; }
inline int get(int l, int r) { return get(r) - get(l - 1); }

std::vector<int> check_validity(int N, std::vector<int> X, std::vector<int> Y,
                                std::vector<int> S, std::vector<int> E,
                                std::vector<int> L, std::vector<int> R) {
	int n = N;
	int m = sz(X); int q = sz(S);
	ve<array<int,3>> e(m);

	for (int i = 0; i < m; ++i) {
		int v, u;
		tie(v, u) = mpp(X[i], Y[i]);
		e[i] = {min(v, u), v, u};
	} sort(all(e));

	w1 = w2 = n;
	for (int i = 0; i < n; ++i) p1[i] = p2[i] = i;

	ve<array<int,5>> que(q);
	for (int i = 0; i < q; ++i) que[i] = {S[i], E[i], L[i], R[i], i};
	sort(all(que), [](const auto &a, const auto &b) { return a[2] > b[2]; });

	ve<int> rt1(q), rt2(q);

	int ptr = sz(e) - 1, ptr1 = 0;
	for (auto &[v, u, l, r, id] : que) {
		while (~ptr && e[ptr][0] >= l) {
			add1(e[ptr][1], e[ptr][2]);
			--ptr;
		} rt1[id] = par1(v);
	}

	for (int i = 0; i < m; ++i) {
		int v, u;
		tie(v, u) = mpp(X[i], Y[i]);
		e[i] = {max(v, u), v, u};
	} sort(all(e));

	sort(all(que), [](const auto &a, const auto &b) { return a[3] < b[3]; });
	for (auto &[v, u, l, r, id] : que) {
		while (ptr1 < sz(e) && e[ptr1][0] <= r) {
			add2(e[ptr1][1], e[ptr1][2]);
			++ptr1;
		} rt2[id] = par2(u);
	}

	for (int i = 0; i < w1; ++i) if (p1[i] == i) dfs1(i);
	for (int i = 0; i < w2; ++i) if (p2[i] == i) dfs2(i);

	ve<int> a(n), b(n);
	for (int i = 0; i < n; ++i) a[i] = in1[i], b[i] = in2[i];
	sort(all(a)), sort(all(b));
	a.erase(unique(all(a)), a.end());
	b.erase(unique(all(b)), b.end());

	ve<int> c(n), d(n);
	for (int i = 0; i < n; ++i) c[i] = lower_bound(all(a), in1[i]) - a.begin();
	for (int i = 0; i < n; ++i) d[i] = lower_bound(all(b), in2[i]) - b.begin();

	ve<array<int,5>> hui;
	for (int i = 0; i < q; ++i) {
		int l1 = in1[rt1[i]], r1 = out1[rt1[i]];
		int l2 = in2[rt2[i]], r2 = out2[rt2[i]];

		l1 = lower_bound(all(a), l1) - a.begin();
		r1 = upper_bound(all(a), r1) - a.begin() - 1;
		l2 = lower_bound(all(b), l2) - b.begin();
		r2 = upper_bound(all(b), r2) - b.begin() - 1;

		if (l1 > r1 || l2 > r2) continue;
		hui.pb({r2    , l1, r1, +1, i});
		hui.pb({l2 - 1, l1, r1, -1, i});
	}

	sort(all(hui));

	ve<pii> al(n);
	for (int i = 0; i < n; ++i) al[i] = {d[i], i};
	sort(all(al));

	ptr = 0;
	ve<int> ans(q);
	for (auto &[pos, l, r, sgn, id] : hui) {
		while (ptr < sz(al) && al[ptr].fi <= pos) {
			upd(c[al[ptr].se], +1);
			++ptr;
		} ans[id] += get(l, r) * sgn;
	}
	for (auto &i : ans) i = !!i;
	return ans;
}

#ifdef LOCAL
namespace {

int read_int() {
  int x;
  if (scanf("%d", &x) != 1) {
    fprintf(stderr, "Error while reading input\n");
    exit(1);
  }
  return x;
}

}  // namespace

int main() {
  int N = read_int();
  int M = read_int();
  int Q = read_int();
  std::vector<int> X(M), Y(M), S(Q), E(Q), L(Q), R(Q);
  for (int j = 0; j < M; ++j) {
    X[j] = read_int();
    Y[j] = read_int();
  }
  for (int i = 0; i < Q; ++i) {
    S[i] = read_int();
    E[i] = read_int();
    L[i] = read_int();
    R[i] = read_int();
  }

  std::vector<int> A = check_validity(N, X, Y, S, E, L, R);
  for (size_t i = 0; i < A.size(); ++i) {
    printf("%d\n", A[i]);
  }
  return 0;
}
#endif

Compilation message

werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:84:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   84 |  for (auto &[v, u, l, r, id] : que) {
      |             ^
werewolf.cpp:98:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   98 |  for (auto &[v, u, l, r, id] : que) {
      |             ^
werewolf.cpp:141:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  141 |  for (auto &[pos, l, r, sgn, id] : hui) {
      |             ^
# Verdict Execution time Memory Grader output
1 Correct 128 ms 282188 KB Output is correct
2 Correct 144 ms 282248 KB Output is correct
3 Correct 132 ms 282380 KB Output is correct
4 Correct 131 ms 282212 KB Output is correct
5 Correct 128 ms 282188 KB Output is correct
6 Correct 132 ms 282192 KB Output is correct
7 Correct 131 ms 282156 KB Output is correct
8 Correct 129 ms 282188 KB Output is correct
9 Correct 139 ms 282220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 282188 KB Output is correct
2 Correct 144 ms 282248 KB Output is correct
3 Correct 132 ms 282380 KB Output is correct
4 Correct 131 ms 282212 KB Output is correct
5 Correct 128 ms 282188 KB Output is correct
6 Correct 132 ms 282192 KB Output is correct
7 Correct 131 ms 282156 KB Output is correct
8 Correct 129 ms 282188 KB Output is correct
9 Correct 139 ms 282220 KB Output is correct
10 Correct 147 ms 283224 KB Output is correct
11 Correct 136 ms 283168 KB Output is correct
12 Correct 137 ms 283128 KB Output is correct
13 Correct 149 ms 283224 KB Output is correct
14 Correct 133 ms 283236 KB Output is correct
15 Correct 153 ms 283252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 810 ms 343828 KB Output is correct
2 Correct 691 ms 346408 KB Output is correct
3 Correct 709 ms 344772 KB Output is correct
4 Correct 774 ms 344120 KB Output is correct
5 Correct 780 ms 343996 KB Output is correct
6 Correct 761 ms 344028 KB Output is correct
7 Correct 734 ms 343796 KB Output is correct
8 Correct 654 ms 346436 KB Output is correct
9 Correct 645 ms 344792 KB Output is correct
10 Correct 665 ms 343956 KB Output is correct
11 Correct 719 ms 343964 KB Output is correct
12 Correct 747 ms 343852 KB Output is correct
13 Correct 778 ms 346420 KB Output is correct
14 Correct 692 ms 346512 KB Output is correct
15 Correct 728 ms 346472 KB Output is correct
16 Correct 719 ms 346472 KB Output is correct
17 Correct 736 ms 343936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 282188 KB Output is correct
2 Correct 144 ms 282248 KB Output is correct
3 Correct 132 ms 282380 KB Output is correct
4 Correct 131 ms 282212 KB Output is correct
5 Correct 128 ms 282188 KB Output is correct
6 Correct 132 ms 282192 KB Output is correct
7 Correct 131 ms 282156 KB Output is correct
8 Correct 129 ms 282188 KB Output is correct
9 Correct 139 ms 282220 KB Output is correct
10 Correct 147 ms 283224 KB Output is correct
11 Correct 136 ms 283168 KB Output is correct
12 Correct 137 ms 283128 KB Output is correct
13 Correct 149 ms 283224 KB Output is correct
14 Correct 133 ms 283236 KB Output is correct
15 Correct 153 ms 283252 KB Output is correct
16 Correct 810 ms 343828 KB Output is correct
17 Correct 691 ms 346408 KB Output is correct
18 Correct 709 ms 344772 KB Output is correct
19 Correct 774 ms 344120 KB Output is correct
20 Correct 780 ms 343996 KB Output is correct
21 Correct 761 ms 344028 KB Output is correct
22 Correct 734 ms 343796 KB Output is correct
23 Correct 654 ms 346436 KB Output is correct
24 Correct 645 ms 344792 KB Output is correct
25 Correct 665 ms 343956 KB Output is correct
26 Correct 719 ms 343964 KB Output is correct
27 Correct 747 ms 343852 KB Output is correct
28 Correct 778 ms 346420 KB Output is correct
29 Correct 692 ms 346512 KB Output is correct
30 Correct 728 ms 346472 KB Output is correct
31 Correct 719 ms 346472 KB Output is correct
32 Correct 736 ms 343936 KB Output is correct
33 Correct 848 ms 344468 KB Output is correct
34 Correct 647 ms 327024 KB Output is correct
35 Correct 806 ms 346884 KB Output is correct
36 Correct 828 ms 344524 KB Output is correct
37 Correct 791 ms 346140 KB Output is correct
38 Correct 821 ms 344880 KB Output is correct
39 Correct 742 ms 349108 KB Output is correct
40 Correct 827 ms 353764 KB Output is correct
41 Correct 777 ms 345556 KB Output is correct
42 Correct 712 ms 344448 KB Output is correct
43 Correct 813 ms 351000 KB Output is correct
44 Correct 782 ms 346140 KB Output is correct
45 Correct 680 ms 349492 KB Output is correct
46 Correct 720 ms 349088 KB Output is correct
47 Correct 691 ms 346720 KB Output is correct
48 Correct 682 ms 346548 KB Output is correct
49 Correct 686 ms 346728 KB Output is correct
50 Correct 680 ms 346740 KB Output is correct
51 Correct 807 ms 353928 KB Output is correct
52 Correct 814 ms 353816 KB Output is correct