답안 #934587

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
934587 2024-02-27T16:14:37 Z VladaMG98 이상한 기계 (APIO19_strange_device) C++17
100 / 100
1420 ms 313656 KB
    // Problem: A - Strange Device
    // Contest: Virtual Judge - 2024-02-27 APIO Simulation
    // URL: https://vjudge.net/contest/612540#problem/A
    // Memory Limit: 512 MB
    // Time Limit: 5000 ms
    // 
    // Powered by CP Editor (https://cpeditor.org)
     
    #include <bits/stdc++.h>
    using namespace std;
     
    #define rep(i, a, b) for(int i = a; i < (b); ++i)
    #define all(x) begin(x), end(x)
    #define sz(x) (int)(x).size()
    #define print(A,t) cerr << #A << ": "; copy(all(A),ostream_iterator<t>(cerr," " )); cerr << endl
    #define prArr(A,n,t)  cerr << #A << ": "; copy(A,A + n,ostream_iterator<t>(cerr," " )); cerr << endl
    #define what_is(x) cerr << #x << " is " << x << endl
    long long typedef ll;
    typedef long double ld;
    typedef pair<int, int> pii;
    typedef vector<int> vi;
     
    int n; ll A, B;
    const int MAXN = 1000010;
    ll L[MAXN], R[MAXN];
     
    void subtask1() {
        set<pair<ll, ll>> ans;
        for (int i = 1; i <= n; i++) {
            for (ll j = L[i]; j <= R[i]; j++) {
                ll x = (j + j / B) % A;
                ll y = j % B;
                ans.insert({x, y});
            }
        }
        cout << sz(ans) << endl;
    }
     
    void subtask4() {
        bool halved = false;
        if (A % 2 == 0) {
            A /= 2;
            halved = true;
        }
        bool full = false;
        vector<pair<ll, int>> events;
        for (int i = 1; i <= n; i++) {
            ll l = L[i] % A, r = R[i] % A;
            if (R[i] - L[i] + 1 >= A) {
                full = true;
                break;
            }
            if (l <= r) {
                events.push_back({l, -1});
                events.push_back({r + 1, 1});
            } else {
                events.push_back({0, -1});
                events.push_back({r + 1, 1});
                events.push_back({l, -1});
                events.push_back({A, 1});
            }
        }
     
        if (full) {
            cout << A << endl;
            if (halved) A *= 2;
            return;
        }
     
        ll ans = 0;
        int open = 0;
        sort(all(events));
        ll cur_time = 0;
        for (auto ev : events) {
            cout << ev.first << " " << ev.second << endl;
            ll passed = ev.first - cur_time;
            if (open > 0) ans += passed;
            open -= ev.second;
            cur_time = ev.first;
        }
        cout << ans << endl;
     
        if (halved) A *= 2;
    }
     
    bool not_taken (ll pos, const vector<ll> &begs, const vector<ll> &ends) {
        ll begs_before = lower_bound(all(begs), pos + 1) - begs.begin();
        ll ends_before = lower_bound(all(ends), pos) - ends.begin();
        return begs_before == ends_before;
    }
     
    ll process (vector<pair<ll, int>> &v) {
        sort(all(v));
        ll cur_time = 0;
        int open = 0;
        ll ans = 0;
     
        vector<ll> begs, ends;
        for (auto ev : v) {
            if (ev.second == -1) begs.push_back(ev.first);
            else ends.push_back(ev.first);
     
            ll passed = ev.first - cur_time;
            if (open > 0) ans += passed;
            open -= ev.second;
            cur_time = ev.first;
        }
        return ans;
    }
     
    void solve() {
        ll cycle_length = A / __gcd(A, B + 1);
        bool got_all = false;
     
        vector<pair<ll, int>> events;
        unordered_map<ll, vector<pair<ll, int>>> events_all;
     
        for (int i = 1; i <= n; i++) {
            ll l = L[i], r = R[i];
            ll k1 = l / B, r1 = l % B;
            ll k2 = r / B, r2 = r % B;
     
            if (k1 == k2) {
                events_all[k1 % cycle_length].emplace_back(r1, -1);
                events_all[k1 % cycle_length].emplace_back(r2 + 1, 1);
            } else {
                events_all[k1 % cycle_length].emplace_back(r1, -1);
                events_all[k1 % cycle_length].emplace_back(B, 1);
     
                events_all[k2 % cycle_length].emplace_back(0, -1);
                events_all[k2 % cycle_length].emplace_back(r2 + 1, 1);
            }
     
            ll fr = k1 + 1, tt = k2 - 1;
            if (fr > tt) continue;
            if (tt - fr + 1 >= cycle_length) {
                got_all = true;
                break;
            }
     
            fr %= cycle_length;
            tt %= cycle_length;
     
            if (fr <= tt) {
                events.push_back({fr, -1});
                events.push_back({tt + 1, 1});
            } else {
                events.push_back({0, -1});
                events.push_back({tt + 1, 1});
                events.push_back({fr, -1});
                events.push_back({cycle_length, 1});
            }
        }
     
        if (got_all) {
            cout << B * cycle_length << endl;
            return;
        }
     
        sort(all(events));
        ll cur_time = 0;
        int open = 0;
     
        vector<ll> begs, ends;
        ll whole = process(events);
     
        for (auto ev : events) {
            if (ev.second == -1) begs.push_back(ev.first);
            else ends.push_back(ev.first - 1);
        }
     
        ll ans = B * whole;
     
        for (auto [k, v] : events_all) {
            if (not_taken(k, begs, ends)) {
                ans += process(v);
            }
        }
     
        cout << ans << endl;
    }
    int main() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL); cout.tie(NULL);
        cin >> n >> A >> B;
        ll S = 0, SL = 0;
        for (int i = 1; i <= n; i++) {
            cin >> L[i] >> R[i];
            S += (R[i] - L[i] + 1);
            SL = max(SL, R[i] - L[i] + 1);
        }
        solve();
        return 0;
    }

