Submission #719754

# Submission time Handle Problem Language Result Execution time Memory
719754 2023-04-06T15:37:11 Z keta_tsimakuridze Alternating Current (BOI18_alternating) C++17
100 / 100
193 ms 9776 KB
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#define f first
#define s second
#define ll long long
#define pii pair<int,int>
#define Pii pair<pair<int, int>, int>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7;
int n, from[N], last2[N], last3[N];
set<Pii> S;
struct seg{
    int a, b, id;
} p[N];

int try_(seg s) {
    queue<int> q;
    q.push(s.id);
    from[s.id] = 0;
    while(q.size()) {
        int u = q.front();
        q.pop();
        if(p[u].b == n) return u;
        if(p[u].b > n && (s.a > p[u].b - n || last3[p[u].b - n] < s.a) && last2[p[u].b - n] == 0) return u;
        if (p[u].b >= n) continue;
        while (S.upper_bound({{p[u].b + 2, 0}, 0}) != S.begin()) {
            Pii x = *--S.upper_bound({{p[u].b + 2, 0}, 0});
            if (last2[p[u].b] < x.f.f) q.push(x.s), from[x.s] = u, S.erase(x);
            else break;
        }
    }
    return -1;
}
bool cmp(seg a, seg b) {
    return (a.a < b.a);
}
int len(int a, int b) {
    return (b >= a ? b - a + 1 : n - a + 1 + b);
}
int get(int x, int a) {
    if(x >= a) return x - a + 1;

    return n - a + 1 + x;
}
signed main() {
    int m;
    cin >> n >> m;
    int mx = 0;
    for(int i = 1; i <= m; i++) {
        cin >> p[i].a >> p[i].b;
        p[i].id = i;
        if(!mx || len(p[i].a, p[i].b) > len(p[mx].a, p[mx].b)) mx = i;
    }
    int st = p[mx].a;
    vector<int> c(n + 5);
    for(int i = 1; i <= m; i++) {
        p[i].a = get(p[i].a, st);
        p[i].b = get(p[i].b, st);
        ++c[p[i].a], --c[p[i].b + 1];
        if(p[i].a > p[i].b) ++c[1], p[i].b = p[i].b + n;
    }

    for(int i = 1; i <= n; i++) {
        c[i] += c[i - 1];
        last3[i] = last3[i - 1];
        last2[i] = last2[i - 1];
        if(c[i] == 1) {
            cout << "impossible";
            return 0;
        }
        if(c[i] == 2) last2[i] = i;
        if(c[i] <= 3) last3[i] = i;
    }
    vector<seg> x;
    for(int i = 1; i <= m; i++) {
        if(i == mx) continue;
        if(p[i].a <= p[mx].b + 1) {
            if(last2[p[mx].b] < p[i].a) x.push_back(p[i]);
        }
        else S.insert({{p[i].a, p[i].b}, i});
    }
    sort(x.begin(), x.end(), cmp);
    int ST = (p[mx].b == n ? mx : -1);
    while(x.size()) {

        seg s = x.back(); x.pop_back();

        ST = try_(s);
        if(ST != -1) {
            ST = s.id;
            break;
        }
    }
    if(ST == -1) {
        cout << "impossible";
        return 0;
    }
    S.clear();
    for(int i = 1; i <= m; i++) {
        if(i == mx) continue;
        if(p[i].a <= p[mx].b + 1) {}
        else S.insert({{p[i].a, p[i].b}, i});
    }
    vector<int> ans(m + 5); ans[mx] = 1;
    int EN = try_(p[ST]);
  	if(EN == -1) {
      while(true) EN = -1;
    }
    while(EN) {
        ans[EN] = 1;
        EN = from[EN];
    }
    for(int i = 1; i <= m; i++) cout << ans[i] << "";
}
/*
 10 5
 1 5
 6 7
 5 1
 7 2
 2 4
 */
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 312 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 320 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 316 KB Output is correct
52 Correct 1 ms 312 KB Output is correct
53 Correct 1 ms 312 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 212 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 2 ms 316 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 1 ms 316 KB Output is correct
63 Correct 1 ms 312 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 0 ms 316 KB Output is correct
66 Correct 1 ms 312 KB Output is correct
67 Correct 1 ms 340 KB Output is correct
68 Correct 1 ms 312 KB Output is correct
69 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 312 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 320 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 316 KB Output is correct
52 Correct 1 ms 312 KB Output is correct
53 Correct 1 ms 312 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 212 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 2 ms 316 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 1 ms 316 KB Output is correct
63 Correct 1 ms 312 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 0 ms 316 KB Output is correct
66 Correct 1 ms 312 KB Output is correct
67 Correct 1 ms 340 KB Output is correct
68 Correct 1 ms 312 KB Output is correct
69 Correct 1 ms 340 KB Output is correct
70 Correct 2 ms 316 KB Output is correct
71 Correct 1 ms 320 KB Output is correct
72 Correct 1 ms 320 KB Output is correct
73 Correct 1 ms 312 KB Output is correct
74 Correct 1 ms 340 KB Output is correct
75 Correct 1 ms 212 KB Output is correct
76 Correct 1 ms 212 KB Output is correct
77 Correct 1 ms 312 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 1 ms 212 KB Output is correct
81 Correct 1 ms 340 KB Output is correct
82 Correct 1 ms 340 KB Output is correct
83 Correct 1 ms 340 KB Output is correct
84 Correct 3 ms 340 KB Output is correct
85 Correct 1 ms 340 KB Output is correct
86 Correct 0 ms 340 KB Output is correct
87 Correct 1 ms 340 KB Output is correct
88 Correct 1 ms 212 KB Output is correct
89 Correct 1 ms 340 KB Output is correct
90 Correct 1 ms 340 KB Output is correct
91 Correct 1 ms 340 KB Output is correct
92 Correct 1 ms 340 KB Output is correct
93 Correct 1 ms 212 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 1 ms 340 KB Output is correct
96 Correct 2 ms 340 KB Output is correct
97 Correct 2 ms 340 KB Output is correct
98 Correct 2 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 4260 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 29 ms 1260 KB Output is correct
4 Correct 30 ms 2344 KB Output is correct
5 Correct 176 ms 9336 KB Output is correct
6 Correct 59 ms 2592 KB Output is correct
7 Correct 186 ms 8816 KB Output is correct
8 Correct 5 ms 1620 KB Output is correct
9 Correct 2 ms 1492 KB Output is correct
10 Correct 193 ms 9776 KB Output is correct
11 Correct 137 ms 7628 KB Output is correct
12 Correct 57 ms 2992 KB Output is correct
13 Correct 1 ms 1492 KB Output is correct
14 Correct 1 ms 1492 KB Output is correct
15 Correct 70 ms 3156 KB Output is correct
16 Correct 27 ms 2036 KB Output is correct
17 Correct 81 ms 4848 KB Output is correct
18 Correct 74 ms 3376 KB Output is correct
19 Correct 6 ms 1480 KB Output is correct
20 Correct 134 ms 9216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 312 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 320 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 316 KB Output is correct
52 Correct 1 ms 312 KB Output is correct
53 Correct 1 ms 312 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 212 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 2 ms 316 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 1 ms 316 KB Output is correct
63 Correct 1 ms 312 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 0 ms 316 KB Output is correct
66 Correct 1 ms 312 KB Output is correct
67 Correct 1 ms 340 KB Output is correct
68 Correct 1 ms 312 KB Output is correct
69 Correct 1 ms 340 KB Output is correct
70 Correct 2 ms 316 KB Output is correct
71 Correct 1 ms 320 KB Output is correct
72 Correct 1 ms 320 KB Output is correct
73 Correct 1 ms 312 KB Output is correct
74 Correct 1 ms 340 KB Output is correct
75 Correct 1 ms 212 KB Output is correct
76 Correct 1 ms 212 KB Output is correct
77 Correct 1 ms 312 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 1 ms 212 KB Output is correct
81 Correct 1 ms 340 KB Output is correct
82 Correct 1 ms 340 KB Output is correct
83 Correct 1 ms 340 KB Output is correct
84 Correct 3 ms 340 KB Output is correct
85 Correct 1 ms 340 KB Output is correct
86 Correct 0 ms 340 KB Output is correct
87 Correct 1 ms 340 KB Output is correct
88 Correct 1 ms 212 KB Output is correct
89 Correct 1 ms 340 KB Output is correct
90 Correct 1 ms 340 KB Output is correct
91 Correct 1 ms 340 KB Output is correct
92 Correct 1 ms 340 KB Output is correct
93 Correct 1 ms 212 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 1 ms 340 KB Output is correct
96 Correct 2 ms 340 KB Output is correct
97 Correct 2 ms 340 KB Output is correct
98 Correct 2 ms 320 KB Output is correct
99 Correct 57 ms 4260 KB Output is correct
100 Correct 1 ms 1492 KB Output is correct
101 Correct 29 ms 1260 KB Output is correct
102 Correct 30 ms 2344 KB Output is correct
103 Correct 176 ms 9336 KB Output is correct
104 Correct 59 ms 2592 KB Output is correct
105 Correct 186 ms 8816 KB Output is correct
106 Correct 5 ms 1620 KB Output is correct
107 Correct 2 ms 1492 KB Output is correct
108 Correct 193 ms 9776 KB Output is correct
109 Correct 137 ms 7628 KB Output is correct
110 Correct 57 ms 2992 KB Output is correct
111 Correct 1 ms 1492 KB Output is correct
112 Correct 1 ms 1492 KB Output is correct
113 Correct 70 ms 3156 KB Output is correct
114 Correct 27 ms 2036 KB Output is correct
115 Correct 81 ms 4848 KB Output is correct
116 Correct 74 ms 3376 KB Output is correct
117 Correct 6 ms 1480 KB Output is correct
118 Correct 134 ms 9216 KB Output is correct
119 Correct 2 ms 1472 KB Output is correct
120 Correct 150 ms 8200 KB Output is correct
121 Correct 85 ms 5732 KB Output is correct
122 Correct 1 ms 256 KB Output is correct
123 Correct 6 ms 1492 KB Output is correct
124 Correct 21 ms 1716 KB Output is correct
125 Correct 44 ms 4124 KB Output is correct
126 Correct 1 ms 1492 KB Output is correct
127 Correct 20 ms 2260 KB Output is correct
128 Correct 179 ms 8780 KB Output is correct
129 Correct 105 ms 5864 KB Output is correct
130 Correct 115 ms 6856 KB Output is correct
131 Correct 2 ms 1492 KB Output is correct
132 Correct 2 ms 1492 KB Output is correct
133 Correct 79 ms 5440 KB Output is correct
134 Correct 25 ms 2588 KB Output is correct
135 Correct 1 ms 1492 KB Output is correct
136 Correct 35 ms 2124 KB Output is correct
137 Correct 47 ms 2736 KB Output is correct
138 Correct 3 ms 1492 KB Output is correct