Submission #831219

# Submission time Handle Problem Language Result Execution time Memory
831219 2023-08-19T23:44:32 Z NK_ Towers (NOI22_towers) C++17
100 / 100
1436 ms 119544 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;
 
#define nl '\n'
#define pb push_back
#define pf push_front

#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
 
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;

using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;

using db = long double;

template<class T> using pq = priority_queue<T, V<T>, greater<T>>;

const int MOD = 1e9 + 7;
const ll INFL = ll(1e16) + 10;

struct Scanner {
    FILE* stream;
    Scanner(FILE* s) : stream(s) {}
    char buf[1 << 20], * l = buf, * r = buf;
    bool flush() { l = buf; r = l + fread(buf, 1, 1 << 20, stream); return l == r; }
    void get(char& c) { c = l == r && flush() ? ' ' : *l++; }
    friend Scanner& operator >>(Scanner& in, char& c) { return in.get(c), in; }
    friend Scanner& operator >>(Scanner& in, char* s) {
        for (in.get(s[0]); isspace(s[0]); in.get(s[0]));
        for (int i = 0; !isspace(s[i]) || (s[i] = '\0'); i++) in.get(s[i + 1]);
        return in;
    }
    friend Scanner& operator >>(Scanner& in, std::string& s) {
        char c;
        for (in.get(c); isspace(c); in.get(c));
        for (s = ""; !isspace(c); in.get(c)) s += c;
        return in;
    }
    template <class T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
    friend Scanner& operator >>(Scanner& in, T& x) {
        char c, f = '+';
        for (in.get(c); !isdigit(c); in.get(c))
            if constexpr (std::is_signed_v<T>) f = c;
        for (x = 0; isdigit(c); in.get(c)) x = x * (1 << 1) + x * (1 << 3) + c - '0';
        if constexpr (std::is_signed_v<T>) x = f == '-' ? -x : x;
        return in;
    }
    template <class T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
    friend Scanner& operator >>(Scanner& in, T& x) {
        std::string s; in >> s; x = std::stod(s);
        return in;
    }
    template <class T, class U>
    friend Scanner& operator >>(Scanner& in, std::pair<T, U>& a) {
        return in >> a.first >> a.second;
    }
    template <class T, size_t S>
    friend Scanner& operator >>(Scanner& in, std::array<T, S>& a) {
        for (int i = 0, n = S; i < n; i++) in >> a[i];
        return in;
    }
    template <class T>
    friend Scanner& operator >>(Scanner& in, std::vector<T>& a) {
        for (int i = 0, n = a.size(); i < n; i++) in >> a[i];
        return in;
    }
};
 
struct Printer {
    FILE* stream;
    Printer(FILE* s) : stream(s) {}
    char buf[1 << 20], * l = buf, * r = buf + (1 << 20) - 1;
    int format = 0, precision = 15;
    ~Printer() { flush(); }
    void flush() { fwrite(buf, 1, l - buf, stream); l = buf; }
    void put(const char& c) { *l++ = c; if (l == r) flush(); }
    friend Printer& operator <<(Printer& out, const char& c) { return out.put(c), out; }
    friend Printer& operator <<(Printer& out, const char* s) {
        for (int i = 0; s[i] != '\0'; i++) out.put(s[i]);
        return out;
    }
    friend Printer& operator <<(Printer& out, const std::string& s) {
        for (int i = 0, n = s.size(); i < n; i++) out.put(s[i]);
        return out;
    }
    template <class T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
    friend Printer& operator <<(Printer& out, T x) {
        static char s[40];
        static int i = 0;
        if (x == 0) { out.put('0'); return out; }
        if constexpr (std::is_signed_v<T>) x = x < 0 ? out.put('-'), -x : x;
        while (x > 0) s[++i] = x % 10 + '0', x /= 10;
        while (i > 0) out.put(s[i--]);
        return out;
    }
    template <class T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
    friend Printer& operator <<(Printer& out, T x) {
        std::ostringstream oss;
        oss << std::fixed << std::setprecision(out.precision) << x;
        return out << oss.str();
    }
    template <class T, class U>
    friend Printer& operator <<(Printer& out, const std::pair<T, U>& a) {
        return out << a.first << " \n"[out.format > 1] << a.second;
    }
    template <class T, size_t S>
    friend Printer& operator <<(Printer& out, const std::array<T, S>& a) {
        out << a[0];
        for (int i = 1, n = S; i < n; i++) out << " \n"[out.format > 1] << a[i];
        return out;
    }
    template <class T>
    friend Printer& operator <<(Printer& out, const std::vector<T>& a) {
        if (!a.empty()) out << a[0];
        for (int i = 1, n = a.size(); i < n; i++) out << " \n"[out.format > 0] << a[i];
        return out;
    }
};

