Submission #289722

# Submission time Handle Problem Language Result Execution time Memory
289722 2020-09-02T23:42:09 Z jainbot27 Growing Trees (BOI11_grow) C++17
100 / 100
125 ms 3448 KB
//author: JainBot27
//Created on: 09/02/20
#pragma GCC optimize("Ofast")
#pragma GCC target("sse4,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;


#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ts to_string
#define ub upper_bound
#define lb lower_bound
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()
#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, y, z) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, z, 0)
#define trav(x, y) for(auto&x:y)


const string nl = "\n";
using ll = int64_t;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using str = string;
using vpii = vector<pii>;
using vpll = vector<pll>;


template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


str ts(char c) { return str(1,c); }
str ts(bool b) { return b ? "true" : "false"; }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
template<class A> str ts(complex<A> c) { 
    stringstream ss; ss << c; return ss.str(); }
str ts(vector<bool> v) { 
    str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
    res += "}"; return res; }
template<size_t SZ> str ts(bitset<SZ> b) {
    str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
    return res; }
template<class A, class B> str ts(pair<A,B> p);
template<class T> str ts(T v) { 
    bool fst = 1; str res = "{";
    for (const auto& x: v) {
        if (!fst) res += ", ";
        fst = 0; res += ts(x);
    }
    res += "}"; return res;
}
template<class A, class B> str ts(pair<A,B> p) {
    return "("+ts(p.f)+", "+ts(p.s)+")"; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << ts(h); if (sizeof...(t)) cerr << ", ";
    DBG(t...); }


#ifdef D 
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

using node = int;
struct BIT{
    //ask for queries 0 idxed or remove ++idx/++r
    vector<node> bit;
    int n;
    void init(int x){
        n=x+1;
        bit.assign(n + 1,0);
    }
    node sum(int r){
        node ret = 0;
        for(r++; r ; r -= r & -r){
            ret += bit[r];
        }
        return ret;
    }
    node sum(int l, int r){
        return sum(r) - sum(l-1);
    }
    void add(int idx, node delta){
        for(idx++; idx < n; idx += idx & -idx){
            bit[idx] += delta;
        }
    }

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

    node query(int u){
        return sum(u);
    }
}bit;
int n, m; vi a;

int ff(int val){
    int lo = 0, hi = n-1;
    // return first spot >= val
    while(lo <= hi){
        int mid = (lo + hi)/2;
        if(bit.query(mid) >= val){
            hi = mid - 1;
        }
        else{
            lo = mid + 1;
        }
    }
    return lo;
}
void init(){
    cin >> n >> m;
    a.resize(n); 
    bit.init(n);
    F0R(i, n){
        cin >> a[i];
    }
    sort(all(a));
    F0R(i, n){
        bit.update(i, i, a[i]);
    }
}

void query(){
    char t; 
    cin >> t;
    if(t=='F'){
        int c, h;cin >> c >> h;
        int p1 = ff(h);
        if(p1 + c >= n){
            bit.update(p1, n-1, 1); 
            return;
        }

        int mx = bit.query(p1 + c - 1);
        int p2 = ff(mx + 1);
        p2--;
        int p3 = ff(mx);
        bit.update(p1, p3-1, 1);
        int amt = p3-p1;
        bit.update(p2-(c-amt)+1, p2, 1);
    }
    else{
        int l, r;
        cin >> l >> r;
        cout << ff(r+1)-ff(l) << nl;
    }
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    init();
    F0R(i, m) query();
}
/*
 * Stuff I should try:
 * Is it Monotonic? -> Binary Search
 * Is N small? -> Bitsets or other exponential
 * Edge Cases? Look Carefully 
 * Can't figure anything out? Brute Force and see patterns
 * Try to prove greedies
 * Prefix Sums, Amortization, DP, Data Structures
*/
# Verdict Execution time Memory Grader output
1 Correct 77 ms 2424 KB Output is correct
2 Correct 125 ms 2808 KB Output is correct
3 Correct 47 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 53 ms 1400 KB Output is correct
6 Correct 61 ms 1656 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 30 ms 1184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 1656 KB Output is correct
2 Correct 67 ms 1784 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 40 ms 1400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 1912 KB Output is correct
2 Correct 71 ms 1776 KB Output is correct
3 Correct 7 ms 512 KB Output is correct
4 Correct 63 ms 1912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 2040 KB Output is correct
2 Correct 109 ms 2424 KB Output is correct
3 Correct 20 ms 896 KB Output is correct
4 Correct 43 ms 2424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 2040 KB Output is correct
2 Correct 111 ms 2552 KB Output is correct
3 Correct 47 ms 2556 KB Output is correct
4 Correct 19 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 2168 KB Output is correct
2 Correct 79 ms 2424 KB Output is correct
3 Correct 49 ms 2680 KB Output is correct
4 Correct 19 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 2852 KB Output is correct
2 Correct 113 ms 2296 KB Output is correct
3 Correct 34 ms 1912 KB Output is correct
4 Correct 77 ms 2244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 2556 KB Output is correct
2 Correct 123 ms 2808 KB Output is correct
3 Correct 122 ms 2936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 3448 KB Output is correct