답안 #983283

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
983283 2024-05-15T09:56:15 Z vjudge1 이상한 기계 (APIO19_strange_device) C++17
15 / 100
603 ms 190920 KB
#include <time.h>
#include <cstdlib>
#include <stack>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <map>
#include <set>
#include <iterator>
#include <deque>
#include <queue>
#include <sstream>
#include <array>
#include <string>
#include <tuple>
#include <chrono>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <list>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <bitset>
#define ll long long
using namespace std;

ll n, A, B;
ll l[1000005], r[1000005], sum = 0;
set<pair<ll, ll>> st1, st;
deque<pair<ll, ll>> st2;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n >> A >> B;
    for(int i = 1; i <= n; i++){
      cin >> l[i] >> r[i];
      sum += (r[i] - l[i] + 1);
    }
    if(sum <= 1e6){
      for(int i = 1; i <= n; i++){
          for(ll j = l[i]; j <= r[i]; j++){
              ll x = (j + j / B) % A, y = (j % B);
              st.insert({x, y});
          }
      }
      cout << int(st.size()) << "\n";
      return 0;
    }
    if(n == 1){
        ll x = (l[1] + l[1] / B) % A; 
        if(l[1] + B <= r[1]){
            ll num = l[1] + B;
            ll y = (num + num / B) % A;
            if(x == y){
                cout << B << "\n";
                return 0;
            }
            ll d = abs(x - y);
            ll ans = gcd(d, A);
            ll rg = l[1] + ans * B - 1;
            cout << min(r[1], rg) - l[1] + 1 << "\n";
            return 0;
        }
        else{
            cout << r[1] - l[1] + 1 << "\n";
            return 0;
        }
    }
    ll lf[n + 2], rg[n + 2];
    for(int i = 1; i <= n; i++){
        lf[i] = l[i];
        rg[i] = min(r[i], l[i] + A - 1);
        st1.insert({l[i], i});
        // st2.insert({rg[i], i});
    }
    ll ans = 0, mx = 0, mn = 0;
    bool f = 0;
    while(!st1.empty()){
        if(!f){
            auto it = st1.begin();
            mn = it->first;
            mx = r[it->second];
            f = 1;
            st1.erase(st1.begin());
            continue;
        }
        else{
            if(st1.begin()->first <= mx){
                auto it = st1.begin();
                mx = max(r[it->second], mx);
                st1.erase(st1.begin());
                continue;
            }
            else{
                ll res = A;
                if(res % 2 == 1) res *= 2;
                if(mx - mn + 1 >= res / 2){
                    cout << res / 2;
                    return 0;
                }
                ll l1 = (2 * mn) % A;
                ll r1 = (2 * mx) % A;
                if(l1 > r1){
                    st2.push_back({l1, A - (A % 2)});
                    st2.push_back({0, r1});
                }
                else{
                    st2.push_back({l1, r1});
                }
                auto it = st1.begin();
                mn = it->first;
                mx = r[it->second];
                f = 1;
                st1.erase(st1.begin());
            }
        }
    }
    ll res = A;
    if(res % 2 == 1) res *= 2;
    if(mx - mn + 1 >= res / 2){
        cout << res / 2;
        return 0;
    }
    ll l1 = (2 * mn) % A;
    ll r1 = (2 * mx) % A;
    if(l1 > r1){
        st2.push_back({l1, A - (A % 2)});
        st2.push_back({0, r1});
    }
    else st2.push_back({l1, r1});
    sort(st2.begin(), st2.end());
    f = 0;
    mn = 0, mx = 0;
    ans = 0;
    cout << 1 / 0;
    while(!st2.empty()){
        if(!f){
            f = 1;
            mn = st2.front().first;
            mx = st2.front().second;
            st2.pop_front();
            continue;
        }
        else{
            if(st2.front().first <= mx){
                mx = max(mx, st2.front().second);
                st2.pop_front();
                continue;
            }
            else{
                ans = (mx - mn) / 2;
                mn = st2.front().first;
                mn = st2.front().first;
                mx = st2.front().second;
                st2.pop_front();
            }
        }
    }
    ans += (mx - mn) / 2;
    cout << ans;
}

Compilation message

strange_device.cpp: In function 'int main()':
strange_device.cpp:138:15: warning: division by zero [-Wdiv-by-zero]
  138 |     cout << 1 / 0;
      |             ~~^~~
strange_device.cpp:72:8: warning: variable 'lf' set but not used [-Wunused-but-set-variable]
   72 |     ll lf[n + 2], rg[n + 2];
      |        ^~
strange_device.cpp:72:19: warning: variable 'rg' set but not used [-Wunused-but-set-variable]
   72 |     ll lf[n + 2], rg[n + 2];
      |                   ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 34 ms 14424 KB Output is correct
3 Correct 57 ms 20000 KB Output is correct
4 Correct 2 ms 2908 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 6 ms 3164 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 33 ms 9352 KB Output is correct
16 Correct 21 ms 8792 KB Output is correct
17 Correct 49 ms 12880 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 2396 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 104 ms 34388 KB Output is correct
3 Correct 130 ms 34092 KB Output is correct
4 Correct 111 ms 32596 KB Output is correct
5 Incorrect 364 ms 94420 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Runtime error 603 ms 190920 KB Execution killed with signal 4
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Runtime error 603 ms 190920 KB Execution killed with signal 4
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Runtime error 603 ms 190920 KB Execution killed with signal 4
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Incorrect 38 ms 14464 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 34 ms 14424 KB Output is correct
3 Correct 57 ms 20000 KB Output is correct
4 Correct 2 ms 2908 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 6 ms 3164 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 33 ms 9352 KB Output is correct
16 Correct 21 ms 8792 KB Output is correct
17 Correct 49 ms 12880 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 2396 KB Output is correct
23 Correct 1 ms 2392 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 104 ms 34388 KB Output is correct
26 Correct 130 ms 34092 KB Output is correct
27 Correct 111 ms 32596 KB Output is correct
28 Incorrect 364 ms 94420 KB Output isn't correct
29 Halted 0 ms 0 KB -