Submission #714525

# Submission time Handle Problem Language Result Execution time Memory
714525 2023-03-24T22:22:43 Z MohamedFaresNebili Alternating Current (BOI18_alternating) C++14
100 / 100
273 ms 12708 KB
#include <bits/stdc++.h>

        using namespace std;

        int N, M, X[100005], Y[100005];
        int R[100005], ST[400005], lazy[400005];
        vector<int> adj[100005];

        void prop(int v, int l, int r) {
            if(l == r || !lazy[v]) return;
            lazy[v * 2] += lazy[v];
            lazy[v * 2 + 1] += lazy[v];
            ST[v * 2] += lazy[v];
            ST[v * 2 + 1] += lazy[v];
            lazy[v] = 0;
            return;
        }
        void update(int v, int l, int r, int lo, int hi, int val) {
            prop(v, l, r);
            if(l > hi || r < lo) return;
            if(l >= lo && r <= hi) {
                ST[v] += val; lazy[v] += val;
                prop(v, l, r); return;
            }
            update(v * 2, l, (l + r) / 2, lo, hi, val);
            update(v * 2 + 1, (l + r) / 2 + 1, r, lo, hi, val);
            ST[v] = min(ST[v * 2], ST[v * 2 + 1]);
        }
        int query(int v, int l, int r, int lo, int hi) {
            prop(v, l, r);
            if(l > hi || r < lo) return 1000000007;
            if(l >= lo && r <= hi) return ST[v];
            return min(query(v * 2, l, (l + r) / 2, lo, hi),
                query(v * 2 + 1, (l + r) / 2 + 1, r, lo, hi));
        }
        bool dfs(int v) {
            for(auto u : adj[v]) {
                if(R[u] == 0) {
                    R[u] = -R[v];
                    if(dfs(u)) return true;
                }
                else if(R[u] == R[v]) return true;
            }
            return false;
        }

        int32_t main()
        {
            ios_base::sync_with_stdio(false);
            cin.tie(0); cout.tie(0);
            cin >> N >> M; int K = 0; set<int> S;
            vector<pair<int, int>> P;
            for(int l = 0; l < M; l++) {
                cin >> X[l] >> Y[l]; X[l]--, Y[l]--;
                if(X[l] > Y[l]) S.insert(l);
                P.push_back({X[l], -l - 1});
                P.push_back({Y[l], l});
            }
            sort(P.begin(), P.end());
            for(int l = 0; l < N; l++) {
                while(K < P.size() && P[K] < make_pair(l, 0)) {
                    if(P[K].second < 0) S.insert(-P[K].second - 1);
                    else S.erase(P[K].second);
                    K++;
                }
                if(S.size() < 2) { cout << "impossible\n"; return 0; }
                if(S.size() == 2) {
                    int U = *S.begin();
                    int V = *next(S.begin());
                    adj[U].push_back(V);
                    adj[V].push_back(U);
                }
            }
            for(int l = 0; l < M; l++) {
                if(R[l] == 0 && adj[l].size() > 0) {
                    R[l] = -1;
                    if(dfs(l)) { cout << "impossible\n"; return 0; }
                }
            }
            for(int l = 0; l < M; l++) {
                if(R[l] >= 0) {
                    if(X[l] <= Y[l])
                        update(1, 0, N - 1, X[l], Y[l], 1);
                    else update(1, 0, N - 1, 0, Y[l], 1),
                        update(1, 0, N - 1, X[l], N - 1, 1);
                }
            }
            for(int l = 0; l < M; l++) {
                if(!R[l]) {
                    int T = 1000000007;
                    if(X[l] <= Y[l])
                        T = query(1, 0, N - 1, X[l], Y[l]);
                    else T = min(query(1, 0, N - 1, 0, Y[l]),
                            query(1, 0, N - 1, X[l], N - 1));
                    if(T <= 1) R[l] = 1;
                    else {
                        R[l] = -1;
                        if(X[l] <= Y[l])
                            update(1, 0, N - 1, X[l], Y[l], -1);
                        else update(1, 0, N - 1, 0, Y[l], -1),
                            update(1, 0, N - 1, X[l], N - 1, -1);
                    }
                }
            }
            for(int l = 0; l < M; l++)
                cout << (R[l] == -1 ? 0 : 1);
        }



