Submission #874406

# Submission time Handle Problem Language Result Execution time Memory
874406 2023-11-16T23:08:32 Z BBart888 Growing Trees (BOI11_grow) C++14
100 / 100
91 ms 3804 KB
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <array>
#include <set>
#include <queue>
#include <map>
#include <iomanip>
#include <stack>
#include <stdio.h>      
#include <stdlib.h>     
#include <time.h> 
#include <string>
#include <fstream>
#include <unordered_map>



using namespace std;
using ll = long long;
using ld = long double;



using Mat = array<array<ll, 2>, 2>;
const ll mod1 = 1e9 + 7;
const ll mod2 = 1e9 + 123;
const int MAXN = 2e5 + 11;
const int B = 100;
const ll P = 207971;


int n, m;
vector<int> h;


int bit[MAXN];

void add(int l, int r, int x) {  // add x to [l, r]
    if (r < l) return;
    for (; l < n; l |= l + 1) bit[l] += x;
    for (++r; r < n; r |= r + 1) bit[r] -= x;
}
int query(int i) {  // point query at i
    int sum = 0;
    for (; i >= 0; i &= i + 1, --i) sum += bit[i];
    return sum;
}

template <class T, class U> T firstTrue(T lo, T hi, U f) {
    //assert(lo <= hi);
    ++hi;              // assuming f is increasing
    while (lo < hi) {  // find first index such that f is true
        T mid = lo + (hi - lo) / 2;
        f(mid) ? hi = mid : lo = mid + 1;
    }
    return lo;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);


    cin >> n >> m;

    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        h.push_back(x);
    }


    sort(h.begin(), h.end());


    for (int i = 0; i < n; i++)
        add(i, i, h[i]);
    
    
    for (int i = 0; i < m; i++)
    {
        char type;
        int a, b;
        
        cin >> type >> a >> b;
        if (type == 'C')
        {
            int l = firstTrue(0, n - 1, [&](int i) {return query(i) >= a; });
            int r = firstTrue(0, n - 1, [&](int i) { return query(i) > b; });
            cout << r - l << '\n';
        }
        else
        {
            int l = firstTrue(0, n - 1, [&](int i) {return query(i) >= b; });
            if (l + a - 1 > n - 1)
            {
                add(l, n - 1, 1);
                continue;
            }

            int r = l + a - 1;

            int x = query(r);

            int _l = firstTrue(0, n - 1, [&](int i) {return query(i) >= x; });
            int _r = firstTrue(0, n - 1, [&](int i) {return query(i) > x; }) - 1;

            add(l, _l-1, 1);
            add(_r - (a-(_l - l)) + 1, _r, 1);
        }
    }












    return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 51 ms 2520 KB Output is correct
2 Correct 89 ms 2948 KB Output is correct
3 Correct 47 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 2 ms 528 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 40 ms 1364 KB Output is correct
6 Correct 43 ms 1620 KB Output is correct
7 Correct 3 ms 604 KB Output is correct
8 Correct 21 ms 1268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 1840 KB Output is correct
2 Correct 46 ms 1872 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 26 ms 1468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 1880 KB Output is correct
2 Correct 49 ms 1844 KB Output is correct
3 Correct 5 ms 604 KB Output is correct
4 Correct 50 ms 2176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 2020 KB Output is correct
2 Correct 79 ms 2380 KB Output is correct
3 Correct 14 ms 1116 KB Output is correct
4 Correct 40 ms 2544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 2132 KB Output is correct
2 Correct 82 ms 2628 KB Output is correct
3 Correct 49 ms 2784 KB Output is correct
4 Correct 14 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 2260 KB Output is correct
2 Correct 51 ms 2520 KB Output is correct
3 Correct 51 ms 2764 KB Output is correct
4 Correct 14 ms 1112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 3020 KB Output is correct
2 Correct 82 ms 2516 KB Output is correct
3 Correct 23 ms 2012 KB Output is correct
4 Correct 48 ms 2260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 2624 KB Output is correct
2 Correct 91 ms 2956 KB Output is correct
3 Correct 91 ms 3160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 3804 KB Output is correct