Submission #672056

# Submission time Handle Problem Language Result Execution time Memory
672056 2022-12-14T16:36:01 Z LittleCube Alternating Current (BOI18_alternating) C++17
100 / 100
211 ms 12256 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;

int N, M, ans[100005], a[100005], b[100005];
vector<int> E[100005];

struct segTree
{
    int seg[400005], lazy[400005];

    int getval(int i)
    {
        return seg[i] + lazy[i];
    }

    void push(int i)
    {
        lazy[i << 1] += lazy[i];
        lazy[i << 1 | 1] += lazy[i];
        seg[i] += lazy[i];
        lazy[i] = 0;
    }

    void pull(int i)
    {
        seg[i] = min(getval(i << 1), getval(i << 1 | 1));
    }

    void modify(int mL, int mR, int val, int i = 1, int L = 1, int R = N)
    {
        if (mL <= L && R <= mR)
            lazy[i] += val;
        else if (R < mL || mR < L)
            return;
        else
        {
            int mid = (L + R) / 2;
            push(i);
            modify(mL, mR, val, i << 1, L, mid);
            modify(mL, mR, val, i << 1 | 1, mid + 1, R);
            pull(i);
        }
    }

    int query(int qL, int qR, int i = 1, int L = 1, int R = N)
    {
        if (qL <= L && R <= qR)
            return getval(i);
        else if (R < qL || qR < L)
            return 1e9;
        else
        {
            int mid = (L + R) / 2;
            push(i);
            return min(query(qL, qR, i << 1, L, mid), query(qL, qR, i << 1 | 1, mid + 1, R));
        }
    }
} seg;

bool dfs(int u)
{
    bool r = 1;
    for (int v : E[u])
        if (ans[v] == 0)
        {
            ans[v] = ans[u] ^ 3;
            r &= dfs(v);
        }
        else
            r &= (ans[u] ^ ans[v]) == 3;
    return r;
}

signed main()
{
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> N >> M;
    set<int> sweep;
    vector<pii> event;
    for (int i = 1; i <= M; i++)
    {
        cin >> a[i] >> b[i];
        if (b[i] < a[i])
        {
            sweep.insert(i);
            event.emplace_back(pii(b[i] + 1, -i));
            event.emplace_back(pii(a[i], i));
        }
        else
        {
            event.emplace_back(pii(a[i], i));
            event.emplace_back(pii(b[i] + 1, -i));
        }
    }
    sort(event.begin(), event.end());
    for (int i = 1, j = 0; i <= N; i++)
    {
        while (j < event.size() && event[j].F == i)
        {
            auto [t, k] = event[j];
            if (k > 0)
                sweep.insert(k);
            else
                sweep.erase(-k);
            j++;
        }
        if (sweep.size() <= 1)
        {
            cout << "impossible\n";
            return 0;
        }
        else if (sweep.size() == 2)
        {
            int u = *sweep.begin(), v = *next(sweep.begin());
            E[u].emplace_back(v);
            E[v].emplace_back(u);
        }
    }
    for (int i = 1; i <= M; i++)
        if (ans[i] == 0 && !E[i].empty())
        {
            ans[i] = 1;
            if (!dfs(i))
            {
                cout << "impossible\n";
                return 0;
            }
        }
    for (int i = 1; i <= M; i++)
    {
        if (ans[i] <= 1)
        {
            if (b[i] < a[i])
            {
                seg.modify(a[i], N, 1);
                seg.modify(1, b[i], 1);
            }
            else
                seg.modify(a[i], b[i], 1);
        }
    }
    for (int i = 1; i <= M; i++)
        if (ans[i] == 0)
        {
            int rem = 0;
            if (b[i] < a[i])
                rem = min(seg.query(a[i], N), seg.query(1, b[i]));
            else
                rem = seg.query(a[i], b[i]);

            assert(rem > 0);
            if (rem >= 2)
            {
                ans[i] = 2;
                if (b[i] < a[i])
                {
                    seg.modify(a[i], N, -1);
                    seg.modify(1, b[i], -1);
                }
                else
                    seg.modify(a[i], b[i], -1);
            }
            else
                ans[i] = 1;
        }
    for (int i = 1; i <= M; i++)
        cout << ans[i] - 1;
    cout << '\n';
}
/*
5 6
1 3
2 4
3 5
4 1
5 2
1 3
*/

Compilation message

alternating.cpp: In function 'int main()':
alternating.cpp:103:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |         while (j < event.size() && event[j].F == i)
      |                ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 1 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 1 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 1 ms 2772 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2676 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 1 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 1 ms 2644 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 2684 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2624 KB Output is correct
