답안 #641565

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
641565 2022-09-17T03:43:31 Z baojiaopisu 이상한 기계 (APIO19_strange_device) C++14
100 / 100
1584 ms 92880 KB
#include<bits/stdc++.h>
 
using namespace std;
 
using ll = long long;
using ld = long double;
using ull = unsigned long long;
 
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
 
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mp make_pair
#define ins insert
#define btpc __builtin_popcount
#define btclz __builtin_clz
 
#define sz(x) (int)(x.size());
#define all(x) x.begin(), x.end()
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
 
template<class X, class Y>
    bool minimize(X &x, const Y &y) {
        if (x > y)
        {
            x = y;
            return true;
        }
        return false;
    }
template<class X, class Y>
    bool maximize(X &x, const Y &y) {
        if (x < y)
        {
            x = y;
            return true;
        }
        return false;
    }
 
const int MOD = 1e9 + 7; //998244353
 
template<class X, class Y>
    void add(X &x, const Y &y) {
        x = (x + y);
        if(x >= MOD) x -= MOD;
    }
 
template<class X, class Y> 
    void sub(X &x, const Y &y) {
        x = (x - y);
        if(x < 0) x += MOD;
    }
 
/* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/
 
const ll INF = 2e18;
const int N = 1e5 + 10;

ll gcd(ll a, ll b) {
    if(b == 0) return a;
    return gcd(b, a % b);
}
 
void solve() {
    int n; ll A, B; cin >> n >> A >> B;
    ll m = A / gcd(B + 1, A);
    if(INF / m < B) m = -1;
    else m = m * B;
 
    if(m == -1) m = INF;
    if(m > 1e18) {
        ll ans = 0;
        for(int i = 1; i <= n; i++) {
            ll L, R; cin >> L >> R;
            ans += R - L + 1;
        }
        cout << ans;
        return;
    }

    set<pll> seg;
    for(int i = 1; i <= n; i++) {
        ll L, R; cin >> L >> R;
        if(R - L + 1 >= m) {
            cout << m;
            return;
        }
 
        ll l = L % m;
        if(l + (R - L) >= m) {
            seg.ins(mp(l, m - 1));
            L += (m - 1) - l;
            L++;
            seg.ins(mp(0, R - L));
        } else {
            seg.ins(mp(l, l + (R - L)));
        }
    }
        
    ll ans = 0;
    while(seg.size() > 1) {
        auto iter = seg.begin();
        pll x = (*iter); ++iter; pll y = (*iter);
        if(x.se < y.fi) {
            ans += x.se - x.fi + 1;
            seg.erase(seg.begin());
        } else {
            seg.erase(x);
            seg.erase(y);
            x.fi = min(x.fi, y.fi);
            x.se = max(x.se, y.se);
            seg.ins(x);
        }
    }

    if(seg.size()) {
        auto iter = seg.begin();
        pll x = (*iter);
        ans += x.se - x.fi + 1;
    }

    cout << ans;
}  
 
int main()
{

    int tc = 1, ddd = 0;
    // cin >> tc;
    while(tc--) {
        //ddd++;
        //cout << "Case #" << ddd << ": ";
        solve();
    }
}

Compilation message

strange_device.cpp: In function 'int main()':
strange_device.cpp:139:17: warning: unused variable 'ddd' [-Wunused-variable]
  139 |     int tc = 1, ddd = 0;
      |                 ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 15 ms 836 KB Output is correct
3 Correct 17 ms 896 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 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 1 ms 212 KB Output is correct
16 Correct 16 ms 808 KB Output is correct
17 Correct 144 ms 6532 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 901 ms 276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1431 ms 62840 KB Output is correct
3 Correct 1497 ms 76224 KB Output is correct
4 Correct 1584 ms 76336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1431 ms 62840 KB Output is correct
3 Correct 1497 ms 76224 KB Output is correct
4 Correct 1584 ms 76336 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1467 ms 76336 KB Output is correct
7 Correct 1514 ms 76340 KB Output is correct
8 Correct 1481 ms 76424 KB Output is correct
9 Correct 1571 ms 76332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1431 ms 62840 KB Output is correct
3 Correct 1497 ms 76224 KB Output is correct
4 Correct 1584 ms 76336 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 143 ms 10216 KB Output is correct
7 Correct 158 ms 10208 KB Output is correct
8 Correct 144 ms 10156 KB Output is correct
9 Correct 144 ms 10192 KB Output is correct
10 Correct 142 ms 10260 KB Output is correct
11 Correct 150 ms 10248 KB Output is correct
12 Correct 142 ms 10148 KB Output is correct
13 Correct 157 ms 10128 KB Output is correct
14 Correct 160 ms 10188 KB Output is correct
15 Correct 162 ms 10248 KB Output is correct
16 Correct 145 ms 10196 KB Output is correct
17 Correct 144 ms 10156 KB Output is correct
18 Correct 1441 ms 76548 KB Output is correct
19 Correct 1481 ms 76500 KB Output is correct
20 Correct 1475 ms 76540 KB Output is correct
21 Correct 149 ms 10292 KB Output is correct
22 Correct 143 ms 10244 KB Output is correct
23 Correct 435 ms 12752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 147 ms 6468 KB Output is correct
3 Correct 145 ms 6488 KB Output is correct
4 Correct 1505 ms 76412 KB Output is correct
5 Correct 145 ms 10136 KB Output is correct
6 Correct 146 ms 10228 KB Output is correct
7 Correct 146 ms 10248 KB Output is correct
8 Correct 150 ms 10280 KB Output is correct
9 Correct 144 ms 10156 KB Output is correct
10 Correct 139 ms 10192 KB Output is correct
11 Correct 143 ms 10164 KB Output is correct
12 Correct 143 ms 10180 KB Output is correct
13 Correct 144 ms 10224 KB Output is correct
14 Correct 1502 ms 76364 KB Output is correct
15 Correct 142 ms 10188 KB Output is correct
16 Correct 1467 ms 69616 KB Output is correct
17 Correct 1442 ms 69552 KB Output is correct
18 Correct 1 ms 220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 15 ms 836 KB Output is correct
3 Correct 17 ms 896 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 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 1 ms 212 KB Output is correct
16 Correct 16 ms 808 KB Output is correct
17 Correct 144 ms 6532 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 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 2 ms 340 KB Output is correct
26 Correct 3 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 901 ms 276 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1431 ms 62840 KB Output is correct
31 Correct 1497 ms 76224 KB Output is correct
32 Correct 1584 ms 76336 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1467 ms 76336 KB Output is correct
35 Correct 1514 ms 76340 KB Output is correct
36 Correct 1481 ms 76424 KB Output is correct
37 Correct 1571 ms 76332 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 143 ms 10216 KB Output is correct
40 Correct 158 ms 10208 KB Output is correct
41 Correct 144 ms 10156 KB Output is correct
42 Correct 144 ms 10192 KB Output is correct
43 Correct 142 ms 10260 KB Output is correct
44 Correct 150 ms 10248 KB Output is correct
45 Correct 142 ms 10148 KB Output is correct
46 Correct 157 ms 10128 KB Output is correct
47 Correct 160 ms 10188 KB Output is correct
48 Correct 162 ms 10248 KB Output is correct
49 Correct 145 ms 10196 KB Output is correct
50 Correct 144 ms 10156 KB Output is correct
51 Correct 1441 ms 76548 KB Output is correct
52 Correct 1481 ms 76500 KB Output is correct
53 Correct 1475 ms 76540 KB Output is correct
54 Correct 149 ms 10292 KB Output is correct
55 Correct 143 ms 10244 KB Output is correct
56 Correct 435 ms 12752 KB Output is correct
57 Correct 0 ms 212 KB Output is correct
58 Correct 147 ms 6468 KB Output is correct
59 Correct 145 ms 6488 KB Output is correct
60 Correct 1505 ms 76412 KB Output is correct
61 Correct 145 ms 10136 KB Output is correct
62 Correct 146 ms 10228 KB Output is correct
63 Correct 146 ms 10248 KB Output is correct
64 Correct 150 ms 10280 KB Output is correct
65 Correct 144 ms 10156 KB Output is correct
66 Correct 139 ms 10192 KB Output is correct
67 Correct 143 ms 10164 KB Output is correct
68 Correct 143 ms 10180 KB Output is correct
69 Correct 144 ms 10224 KB Output is correct
70 Correct 1502 ms 76364 KB Output is correct
71 Correct 142 ms 10188 KB Output is correct
72 Correct 1467 ms 69616 KB Output is correct
73 Correct 1442 ms 69552 KB Output is correct
74 Correct 1 ms 220 KB Output is correct
75 Correct 1 ms 308 KB Output is correct
76 Correct 1 ms 212 KB Output is correct
77 Correct 0 ms 212 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 14 ms 1196 KB Output is correct
80 Correct 1485 ms 92508 KB Output is correct
81 Correct 1479 ms 92820 KB Output is correct
82 Correct 1503 ms 91968 KB Output is correct
83 Correct 1520 ms 92020 KB Output is correct
84 Correct 1507 ms 92880 KB Output is correct
85 Correct 1483 ms 92488 KB Output is correct
86 Correct 410 ms 12856 KB Output is correct
87 Correct 1 ms 212 KB Output is correct