Compilation message

alternating.cpp: In function 'int32_t main()':
alternating.cpp:61:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |                 while(K < P.size() && P[K] < make_pair(l, 0)) {
      |                       ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2684 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2684 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2680 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2680 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 3 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 1 ms 2680 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2680 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 3 ms 2680 KB Output is correct
28 Correct 2 ms 2676 KB Output is correct
29 Correct 2 ms 2676 KB Output is correct
30 Correct 2 ms 2680 KB Output is correct
31 Correct 2 ms 2680 KB Output is correct
32 Correct 2 ms 2684 KB Output is correct
33 Correct 2 ms 2680 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2680 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 1 ms 2676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2684 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2684 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2680 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2680 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 3 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 1 ms 2680 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2680 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 3 ms 2680 KB Output is correct
28 Correct 2 ms 2676 KB Output is correct
29 Correct 2 ms 2676 KB Output is correct
30 Correct 2 ms 2680 KB Output is correct
31 Correct 2 ms 2680 KB Output is correct
32 Correct 2 ms 2684 KB Output is correct
33 Correct 2 ms 2680 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2680 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 1 ms 2676 KB Output is correct
41 Correct 2 ms 2620 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 2 ms 2688 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 1 ms 2644 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2644 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 1 ms 2644 KB Output is correct
52 Correct 2 ms 2680 KB Output is correct
53 Correct 1 ms 2644 KB Output is correct
54 Correct 2 ms 2644 KB Output is correct
55 Correct 2 ms 2680 KB Output is correct
56 Correct 2 ms 2644 KB Output is correct
57 Correct 2 ms 2644 KB Output is correct
58 Correct 2 ms 2644 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 2 ms 2644 KB Output is correct
61 Correct 2 ms 2644 KB Output is correct
62 Correct 2 ms 2644 KB Output is correct
63 Correct 2 ms 2644 KB Output is correct
64 Correct 2 ms 2644 KB Output is correct
65 Correct 2 ms 2644 KB Output is correct
66 Correct 2 ms 2644 KB Output is correct
67 Correct 2 ms 2676 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2684 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2684 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2680 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2680 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 3 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 1 ms 2680 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2680 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 3 ms 2680 KB Output is correct
28 Correct 2 ms 2676 KB Output is correct
29 Correct 2 ms 2676 KB Output is correct
30 Correct 2 ms 2680 KB Output is correct
31 Correct 2 ms 2680 KB Output is correct
32 Correct 2 ms 2684 KB Output is correct
33 Correct 2 ms 2680 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2680 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 1 ms 2676 KB Output is correct
41 Correct 2 ms 2620 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 2 ms 2688 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 1 ms 2644 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2644 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 1 ms 2644 KB Output is correct
52 Correct 2 ms 2680 KB Output is correct
53 Correct 1 ms 2644 KB Output is correct
54 Correct 2 ms 2644 KB Output is correct
55 Correct 2 ms 2680 KB Output is correct
56 Correct 2 ms 2644 KB Output is correct
57 Correct 2 ms 2644 KB Output is correct
58 Correct 2 ms 2644 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 2 ms 2644 KB Output is correct
61 Correct 2 ms 2644 KB Output is correct
62 Correct 2 ms 2644 KB Output is correct
63 Correct 2 ms 2644 KB Output is correct
64 Correct 2 ms 2644 KB Output is correct
65 Correct 2 ms 2644 KB Output is correct
66 Correct 2 ms 2644 KB Output is correct
67 Correct 2 ms 2676 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 2 ms 2644 KB Output is correct
70 Correct 2 ms 2680 KB Output is correct
71 Correct 2 ms 2812 KB Output is correct
72 Correct 3 ms 2644 KB Output is correct
73 Correct 2 ms 2644 KB Output is correct
74 Correct 2 ms 2676 KB Output is correct
75 Correct 1 ms 2644 KB Output is correct
76 Correct 2 ms 2644 KB Output is correct
77 Correct 2 ms 2680 KB Output is correct
78 Correct 2 ms 2680 KB Output is correct
79 Correct 1 ms 2644 KB Output is correct
80 Correct 2 ms 2644 KB Output is correct
81 Correct 2 ms 2672 KB Output is correct
82 Correct 2 ms 2644 KB Output is correct
83 Correct 3 ms 2772 KB Output is correct
84 Correct 2 ms 2772 KB Output is correct
85 Correct 2 ms 2644 KB Output is correct
86 Correct 2 ms 2676 KB Output is correct
87 Correct 2 ms 2680 KB Output is correct
88 Correct 1 ms 2644 KB Output is correct
89 Correct 2 ms 2680 KB Output is correct
90 Correct 3 ms 2680 KB Output is correct
91 Correct 2 ms 2644 KB Output is correct
92 Correct 3 ms 2644 KB Output is correct
93 Correct 1 ms 2644 KB Output is correct
94 Correct 2 ms 2644 KB Output is correct
95 Correct 2 ms 2644 KB Output is correct
96 Correct 3 ms 2644 KB Output is correct
97 Correct 4 ms 2772 KB Output is correct
98 Correct 2 ms 2684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 11068 KB Output is correct
2 Correct 3 ms 3664 KB Output is correct
3 Correct 15 ms 4692 KB Output is correct
4 Correct 91 ms 9024 KB Output is correct
5 Correct 71 ms 11532 KB Output is correct
6 Correct 37 ms 6352 KB Output is correct
7 Correct 61 ms 11388 KB Output is correct
8 Correct 7 ms 5588 KB Output is correct
9 Correct 5 ms 5376 KB Output is correct
10 Correct 75 ms 11836 KB Output is correct
11 Correct 56 ms 10308 KB Output is correct
12 Correct 51 ms 9564 KB Output is correct
13 Correct 3 ms 3708 KB Output is correct
14 Correct 3 ms 3572 KB Output is correct
15 Correct 109 ms 8796 KB Output is correct
16 Correct 23 ms 6812 KB Output is correct
17 Correct 233 ms 11072 KB Output is correct
18 Correct 182 ms 8952 KB Output is correct
19 Correct 4 ms 2944 KB Output is correct
20 Correct 58 ms 10664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2684 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2684 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 2 ms 2680 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2680 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2676 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 3 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 1 ms 2680 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2680 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 3 ms 2680 KB Output is correct
28 Correct 2 ms 2676 KB Output is correct
29 Correct 2 ms 2676 KB Output is correct
30 Correct 2 ms 2680 KB Output is correct
31 Correct 2 ms 2680 KB Output is correct
32 Correct 2 ms 2684 KB Output is correct
33 Correct 2 ms 2680 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2680 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 1 ms 2676 KB Output is correct
41 Correct 2 ms 2620 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 2 ms 2688 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 1 ms 2644 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2644 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 1 ms 2644 KB Output is correct
52 Correct 2 ms 2680 KB Output is correct
53 Correct 1 ms 2644 KB Output is correct
54 Correct 2 ms 2644 KB Output is correct
55 Correct 2 ms 2680 KB Output is correct
56 Correct 2 ms 2644 KB Output is correct
57 Correct 2 ms 2644 KB Output is correct
58 Correct 2 ms 2644 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 2 ms 2644 KB Output is correct
61 Correct 2 ms 2644 KB Output is correct
62 Correct 2 ms 2644 KB Output is correct
63 Correct 2 ms 2644 KB Output is correct
64 Correct 2 ms 2644 KB Output is correct
65 Correct 2 ms 2644 KB Output is correct
66 Correct 2 ms 2644 KB Output is correct
67 Correct 2 ms 2676 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 2 ms 2644 KB Output is correct
70 Correct 2 ms 2680 KB Output is correct
71 Correct 2 ms 2812 KB Output is correct
72 Correct 3 ms 2644 KB Output is correct
73 Correct 2 ms 2644 KB Output is correct
74 Correct 2 ms 2676 KB Output is correct
75 Correct 1 ms 2644 KB Output is correct
76 Correct 2 ms 2644 KB Output is correct
77 Correct 2 ms 2680 KB Output is correct
78 Correct 2 ms 2680 KB Output is correct
79 Correct 1 ms 2644 KB Output is correct
80 Correct 2 ms 2644 KB Output is correct
81 Correct 2 ms 2672 KB Output is correct
82 Correct 2 ms 2644 KB Output is correct
83 Correct 3 ms 2772 KB Output is correct
84 Correct 2 ms 2772 KB Output is correct
85 Correct 2 ms 2644 KB Output is correct
86 Correct 2 ms 2676 KB Output is correct
87 Correct 2 ms 2680 KB Output is correct
88 Correct 1 ms 2644 KB Output is correct
89 Correct 2 ms 2680 KB Output is correct
90 Correct 3 ms 2680 KB Output is correct
91 Correct 2 ms 2644 KB Output is correct
92 Correct 3 ms 2644 KB Output is correct
93 Correct 1 ms 2644 KB Output is correct
94 Correct 2 ms 2644 KB Output is correct
95 Correct 2 ms 2644 KB Output is correct
96 Correct 3 ms 2644 KB Output is correct
97 Correct 4 ms 2772 KB Output is correct
98 Correct 2 ms 2684 KB Output is correct
99 Correct 47 ms 11068 KB Output is correct
100 Correct 3 ms 3664 KB Output is correct
101 Correct 15 ms 4692 KB Output is correct
102 Correct 91 ms 9024 KB Output is correct
103 Correct 71 ms 11532 KB Output is correct
104 Correct 37 ms 6352 KB Output is correct
105 Correct 61 ms 11388 KB Output is correct
106 Correct 7 ms 5588 KB Output is correct
107 Correct 5 ms 5376 KB Output is correct
108 Correct 75 ms 11836 KB Output is correct
109 Correct 56 ms 10308 KB Output is correct
110 Correct 51 ms 9564 KB Output is correct
111 Correct 3 ms 3708 KB Output is correct
112 Correct 3 ms 3572 KB Output is correct
113 Correct 109 ms 8796 KB Output is correct
114 Correct 23 ms 6812 KB Output is correct
115 Correct 233 ms 11072 KB Output is correct
116 Correct 182 ms 8952 KB Output is correct
117 Correct 4 ms 2944 KB Output is correct
118 Correct 58 ms 10664 KB Output is correct
119 Correct 4 ms 4564 KB Output is correct
120 Correct 61 ms 12708 KB Output is correct
121 Correct 39 ms 8760 KB Output is correct
122 Correct 2 ms 2664 KB Output is correct
123 Correct 4 ms 3028 KB Output is correct
124 Correct 21 ms 4504 KB Output is correct
125 Correct 28 ms 7044 KB Output is correct
126 Correct 4 ms 5076 KB Output is correct
127 Correct 15 ms 5696 KB Output is correct
128 Correct 64 ms 10436 KB Output is correct
129 Correct 34 ms 8596 KB Output is correct
130 Correct 48 ms 9188 KB Output is correct
131 Correct 4 ms 4596 KB Output is correct
132 Correct 4 ms 4856 KB Output is correct
133 Correct 273 ms 11124 KB Output is correct
134 Correct 66 ms 6408 KB Output is correct
135 Correct 2 ms 3284 KB Output is correct
136 Correct 21 ms 4816 KB Output is correct
137 Correct 37 ms 6288 KB Output is correct
138 Correct 5 ms 4692 KB Output is correct