답안 #722084

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
722084 2023-04-11T11:51:00 Z SlavicG Alternating Current (BOI18_alternating) C++17
100 / 100
264 ms 12724 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ll long long
 
#define          all(v)              v.begin(), v.end()
#define         rall(v)              v.rbegin(),v.rend()
 
#define            pb                push_back
#define          sz(a)               (int)a.size()

const int N = 1e5 + 10;
int t[4 * N], lazy[4 * N], n, m, ans[N], a[N], b[N];
vector<int> adj[N];
void push(int i, int l, int r) {
    if(!lazy[i]) return;
    t[i] += lazy[i];
    if(l != r) {
        lazy[2 * i] += lazy[i];
        lazy[2 * i + 1] += lazy[i];
    }
    lazy[i] = 0;
}
void modif(int i, int l, int r, int tl, int tr, int val) {
    push(i, l, r);
    if(l > tr || r < tl) return;
    if(l >= tl && r <= tr) {
        lazy[i] += val;
        push(i, l, r);
        return;
    }
    int mid = l + r >> 1;
    modif(2 * i, l, mid, tl, tr, val);
    modif(2 * i + 1, mid + 1, r, tl, tr, val);
    t[i] = min(t[2 * i], t[2 * i + 1]);
}
int query(int i, int l, int r, int tl, int tr) {
    push(i, l, r);
    if(l > tr || r < tl) return 1e9;
    if(l >= tl && r <= tr) return t[i];
    int mid = l + r >> 1;
    return min(query(2 * i, l, mid, tl, tr), query(2 * i + 1, mid + 1, r, tl, tr));
}
void modif(int l, int r, int val) {
    if(l <= r) modif(1, 1, n, l, r, val);
    else {
        modif(1, 1, n, l, n, val);
        modif(1, 1, n, 1, r, val);
    }
}
int query(int l, int r) {
    if(l <= r) return query(1, 1, n, l, r);
    else {
        return min(query(1, 1, n, l, n), query(1, 1, n, 1, r));
    }
}
bool dfs(int u) {
    for(int v: adj[u]) {
        if(ans[v] == 0) {
            ans[v] = -ans[u];
            if(!dfs(v)) return false;
        } else if(ans[v] == ans[u]) return false;
    }
    return true;
}
void solve() {
    cin >> n >> m;
    set<int> ongoing;
    vector<pair<int, int>> events;
    for(int i = 1; i <= m; ++i) {
        cin >> a[i] >> b[i];
        if(a[i] > b[i]) ongoing.insert(i);
        events.pb({a[i], i});
        events.pb({b[i] + 1, -i});
    }
    sort(all(events));
    for(int i = 1, j = 0; i <= n; ++i) {
        while(j < sz(events) && events[j].first == i) {
            if(events[j].second < 0) ongoing.erase(-events[j].second);
            else ongoing.insert(events[j].second);
            ++j;
        }
        if(sz(ongoing) < 2) {
            cout << "impossible\n";
            return;
        }
        if(sz(ongoing) == 2) {
            int A = *ongoing.begin(), B = *++ongoing.begin();
            adj[A].pb(B);
            adj[B].pb(A);
        }
    }
    for(int i = 1; i <= m; ++i) {
        if(ans[i] == 0 && sz(adj[i])) {
            ans[i] = -1;
            if(!dfs(i)) {
                cout << "impossible\n";
                return;
            }
        }
    }
    for(int i = 1; i <= m; ++i) {
        if(ans[i] >= 0) modif(a[i], b[i], 1);
    }
    for(int i = 1; i <= m; ++i) {
        if(ans[i] == 0) {
            if(query(a[i], b[i]) >= 2) {
                ans[i] = -1;
                modif(a[i], b[i], -1);
            } else {
                ans[i] = 1;
            }
        }
    }
    for(int i = 1; i <= m; ++i) {
        cout << (ans[i] == -1 ? 0 : 1);
    }
}   
 
int32_t main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t = 1;
    //cin >> t;
    while(t--) {
        solve();
    }
}

Compilation message

alternating.cpp: In function 'void modif(int, int, int, int, int, int)':
alternating.cpp:32:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   32 |     int mid = l + r >> 1;
      |               ~~^~~
alternating.cpp: In function 'int query(int, int, int, int, int)':
alternating.cpp:41:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   41 |     int mid = l + r >> 1;
      |               ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Correct 2 ms 2680 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2684 KB Output is correct
