답안 #782499

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
782499 2023-07-14T03:11:47 Z horiiseun Growing Trees (BOI11_grow) C++17
100 / 100
149 ms 11788 KB
#include <iostream>
#include <vector>
#include <tuple>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <cmath>
#include <random>
#include <string>
#include <bitset>
#include <cassert>
#include <climits>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
using namespace std;
 
#define ll long long
#define f first
#define s second
 
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
 
template<typename A> void __print(const A &x);
template<typename A, typename B> void __print(const pair<A, B> &p);
template<typename... A> void __print(const tuple<A...> &t);
template<typename T> void __print(stack<T> s);
template<typename T> void __print(queue<T> q);
template<typename T, typename... U> void __print(priority_queue<T, U...> q);
 
template<typename A> void __print(const A &x) {
    bool first = true;
    cerr << '{';
    for (const auto &i : x) {
        cerr << (first ? "" : ","), __print(i);
        first = false;
    }
    cerr << '}';
}
 
template<typename A, typename B> void __print(const pair<A, B> &p) {
    cerr << '(';
    __print(p.f);
    cerr << ',';
    __print(p.s);
    cerr << ')';
}
 
template<typename... A> void __print(const tuple<A...> &t) {
    bool first = true;
    cerr << '(';
    apply([&first] (const auto &...args) { ((cerr << (first ? "" : ","), __print(args), first = false), ...); }, t);
    cerr << ')';
}
 
template<typename T> void __print(stack<T> s) {
    vector<T> debugVector;
    while (!s.empty()) {
        T t = s.top();
        debugVector.push_back(t);
        s.pop();
    }
    reverse(debugVector.begin(), debugVector.end());
    __print(debugVector);
}
 
template<typename T> void __print(queue<T> q) {
    vector<T> debugVector;
    while (!q.empty()) {
        T t = q.front();
        debugVector.push_back(t);
        q.pop();
    }
    __print(debugVector);
}
 
template<typename T, typename... U> void __print(priority_queue<T, U...> q) {
    vector<T> debugVector;
    while (!q.empty()) {
        T t = q.top();
        debugVector.push_back(t);
        q.pop();
    }
    __print(debugVector);
}
 
void _print() { cerr << "]\n"; }
 
template <typename Head, typename... Tail> void _print(const Head &H, const Tail &...T) {
    __print(H);
    if (sizeof...(T)) cerr << ", ";
    _print(T...);
}
 
#ifdef DEBUG
	#define D(...) cerr << "Line: " << __LINE__ << " [" << #__VA_ARGS__ << "] = ["; _print(__VA_ARGS__)
#else
    #define D(...)
#endif
 
struct Node {
    int l, r, val, lazy;
    Node *lft, *rht;
    Node (int tl, int tr, vector<int> &v): l(tl), r(tr), val(0), lazy(0) {
        if (l + 1 == r) {
            lft = rht = NULL;
            val = v[l];
        } else {
            lft = new Node(l, (l + r) / 2, v);
            rht = new Node((l + r) / 2, r, v);
            val = max(lft->val, rht->val);
        }
    }
};
 
int n, q;
vector<int> h;
Node *root;
 
void propagate(Node *x) {
    x->lft->val += x->lazy;
    x->rht->val += x->lazy;
    x->lft->lazy += x->lazy;
    x->rht->lazy += x->lazy;
    x->lazy = 0;
}
 
void update(Node *x, int l, int r, int v) {
    if (r <= x->l || x->r <= l) {
        return;
    }
    if (l <= x->l && x->r <= r) {
        x->val += v;
        x->lazy += v;
        return;
    }
    propagate(x);
    update(x->lft, l, r, v);
    update(x->rht, l, r, v);
    x->val = max(x->lft->val, x->rht->val);
}
 
int lb(Node *x, int v) {
    if (root->val < v) return n;
    if (x->l + 1 == x->r) {
        return x->l;
    }
    propagate(x);
    if (x->lft->val < v) return lb(x->rht, v);
    return lb(x->lft, v);
}
 
int query(Node *x, int pos) {
    if (pos < x->l || x->r <= pos) {
        return 0;
    }
    if (x->l + 1 == x->r) {
        return x->val;
    }
    propagate(x);
    return max(query(x->lft, pos), query(x->rht, pos));
}
 
int main() {
 
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    cin >> n >> q;
    for (int i = 0, x; i < n; i++) {
        cin >> x;
        h.push_back(x);
    }
    sort(h.begin(), h.end());
 
    root = new Node(0, n, h);
    for (int i = 0, a, b, lf, rh, v, pos, idx, rm, idx1; i < q; i++) {
        char c; cin >> c >> a >> b;
        if (c == 'F') {
            lf = lb(root, b);
            if (lf == n) continue;
            rh = min(lf + a - 1, n - 1), v = query(root, rh), pos = lb(root, v), idx = rh - pos + 1, rm = lb(root, v + 1);
            update(root, lf, pos, 1);
            update(root, rm - idx, rm, 1);
        } else {
            idx = lb(root, b + 1), idx1 = lb(root, a);
            cout << idx - idx1 << "\n";
        }
    }
 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 9972 KB Output is correct
2 Correct 96 ms 10460 KB Output is correct
3 Correct 77 ms 9932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 456 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 28 ms 1744 KB Output is correct
6 Correct 36 ms 1996 KB Output is correct
7 Correct 4 ms 852 KB Output is correct
8 Correct 22 ms 1568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 2132 KB Output is correct
2 Correct 36 ms 2340 KB Output is correct
3 Correct 2 ms 972 KB Output is correct
4 Correct 26 ms 1864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 2196 KB Output is correct
2 Correct 39 ms 2252 KB Output is correct
3 Correct 7 ms 1132 KB Output is correct
4 Correct 41 ms 2352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 7756 KB Output is correct
2 Correct 84 ms 8644 KB Output is correct
3 Correct 13 ms 2348 KB Output is correct
4 Correct 63 ms 8680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 8612 KB Output is correct
2 Correct 95 ms 9380 KB Output is correct
3 Correct 80 ms 8828 KB Output is correct
4 Correct 14 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 9188 KB Output is correct
2 Correct 68 ms 10192 KB Output is correct
3 Correct 88 ms 9784 KB Output is correct
4 Correct 12 ms 2380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 141 ms 9748 KB Output is correct
2 Correct 92 ms 8864 KB Output is correct
3 Correct 29 ms 10804 KB Output is correct
4 Correct 69 ms 10304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 9740 KB Output is correct
2 Correct 97 ms 10424 KB Output is correct
3 Correct 149 ms 11788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 11276 KB Output is correct