답안 #798474

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
798474 2023-07-30T18:17:14 Z serifefedartar Growing Trees (BOI11_grow) C++17
10 / 100
1000 ms 3300 KB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 998244353
#define LOGN 20
#define MAXN 100005
 
int tree[MAXN];
void add(int k, int a) {
    while (k < MAXN) {
        tree[k] += a;
        k += k&-k;
    }
}

void update(int l, int r, int x) {
    add(r+1, -x);
    add(l, x);
}

int query(int k) {
    int res = 0;
    while (k) {
        res += tree[k];
        k -= k&-k;
    }
    return res;
}

int main() {
    fast
    int N, M, q, l, r, c, h;
    cin >> N >> M;

    vector<int> arr(N);
    for (int i = 0; i < N; i++)
        cin >> arr[i];
    sort(arr.begin(), arr.end());
    for (int i = 1; i <= N; i++)
        update(i, i, arr[i-1]);

    char type;
    while (M--) {
        cin >> type;
        if (type == 'F') {
            cin >> c >> h;
            int L = 1;
            int R = N;
            int ans = -1;
            while (R >= L) {
                int mid = L + (R-L)/2;
                int q = query(mid);
                if (q >= h) {
                    R = mid - 1;
                    ans = mid;
                } else
                    L = mid + 1;
            }

            int rightEnd = ans + c - 1;
            if (rightEnd >= N) {
                update(ans, N, 1);
            } else {
                int q = query(rightEnd);
                int L = 1;
                int R = N;
                int ans2 = -1;
                while (R >= L) {
                    int mid = L + (R-L)/2;
                    int q2 = query(mid);
                    if (q2 >= q) {
                        R = mid - 1;
                        ans2 = mid; 
                    } else
                        L = mid + 1;
                }

                L = 1;
                R = N;
                int right = -1;
                while (R >= L) {
                    int mid = L + (R-L)/2;
                    int q2 = query(mid);
                    if (q2 <= q) {
                        L = mid + 1;
                        right = mid;
                    } else
                        R = mid - 1;
                }

                int len = ans2-ans;
                update(ans, ans2-1, 1);
                update(right-c+len+1, right, 1);
            }
        } else {
            cin >> l >> r;
            int L = 1;
            int R = N;
            int ans1 = -1;
            while (R >= L) {
                int mid = L + (R-L)/2;
                int q = query(mid);
                if (q >= l) {
                    R = mid - 1;
                    ans1 = mid;
                } else
                    L = mid + 1;
            }

            L = 1;
            R = N;
            int ans2 = -1;
            while (R >= L) {
                int mid = L + (R-L)/2;
                int q = query(mid);
                if (q > r) {
                    R = mid - 1;
                    ans2 = mid;
                } else
                    L = mid + 1;
            }
            if (ans1 == -1)
                cout << "0\n"; 
            else {
                if (ans2 == -1)
                    ans2 = N+1;
                cout << ans2 - ans1 << "\n";
            }
        }
    }
}

Compilation message

grow.cpp: In function 'int main()':
grow.cpp:36:15: warning: unused variable 'q' [-Wunused-variable]
   36 |     int N, M, q, l, r, c, h;
      |               ^
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1075 ms 1464 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 336 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 36 ms 1356 KB Output is correct
6 Execution timed out 1091 ms 468 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1065 ms 852 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1068 ms 468 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1068 ms 1364 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1074 ms 1432 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1076 ms 1276 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 2764 KB Output is correct
2 Execution timed out 1079 ms 1508 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1069 ms 1620 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 3300 KB Output is correct