11 Correct 2 ms 2688 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 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 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2672 KB Output is correct
20 Correct 2 ms 2680 KB Output is correct
21 Correct 2 ms 2684 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2684 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2680 KB Output is correct
26 Correct 2 ms 2688 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2680 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 2 ms 2676 KB Output is correct
34 Correct 2 ms 2684 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 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Correct 2 ms 2680 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2684 KB Output is correct
11 Correct 2 ms 2688 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 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 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2672 KB Output is correct
20 Correct 2 ms 2680 KB Output is correct
21 Correct 2 ms 2684 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2684 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2680 KB Output is correct
26 Correct 2 ms 2688 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2680 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 2 ms 2676 KB Output is correct
34 Correct 2 ms 2684 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 2 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2684 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2684 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 2 ms 2644 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2680 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 2 ms 2644 KB Output is correct
53 Correct 2 ms 2680 KB Output is correct
54 Correct 2 ms 2680 KB Output is correct
55 Correct 2 ms 2644 KB Output is correct
56 Correct 2 ms 2644 KB Output is correct
57 Correct 3 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 4 ms 2644 KB Output is correct
61 Correct 4 ms 2688 KB Output is correct
62 Correct 2 ms 2680 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 2644 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 2 ms 2680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Correct 2 ms 2680 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2684 KB Output is correct
11 Correct 2 ms 2688 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 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 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2672 KB Output is correct
20 Correct 2 ms 2680 KB Output is correct
21 Correct 2 ms 2684 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2684 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2680 KB Output is correct
26 Correct 2 ms 2688 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2680 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 2 ms 2676 KB Output is correct
34 Correct 2 ms 2684 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 2 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2684 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2684 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 2 ms 2644 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2680 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 2 ms 2644 KB Output is correct
53 Correct 2 ms 2680 KB Output is correct
54 Correct 2 ms 2680 KB Output is correct
55 Correct 2 ms 2644 KB Output is correct
56 Correct 2 ms 2644 KB Output is correct
57 Correct 3 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 4 ms 2644 KB Output is correct
61 Correct 4 ms 2688 KB Output is correct
62 Correct 2 ms 2680 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 2644 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 2 ms 2680 KB Output is correct
70 Correct 2 ms 2684 KB Output is correct
71 Correct 2 ms 2772 KB Output is correct
72 Correct 2 ms 2644 KB Output is correct
73 Correct 2 ms 2644 KB Output is correct
74 Correct 2 ms 2644 KB Output is correct
75 Correct 2 ms 2644 KB Output is correct
76 Correct 2 ms 2644 KB Output is correct
77 Correct 2 ms 2644 KB Output is correct
78 Correct 2 ms 2644 KB Output is correct
79 Correct 2 ms 2644 KB Output is correct
80 Correct 2 ms 2692 KB Output is correct
81 Correct 2 ms 2644 KB Output is correct
82 Correct 2 ms 2644 KB Output is correct
83 Correct 2 ms 2692 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 2644 KB Output is correct
87 Correct 3 ms 2772 KB Output is correct
88 Correct 2 ms 2644 KB Output is correct
89 Correct 2 ms 2644 KB Output is correct
90 Correct 4 ms 2772 KB Output is correct
91 Correct 2 ms 2684 KB Output is correct
92 Correct 2 ms 2644 KB Output is correct
93 Correct 2 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 2772 KB Output is correct
97 Correct 3 ms 2692 KB Output is correct
98 Correct 2 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 10204 KB Output is correct
2 Correct 4 ms 3664 KB Output is correct
3 Correct 15 ms 4620 KB Output is correct
4 Correct 91 ms 9072 KB Output is correct
5 Correct 72 ms 11556 KB Output is correct
6 Correct 38 ms 6352 KB Output is correct
7 Correct 60 ms 11324 KB Output is correct
8 Correct 7 ms 5572 KB Output is correct
9 Correct 6 ms 5332 KB Output is correct
10 Correct 78 ms 11816 KB Output is correct
11 Correct 58 ms 10308 KB Output is correct
12 Correct 52 ms 9364 KB Output is correct
13 Correct 4 ms 3708 KB Output is correct
14 Correct 3 ms 3576 KB Output is correct
15 Correct 112 ms 8752 KB Output is correct
16 Correct 24 ms 6696 KB Output is correct
17 Correct 255 ms 11180 KB Output is correct
18 Correct 183 ms 8888 KB Output is correct
19 Correct 4 ms 3028 KB Output is correct
20 Correct 62 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2684 KB Output is correct
5 Correct 2 ms 2680 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 3 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2684 KB Output is correct
11 Correct 2 ms 2688 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 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 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2672 KB Output is correct
20 Correct 2 ms 2680 KB Output is correct
21 Correct 2 ms 2684 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2684 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2680 KB Output is correct
26 Correct 2 ms 2688 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2680 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 2 ms 2676 KB Output is correct
34 Correct 2 ms 2684 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 2 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2684 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2684 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 2 ms 2644 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2680 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 2 ms 2644 KB Output is correct
53 Correct 2 ms 2680 KB Output is correct
54 Correct 2 ms 2680 KB Output is correct
55 Correct 2 ms 2644 KB Output is correct
56 Correct 2 ms 2644 KB Output is correct
57 Correct 3 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 4 ms 2644 KB Output is correct
61 Correct 4 ms 2688 KB Output is correct
62 Correct 2 ms 2680 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 2644 KB Output is correct
68 Correct 2 ms 2644 KB Output is correct
69 Correct 2 ms 2680 KB Output is correct
70 Correct 2 ms 2684 KB Output is correct
71 Correct 2 ms 2772 KB Output is correct
72 Correct 2 ms 2644 KB Output is correct
73 Correct 2 ms 2644 KB Output is correct
74 Correct 2 ms 2644 KB Output is correct
75 Correct 2 ms 2644 KB Output is correct
76 Correct 2 ms 2644 KB Output is correct
77 Correct 2 ms 2644 KB Output is correct
78 Correct 2 ms 2644 KB Output is correct
79 Correct 2 ms 2644 KB Output is correct
80 Correct 2 ms 2692 KB Output is correct
81 Correct 2 ms 2644 KB Output is correct
82 Correct 2 ms 2644 KB Output is correct
83 Correct 2 ms 2692 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 2644 KB Output is correct
87 Correct 3 ms 2772 KB Output is correct
88 Correct 2 ms 2644 KB Output is correct
89 Correct 2 ms 2644 KB Output is correct
90 Correct 4 ms 2772 KB Output is correct
91 Correct 2 ms 2684 KB Output is correct
92 Correct 2 ms 2644 KB Output is correct
93 Correct 2 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 2772 KB Output is correct
97 Correct 3 ms 2692 KB Output is correct
98 Correct 2 ms 2772 KB Output is correct
99 Correct 52 ms 10204 KB Output is correct
100 Correct 4 ms 3664 KB Output is correct
101 Correct 15 ms 4620 KB Output is correct
102 Correct 91 ms 9072 KB Output is correct
103 Correct 72 ms 11556 KB Output is correct
104 Correct 38 ms 6352 KB Output is correct
105 Correct 60 ms 11324 KB Output is correct
106 Correct 7 ms 5572 KB Output is correct
107 Correct 6 ms 5332 KB Output is correct
108 Correct 78 ms 11816 KB Output is correct
109 Correct 58 ms 10308 KB Output is correct
110 Correct 52 ms 9364 KB Output is correct
111 Correct 4 ms 3708 KB Output is correct
112 Correct 3 ms 3576 KB Output is correct
113 Correct 112 ms 8752 KB Output is correct
114 Correct 24 ms 6696 KB Output is correct
115 Correct 255 ms 11180 KB Output is correct
116 Correct 183 ms 8888 KB Output is correct
117 Correct 4 ms 3028 KB Output is correct
118 Correct 62 ms 10588 KB Output is correct
119 Correct 5 ms 4436 KB Output is correct
120 Correct 58 ms 12724 KB Output is correct
121 Correct 41 ms 8776 KB Output is correct
122 Correct 2 ms 2772 KB Output is correct
123 Correct 4 ms 3028 KB Output is correct
124 Correct 17 ms 4484 KB Output is correct
125 Correct 28 ms 7052 KB Output is correct
126 Correct 5 ms 5076 KB Output is correct
127 Correct 13 ms 5720 KB Output is correct
128 Correct 63 ms 10476 KB Output is correct
129 Correct 35 ms 8564 KB Output is correct
130 Correct 47 ms 9156 KB Output is correct
131 Correct 4 ms 4600 KB Output is correct
132 Correct 5 ms 4676 KB Output is correct
133 Correct 264 ms 10988 KB Output is correct
134 Correct 68 ms 6484 KB Output is correct
135 Correct 3 ms 3192 KB Output is correct
136 Correct 29 ms 4828 KB Output is correct
137 Correct 37 ms 6276 KB Output is correct
138 Correct 4 ms 4692 KB Output is correct