Scanner in(stdin);
Printer out(stdout);

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N; in >> N;
	vpi A(N); for(auto& x : A) {
		in >> x.f >> x.s;
	}

	int ROWS = 0, COLS = 0;
	for(int t = 0; t < 2; t++) {
		vi X; for(auto& x : A) X.pb(x.f); 

		sort(begin(X), end(X)); X.erase(unique(begin(X), end(X)), end(X));

		for(auto& x : A) {
			x.f = lower_bound(begin(X), end(X), x.f) - begin(X);
			swap(x.f, x.s);
		}

		ROWS = sz(X); swap(ROWS, COLS);
	}

	unordered_map<ll, int> IDX; for(int i = 0; i < N; i++) {
		// cerr << A[i].f << " " << A[i].s << " is " << i << endl;
		IDX[A[i].f * 1LL * COLS + A[i].s] = i;
	}

	V<vi> C(COLS);
	for(int i = 0; i < N; i++) C[A[i].s].pb(A[i].f);
	for(int i = 0; i < COLS; i++) sort(begin(C[i]), end(C[i]));

	const pi EMP = mp(-1, -1);
	vpi RF(ROWS, EMP); 
	vpi RB(ROWS, EMP); 


	vi POS(2 * COLS, -1); int Q = 0;
	vi stk;
	function<void(int)> update = [&](int X) {
		stk.pb(X);

		while(sz(stk)) {

			int x = stk.back(); stk.pop_back(); 
			if (POS[x] == -1) continue;

			// 2*y+c => y is col # and c is dir (0 - dwn (+1), 1 - up (-1));
			int y = x / 2, dir = (x % 2 ? -1 : +1);
			while(1) {
				Q++; assert(Q <= 5 * N);

				if (POS[x] < 0 || POS[x] >= sz(C[y])) {
					POS[x] = -1; break;
				}

				if (POS[2*y] == POS[2*y+1]) {
					POS[x] = -1;
					break; // stop because same as other one
				}

				int r = C[y][POS[x]]; // row

				// check if you can put it in
				if (RB[r] == EMP || RB[r].f < y) { // sz(R[r]) == 2 put in at the back
					if (RB[r] != EMP) {
						int i = RB[r].s;
						RB[r] = mp(y, x);
						stk.pb(i); 
					} else RB[r] = mp(y, x);
					break;
				} 

				if (RF[r] == EMP || y < RF[r].f) { // sz(R[r]) == 2 put in at the front
					if (RF[r] != EMP) {
						int i = RF[r].s;
						RF[r] = mp(y, x);
						stk.pb(i);
					} else RF[r] = mp(y, x);
					break;
				}

				POS[x] += dir;
			}
		}
	};


	for(int c = 0; c < COLS; c++) {
		POS[2 * c] = 0; 
		update(2 * c); 

		POS[2 * c + 1] = sz(C[c]) - 1;
		update(2 * c + 1);
	}

	vpi take;
	for(int i = 0; i < 2 * COLS; i++) {
		int c = i / 2; 
		if (0 <= POS[i] && POS[i] < sz(C[c])) {
			take.pb(mp(C[c][POS[i]], c));
		}
	}

	string ans(N, '0');
	for(auto& p : take) {
		ans[IDX[p.f * 1LL * COLS + p.s]] = '1';
	}

	out << ans << nl;

	// cerr << Q << endl;


    return 0;
} 	
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 6804 KB Output is correct
2 Correct 21 ms 7748 KB Output is correct
3 Correct 55 ms 19812 KB Output is correct
4 Correct 113 ms 37044 KB Output is correct
5 Correct 19 ms 8188 KB Output is correct
6 Correct 4 ms 1960 KB Output is correct
7 Correct 122 ms 35556 KB Output is correct
8 Correct 72 ms 26296 KB Output is correct
9 Correct 197 ms 56392 KB Output is correct
10 Correct 167 ms 48336 KB Output is correct
11 Correct 186 ms 57888 KB Output is correct
12 Correct 182 ms 57704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 766 ms 74564 KB Output is correct
2 Correct 775 ms 74700 KB Output is correct
3 Correct 754 ms 74660 KB Output is correct
4 Correct 754 ms 74568 KB Output is correct
5 Correct 764 ms 74668 KB Output is correct
6 Correct 1396 ms 119452 KB Output is correct
7 Correct 1413 ms 119340 KB Output is correct
8 Correct 1380 ms 119420 KB Output is correct
9 Correct 1436 ms 119544 KB Output is correct
10 Correct 1388 ms 119336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 3 ms 1108 KB Output is correct
27 Correct 2 ms 1108 KB Output is correct
28 Correct 2 ms 724 KB Output is correct
29 Correct 2 ms 724 KB Output is correct
30 Correct 2 ms 724 KB Output is correct
31 Correct 2 ms 724 KB Output is correct
32 Correct 2 ms 724 KB Output is correct
33 Correct 2 ms 724 KB Output is correct
34 Correct 2 ms 724 KB Output is correct
35 Correct 2 ms 724 KB Output is correct
36 Correct 2 ms 724 KB Output is correct
37 Correct 2 ms 724 KB Output is correct
38 Correct 2 ms 596 KB Output is correct
39 Correct 2 ms 700 KB Output is correct
40 Correct 2 ms 596 KB Output is correct
41 Correct 2 ms 596 KB Output is correct
42 Correct 2 ms 596 KB Output is correct
43 Correct 1 ms 596 KB Output is correct
44 Correct 1 ms 596 KB Output is correct
45 Correct 1 ms 596 KB Output is correct
46 Correct 2 ms 596 KB Output is correct
47 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 3 ms 1108 KB Output is correct
27 Correct 2 ms 1108 KB Output is correct
28 Correct 2 ms 724 KB Output is correct
29 Correct 2 ms 724 KB Output is correct
30 Correct 2 ms 724 KB Output is correct
31 Correct 2 ms 724 KB Output is correct
32 Correct 2 ms 724 KB Output is correct
33 Correct 2 ms 724 KB Output is correct
34 Correct 2 ms 724 KB Output is correct
35 Correct 2 ms 724 KB Output is correct
36 Correct 2 ms 724 KB Output is correct
37 Correct 2 ms 724 KB Output is correct
38 Correct 2 ms 596 KB Output is correct
39 Correct 2 ms 700 KB Output is correct
40 Correct 2 ms 596 KB Output is correct
41 Correct 2 ms 596 KB Output is correct
42 Correct 2 ms 596 KB Output is correct
43 Correct 1 ms 596 KB Output is correct
44 Correct 1 ms 596 KB Output is correct
45 Correct 1 ms 596 KB Output is correct
46 Correct 2 ms 596 KB Output is correct
47 Correct 1 ms 596 KB Output is correct
48 Correct 59 ms 15932 KB Output is correct
49 Correct 59 ms 15868 KB Output is correct
50 Correct 65 ms 13244 KB Output is correct
51 Correct 65 ms 13248 KB Output is correct
52 Correct 45 ms 8384 KB Output is correct
53 Correct 44 ms 8356 KB Output is correct
54 Correct 43 ms 8384 KB Output is correct
55 Correct 36 ms 7788 KB Output is correct
56 Correct 38 ms 7696 KB Output is correct
57 Correct 36 ms 7712 KB Output is correct
58 Correct 31 ms 7624 KB Output is correct
59 Correct 31 ms 7576 KB Output is correct
60 Correct 24 ms 7484 KB Output is correct
61 Correct 24 ms 7592 KB Output is correct
62 Correct 24 ms 7592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 16 ms 6804 KB Output is correct
27 Correct 21 ms 7748 KB Output is correct
28 Correct 55 ms 19812 KB Output is correct
29 Correct 113 ms 37044 KB Output is correct
30 Correct 19 ms 8188 KB Output is correct
31 Correct 4 ms 1960 KB Output is correct
32 Correct 122 ms 35556 KB Output is correct
33 Correct 72 ms 26296 KB Output is correct
34 Correct 197 ms 56392 KB Output is correct
35 Correct 167 ms 48336 KB Output is correct
36 Correct 186 ms 57888 KB Output is correct
37 Correct 182 ms 57704 KB Output is correct
38 Correct 766 ms 74564 KB Output is correct
39 Correct 775 ms 74700 KB Output is correct
40 Correct 754 ms 74660 KB Output is correct
41 Correct 754 ms 74568 KB Output is correct
42 Correct 764 ms 74668 KB Output is correct
43 Correct 1396 ms 119452 KB Output is correct
44 Correct 1413 ms 119340 KB Output is correct
45 Correct 1380 ms 119420 KB Output is correct
46 Correct 1436 ms 119544 KB Output is correct
47 Correct 1388 ms 119336 KB Output is correct
48 Correct 3 ms 1108 KB Output is correct
49 Correct 2 ms 1108 KB Output is correct
50 Correct 2 ms 724 KB Output is correct
51 Correct 2 ms 724 KB Output is correct
52 Correct 2 ms 724 KB Output is correct
53 Correct 2 ms 724 KB Output is correct
54 Correct 2 ms 724 KB Output is correct
55 Correct 2 ms 724 KB Output is correct
56 Correct 2 ms 724 KB Output is correct
57 Correct 2 ms 724 KB Output is correct
58 Correct 2 ms 724 KB Output is correct
59 Correct 2 ms 724 KB Output is correct
60 Correct 2 ms 596 KB Output is correct
61 Correct 2 ms 700 KB Output is correct
62 Correct 2 ms 596 KB Output is correct
63 Correct 2 ms 596 KB Output is correct
64 Correct 2 ms 596 KB Output is correct
65 Correct 1 ms 596 KB Output is correct
66 Correct 1 ms 596 KB Output is correct
67 Correct 1 ms 596 KB Output is correct
68 Correct 2 ms 596 KB Output is correct
69 Correct 1 ms 596 KB Output is correct
70 Correct 59 ms 15932 KB Output is correct
71 Correct 59 ms 15868 KB Output is correct
72 Correct 65 ms 13244 KB Output is correct
73 Correct 65 ms 13248 KB Output is correct
74 Correct 45 ms 8384 KB Output is correct
75 Correct 44 ms 8356 KB Output is correct
76 Correct 43 ms 8384 KB Output is correct
77 Correct 36 ms 7788 KB Output is correct
78 Correct 38 ms 7696 KB Output is correct
79 Correct 36 ms 7712 KB Output is correct
80 Correct 31 ms 7624 KB Output is correct
81 Correct 31 ms 7576 KB Output is correct
82 Correct 24 ms 7484 KB Output is correct
83 Correct 24 ms 7592 KB Output is correct
84 Correct 24 ms 7592 KB Output is correct
85 Correct 1 ms 212 KB Output is correct
86 Correct 0 ms 212 KB Output is correct
87 Correct 0 ms 212 KB Output is correct
88 Correct 1356 ms 116904 KB Output is correct
89 Correct 1355 ms 116956 KB Output is correct
90 Correct 1363 ms 116960 KB Output is correct
91 Correct 806 ms 69088 KB Output is correct
92 Correct 788 ms 68992 KB Output is correct
93 Correct 805 ms 69056 KB Output is correct
94 Correct 711 ms 64140 KB Output is correct
95 Correct 725 ms 64228 KB Output is correct
96 Correct 715 ms 64272 KB Output is correct
97 Correct 688 ms 63516 KB Output is correct
98 Correct 681 ms 63572 KB Output is correct
99 Correct 670 ms 63568 KB Output is correct
100 Correct 616 ms 60352 KB Output is correct
101 Correct 600 ms 60356 KB Output is correct
102 Correct 567 ms 59712 KB Output is correct
103 Correct 579 ms 59784 KB Output is correct
104 Correct 476 ms 59852 KB Output is correct
105 Correct 461 ms 59800 KB Output is correct
106 Correct 282 ms 58184 KB Output is correct
107 Correct 293 ms 58172 KB Output is correct