Submission #783652

# Submission time Handle Problem Language Result Execution time Memory
783652 2023-07-15T07:34:09 Z chanhchuong123 Growing Trees (BOI11_grow) C++14
100 / 100
47 ms 1604 KB
#include <bits/stdc++.h>
using namespace std;
#define task ""
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)

template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}

const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};

const int MAX = 100100;
int n, m;
int h[MAX];
int bit[MAX];

void update(int id, int delta) {
    for (; id <= n; id += id & -id)
        bit[id] += delta;
}
void update(int l, int r, int delta) {
    update(l, +delta);
    update(r + 1, -delta);
}

int get(int id) {
    int res = 0;
    for (; id > 0; id -= id & -id)
        res += bit[id];
    return res;
}

int getPos(int value) {
    int id = 0, curValue = 0;
    for (int j = 16; j >= 0; --j) if (id + (1 << j) <= n) {
        if (curValue + bit[id + (1 << j)] <= value) {
            curValue += bit[id + (1 << j)];
            id += (1 << j);
        }
    }
    return id;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);

	if (fopen(task".inp", "r")) {
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}

    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        cin >> h[i];
    }
    sort(h + 1, h + 1 + n);
    for (int i = 1; i <= n; ++i) {
        update(i, i, h[i]);
    }
    for (int i = 1; i <= m; ++i) {
        char op; cin >> op;
        if (op == 'C') {
            int l, r; cin >> l >> r;
            cout << getPos(r) - getPos(l - 1) << '\n';
        } else {
            int c, h;cin >> c >> h;
            int st = getPos(h - 1);
            if (st + c >= n) update(st + 1, n, +1);
            else {
                 int lastValue = get(st + c), lt = getPos(lastValue - 1);
                 if (lt - st <= c) {
                    update(st + 1, lt, +1);
                    c -= lt - st;
                 }
                 if (c > 0) {
                    int rt = getPos(lastValue);
                    update(max(lt + 1, rt - c + 1), rt, +1);
                 }
            }
        }
    }


	return 0;
}

Compilation message

grow.cpp: In function 'int main()':
grow.cpp:53:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:54:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 33 ms 1236 KB Output is correct
2 Correct 45 ms 1116 KB Output is correct
3 Correct 29 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 19 ms 556 KB Output is correct
6 Correct 25 ms 588 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 23 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 644 KB Output is correct
2 Correct 24 ms 724 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 20 ms 584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 716 KB Output is correct
2 Correct 26 ms 656 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 25 ms 676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 952 KB Output is correct
2 Correct 41 ms 1008 KB Output is correct
3 Correct 8 ms 484 KB Output is correct
4 Correct 24 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 980 KB Output is correct
2 Correct 41 ms 1072 KB Output is correct
3 Correct 28 ms 860 KB Output is correct
4 Correct 8 ms 520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 1176 KB Output is correct
2 Correct 33 ms 1228 KB Output is correct
3 Correct 29 ms 940 KB Output is correct
4 Correct 8 ms 532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 1108 KB Output is correct
2 Correct 41 ms 1072 KB Output is correct
3 Correct 17 ms 1100 KB Output is correct
4 Correct 38 ms 1092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 1236 KB Output is correct
2 Correct 45 ms 1052 KB Output is correct
3 Correct 47 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 1604 KB Output is correct