답안 #593378

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
593378 2022-07-11T02:02:39 Z Do_you_copy Growing Trees (BOI11_grow) C++17
100 / 100
88 ms 3380 KB
#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
using ull = unsigned ll;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());

ll min(const ll &a, const ll &b){
    return (a < b) ? a : b;
}

ll max(const ll &a, const ll &b){
    return (a > b) ? a : b;
}

//const ll Mod = 1000000009;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 1e5 + 1;

int n, m;
int bit[maxN];
int a[maxN];

void update(int x, int val){
    for (; x <= n; x += x & -x){
        bit[x] += val;
    }
}

int get(int x){
    int ans = 0;
    for (; x; x -= x & -x){
        ans += bit[x];
    }
    return ans;
}

void updatehigh(int x, int y){
    int l = 1, r = n;
    while (l <= r){
        int mid = (l + r) / 2;
        if (get(mid) < y) l = mid + 1;
        else r = mid - 1;
    }
    ++r;
    if (r + x - 1 >= n){
        update(r, 1);
        return;
    }
    int need = x;
    x = r + x - 1;
    int s = r;
    int z = get(x);
    l = r; r = x;
    while (l <= r){
        int mid = (l + r) / 2;
        if (get(mid) != z) l = mid + 1;
        else r = mid - 1;
    }
    int ll = r + 1;
    l = x; r = n;
    while (l <= r){
        int mid = (l + r) / 2;
        if (get(mid) != z) r = mid - 1;
        else l = mid + 1;
    }
    int rr = r + 1;
    if (ll != s){
        update(s, 1);
        update(ll, -1);
    }
    update(rr - (need - (ll - s)), 1);
    update(rr, -1);
}

int getlow(int x){
    int l = 1, r = n;
    while (l <= r){
        int mid = (l + r) / 2;
        if (get(mid) < x) l = mid + 1;
        else r = mid - 1;
    }
    //cerr << r << " " << x << "?\n";
    return r;
}

int gethigh(int x){
    int l = 1, r = n;
    while (l <= r){
        int mid = (l + r) / 2;
        if (get(mid) <= x) l = mid + 1;
        else r = mid - 1;
    }
    //cerr << r << " " << x << "!\n";
    return r;
}


void Init(){
    cin >> n >> m;
    for (int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; ++i) update(i, a[i] - a[i - 1]);
    while (m--){
        /*for (int i = 1; i <= n; ++i) cerr << get(i) << " ";
        cerr << "\n";*/
        char c; cin >> c;
        if (c == 'F'){
            int x, y;
            cin >> x >> y;
            updatehigh(x, y);
        }
        else{
            int l, r;
            cin >> l >> r;
            cout << gethigh(r) - getlow(l) << "\n";
        }
    }
}


int main(){
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    faster;
    ll tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
}

Compilation message

grow.cpp: In function 'int main()':
grow.cpp:135:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 2448 KB Output is correct
2 Correct 84 ms 2828 KB Output is correct
3 Correct 35 ms 2556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 42 ms 1340 KB Output is correct
6 Correct 48 ms 1604 KB Output is correct
7 Correct 3 ms 356 KB Output is correct
8 Correct 24 ms 1052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 1628 KB Output is correct
2 Correct 50 ms 1772 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 31 ms 1304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 1872 KB Output is correct
2 Correct 59 ms 1780 KB Output is correct
3 Correct 5 ms 460 KB Output is correct
4 Correct 52 ms 1732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 1992 KB Output is correct
2 Correct 78 ms 2252 KB Output is correct
3 Correct 15 ms 852 KB Output is correct
4 Correct 30 ms 2352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 1956 KB Output is correct
2 Correct 81 ms 2388 KB Output is correct
3 Correct 33 ms 2508 KB Output is correct
4 Correct 15 ms 820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 2108 KB Output is correct
2 Correct 61 ms 2300 KB Output is correct
3 Correct 34 ms 2636 KB Output is correct
4 Correct 15 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 2680 KB Output is correct
2 Correct 79 ms 2200 KB Output is correct
3 Correct 23 ms 1820 KB Output is correct
4 Correct 53 ms 2212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 2520 KB Output is correct
2 Correct 86 ms 2708 KB Output is correct
3 Correct 88 ms 2884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 3380 KB Output is correct