Compilation message

strange_device.cpp: In function 'void solve()':
strange_device.cpp:161:12: warning: unused variable 'cur_time' [-Wunused-variable]
  161 |         ll cur_time = 0;
      |            ^~~~~~~~
strange_device.cpp:162:13: warning: unused variable 'open' [-Wunused-variable]
  162 |         int open = 0;
      |             ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 4 ms 3416 KB Output is correct
3 Correct 4 ms 3164 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2512 KB Output is correct
13 Correct 1 ms 2252 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 4 ms 3420 KB Output is correct
17 Correct 40 ms 14284 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2512 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 1 ms 2556 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 562 ms 204096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 490 ms 154800 KB Output is correct
3 Correct 1407 ms 313656 KB Output is correct
4 Correct 1420 ms 313084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 490 ms 154800 KB Output is correct
3 Correct 1407 ms 313656 KB Output is correct
4 Correct 1420 ms 313084 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1074 ms 241488 KB Output is correct
7 Correct 436 ms 117096 KB Output is correct
8 Correct 982 ms 242044 KB Output is correct
9 Correct 1079 ms 242324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 490 ms 154800 KB Output is correct
3 Correct 1407 ms 313656 KB Output is correct
4 Correct 1420 ms 313084 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 61 ms 24324 KB Output is correct
7 Correct 99 ms 30668 KB Output is correct
8 Correct 112 ms 30232 KB Output is correct
9 Correct 101 ms 30452 KB Output is correct
10 Correct 56 ms 21788 KB Output is correct
11 Correct 96 ms 30448 KB Output is correct
12 Correct 92 ms 31172 KB Output is correct
13 Correct 92 ms 31560 KB Output is correct
14 Correct 44 ms 14288 KB Output is correct
15 Correct 72 ms 24508 KB Output is correct
16 Correct 33 ms 14784 KB Output is correct
17 Correct 110 ms 30396 KB Output is correct
18 Correct 1137 ms 239608 KB Output is correct
19 Correct 1306 ms 239372 KB Output is correct
20 Correct 1409 ms 239748 KB Output is correct
21 Correct 99 ms 30204 KB Output is correct
22 Correct 98 ms 30244 KB Output is correct
23 Correct 193 ms 83072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 32 ms 13976 KB Output is correct
3 Correct 32 ms 13904 KB Output is correct
4 Correct 450 ms 131320 KB Output is correct
5 Correct 35 ms 14928 KB Output is correct
6 Correct 35 ms 14820 KB Output is correct
7 Correct 34 ms 14928 KB Output is correct
8 Correct 40 ms 17336 KB Output is correct
9 Correct 40 ms 17584 KB Output is correct
10 Correct 33 ms 13668 KB Output is correct
11 Correct 40 ms 14084 KB Output is correct
12 Correct 32 ms 13912 KB Output is correct
13 Correct 40 ms 16468 KB Output is correct
14 Correct 316 ms 82024 KB Output is correct
15 Correct 42 ms 14028 KB Output is correct
16 Correct 397 ms 81568 KB Output is correct
17 Correct 465 ms 128448 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 4 ms 3416 KB Output is correct
3 Correct 4 ms 3164 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 0 ms 2512 KB Output is correct
13 Correct 1 ms 2252 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 4 ms 3420 KB Output is correct
17 Correct 40 ms 14284 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2512 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 2 ms 2652 KB Output is correct
26 Correct 1 ms 2556 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 562 ms 204096 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 490 ms 154800 KB Output is correct
31 Correct 1407 ms 313656 KB Output is correct
32 Correct 1420 ms 313084 KB Output is correct
33 Correct 1 ms 2396 KB Output is correct
34 Correct 1074 ms 241488 KB Output is correct
35 Correct 436 ms 117096 KB Output is correct
36 Correct 982 ms 242044 KB Output is correct
37 Correct 1079 ms 242324 KB Output is correct
38 Correct 1 ms 2396 KB Output is correct
39 Correct 61 ms 24324 KB Output is correct
40 Correct 99 ms 30668 KB Output is correct
41 Correct 112 ms 30232 KB Output is correct
42 Correct 101 ms 30452 KB Output is correct
43 Correct 56 ms 21788 KB Output is correct
44 Correct 96 ms 30448 KB Output is correct
45 Correct 92 ms 31172 KB Output is correct
46 Correct 92 ms 31560 KB Output is correct
47 Correct 44 ms 14288 KB Output is correct
48 Correct 72 ms 24508 KB Output is correct
49 Correct 33 ms 14784 KB Output is correct
50 Correct 110 ms 30396 KB Output is correct
51 Correct 1137 ms 239608 KB Output is correct
52 Correct 1306 ms 239372 KB Output is correct
53 Correct 1409 ms 239748 KB Output is correct
54 Correct 99 ms 30204 KB Output is correct
55 Correct 98 ms 30244 KB Output is correct
56 Correct 193 ms 83072 KB Output is correct
57 Correct 1 ms 2396 KB Output is correct
58 Correct 32 ms 13976 KB Output is correct
59 Correct 32 ms 13904 KB Output is correct
60 Correct 450 ms 131320 KB Output is correct
61 Correct 35 ms 14928 KB Output is correct
62 Correct 35 ms 14820 KB Output is correct
63 Correct 34 ms 14928 KB Output is correct
64 Correct 40 ms 17336 KB Output is correct
65 Correct 40 ms 17584 KB Output is correct
66 Correct 33 ms 13668 KB Output is correct
67 Correct 40 ms 14084 KB Output is correct
68 Correct 32 ms 13912 KB Output is correct
69 Correct 40 ms 16468 KB Output is correct
70 Correct 316 ms 82024 KB Output is correct
71 Correct 42 ms 14028 KB Output is correct
72 Correct 397 ms 81568 KB Output is correct
73 Correct 465 ms 128448 KB Output is correct
74 Correct 1 ms 2396 KB Output is correct
75 Correct 1 ms 2392 KB Output is correct
76 Correct 1 ms 2396 KB Output is correct
77 Correct 0 ms 2396 KB Output is correct
78 Correct 1 ms 2516 KB Output is correct
79 Correct 6 ms 3932 KB Output is correct
80 Correct 335 ms 88164 KB Output is correct
81 Correct 451 ms 134592 KB Output is correct
82 Correct 1181 ms 242624 KB Output is correct
83 Correct 1358 ms 243348 KB Output is correct
84 Correct 1302 ms 242736 KB Output is correct
85 Correct 1223 ms 243364 KB Output is correct
86 Correct 186 ms 86220 KB Output is correct
87 Correct 1 ms 2396 KB Output is correct