40 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 1 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 1 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 1 ms 2772 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2676 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 1 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 1 ms 2644 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 2684 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2624 KB Output is correct
40 Correct 2 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 1 ms 2644 KB Output is correct
43 Correct 2 ms 2680 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 2 ms 2688 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2688 KB Output is correct
50 Correct 2 ms 2684 KB Output is correct
51 Correct 2 ms 2676 KB Output is correct
52 Correct 2 ms 2644 KB Output is correct
53 Correct 2 ms 2644 KB Output is correct
54 Correct 2 ms 2644 KB Output is correct
55 Correct 2 ms 2684 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 2680 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 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 1 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 1 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 1 ms 2772 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2676 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 1 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 1 ms 2644 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 2684 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2624 KB Output is correct
40 Correct 2 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 1 ms 2644 KB Output is correct
43 Correct 2 ms 2680 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 2 ms 2688 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2688 KB Output is correct
50 Correct 2 ms 2684 KB Output is correct
51 Correct 2 ms 2676 KB Output is correct
52 Correct 2 ms 2644 KB Output is correct
53 Correct 2 ms 2644 KB Output is correct
54 Correct 2 ms 2644 KB Output is correct
55 Correct 2 ms 2684 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 2680 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 2644 KB Output is correct
70 Correct 2 ms 2696 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 2644 KB Output is correct
81 Correct 2 ms 2644 KB Output is correct
82 Correct 1 ms 2648 KB Output is correct
83 Correct 3 ms 2644 KB Output is correct
84 Correct 2 ms 2772 KB Output is correct
85 Correct 2 ms 2644 KB Output is correct
86 Correct 1 ms 2688 KB Output is correct
87 Correct 2 ms 2772 KB Output is correct
88 Correct 2 ms 2688 KB Output is correct
89 Correct 2 ms 2644 KB Output is correct
90 Correct 3 ms 2692 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 2644 KB Output is correct
97 Correct 3 ms 2688 KB Output is correct
98 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 10240 KB Output is correct
2 Correct 4 ms 3664 KB Output is correct
3 Correct 13 ms 4052 KB Output is correct
4 Correct 73 ms 7976 KB Output is correct
5 Correct 70 ms 9928 KB Output is correct
6 Correct 36 ms 5328 KB Output is correct
7 Correct 81 ms 9972 KB Output is correct
8 Correct 7 ms 5028 KB Output is correct
9 Correct 5 ms 4816 KB Output is correct
10 Correct 72 ms 10224 KB Output is correct
11 Correct 58 ms 8960 KB Output is correct
12 Correct 69 ms 9848 KB Output is correct
13 Correct 3 ms 3708 KB Output is correct
14 Correct 4 ms 3540 KB Output is correct
15 Correct 109 ms 8232 KB Output is correct
16 Correct 23 ms 6736 KB Output is correct
17 Correct 209 ms 10560 KB Output is correct
18 Correct 166 ms 8972 KB Output is correct
19 Correct 4 ms 2900 KB Output is correct
20 Correct 58 ms 10524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 1 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 1 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 1 ms 2772 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2676 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 1 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 1 ms 2644 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 2684 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 2 ms 2624 KB Output is correct
40 Correct 2 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 1 ms 2644 KB Output is correct
43 Correct 2 ms 2680 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 2 ms 2688 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2688 KB Output is correct
50 Correct 2 ms 2684 KB Output is correct
51 Correct 2 ms 2676 KB Output is correct
52 Correct 2 ms 2644 KB Output is correct
53 Correct 2 ms 2644 KB Output is correct
54 Correct 2 ms 2644 KB Output is correct
55 Correct 2 ms 2684 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 2680 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 2644 KB Output is correct
70 Correct 2 ms 2696 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 2644 KB Output is correct
81 Correct 2 ms 2644 KB Output is correct
82 Correct 1 ms 2648 KB Output is correct
83 Correct 3 ms 2644 KB Output is correct
84 Correct 2 ms 2772 KB Output is correct
85 Correct 2 ms 2644 KB Output is correct
86 Correct 1 ms 2688 KB Output is correct
87 Correct 2 ms 2772 KB Output is correct
88 Correct 2 ms 2688 KB Output is correct
89 Correct 2 ms 2644 KB Output is correct
90 Correct 3 ms 2692 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 2644 KB Output is correct
97 Correct 3 ms 2688 KB Output is correct
98 Correct 2 ms 2772 KB Output is correct
99 Correct 59 ms 10240 KB Output is correct
100 Correct 4 ms 3664 KB Output is correct
101 Correct 13 ms 4052 KB Output is correct
102 Correct 73 ms 7976 KB Output is correct
103 Correct 70 ms 9928 KB Output is correct
104 Correct 36 ms 5328 KB Output is correct
105 Correct 81 ms 9972 KB Output is correct
106 Correct 7 ms 5028 KB Output is correct
107 Correct 5 ms 4816 KB Output is correct
108 Correct 72 ms 10224 KB Output is correct
109 Correct 58 ms 8960 KB Output is correct
110 Correct 69 ms 9848 KB Output is correct
111 Correct 3 ms 3708 KB Output is correct
112 Correct 4 ms 3540 KB Output is correct
113 Correct 109 ms 8232 KB Output is correct
114 Correct 23 ms 6736 KB Output is correct
115 Correct 209 ms 10560 KB Output is correct
116 Correct 166 ms 8972 KB Output is correct
117 Correct 4 ms 2900 KB Output is correct
118 Correct 58 ms 10524 KB Output is correct
119 Correct 4 ms 4308 KB Output is correct
120 Correct 57 ms 12256 KB Output is correct
121 Correct 37 ms 8260 KB Output is correct
122 Correct 2 ms 2684 KB Output is correct
123 Correct 4 ms 3028 KB Output is correct
124 Correct 17 ms 4496 KB Output is correct
125 Correct 28 ms 6476 KB Output is correct
126 Correct 4 ms 4564 KB Output is correct
127 Correct 12 ms 5208 KB Output is correct
128 Correct 62 ms 9924 KB Output is correct
129 Correct 40 ms 8140 KB Output is correct
130 Correct 51 ms 8776 KB Output is correct
131 Correct 4 ms 4432 KB Output is correct
132 Correct 5 ms 4692 KB Output is correct
133 Correct 211 ms 10528 KB Output is correct
134 Correct 56 ms 5968 KB Output is correct
135 Correct 2 ms 3196 KB Output is correct
136 Correct 22 ms 4816 KB Output is correct
137 Correct 39 ms 6212 KB Output is correct
138 Correct 4 ms 4228 KB Output is correct