Submission #711045

# Submission time Handle Problem Language Result Execution time Memory
711045 2023-03-16T07:37:15 Z swagchicken Growing Trees (BOI11_grow) C++14
100 / 100
243 ms 16400 KB
#include <iostream>
#include <tuple>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <random>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <cctype>
#include <climits>
#include <chrono>
 
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pii;
typedef pair < pair < int, int >, int > piii;
typedef pair < pair <int, int > , pair <int, int> > piiii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
 
#define FOR(i,a,b) for(int i = a; i < b; i ++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i --)
#define all(a) a.begin(), a.end()
#define endl '\n';
#define sz(x) (int)(x).size()
 
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
 
template <typename T>
void pr(vector<T> &v) {
    FOR(i, 0, sz(v)) cout << v[i] << " ";
    cout << endl;
}
template <typename T>
void pr(vector<vector<T> > &v) {
    FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
    cin >> x;
}
template <typename T>
void re(vector<T> &a) {
    FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
    re(first);
    re(rest...);
}
template <typename T>
void pr(T x) {
    cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
    cout << first << " ";
    pr(rest...);
    cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
    cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}
 
const ll MOD  = 1000000007;
#define inf 1e18;
#define INF INT_MAX

long double PI = 4*atan(1);
long double eps = 1e-12;


template<class T>
class LazySegmentTree {                              
private:
  int n;                                        
  vector<T> A, stsum, stmin, stmax, lazy;        
  T ld = 0;        // lazy default value, -1 usually, but customize                

  int l(int p) { return  2*p; }                
  int r(int p) { return 2*p+1; }       

  void build(int p, int L, int R) {             
    if (L == R) {
      stsum[p] = A[L];      
      stmin[p] = A[L];
      stmax[p] = A[L];  
    }                    
    else {
      int m = (L+R)/2;
      build(l(p), L  , m);
      build(r(p), m+1, R);
      stsum[p] = stsum[l(p)] + stsum[r(p)];
      stmin[p] = min(stmin[l(p)], stmin[r(p)]);
      stmax[p] = max(stmax[l(p)], stmax[r(p)]);
    }
  }      


  void propagate(int p, int L, int R) {
    if (lazy[p] != ld) {                         // has a lazy flag
      stsum[p] += (R - L + 1) * lazy[p];  
      stmin[p] += lazy[p];
      stmax[p] += lazy[p];                      
      if (L != R) {                               // not a leaf
        lazy[l(p)] += lazy[p];      
        lazy[r(p)] += lazy[p];       // propagate downwards
      }           
      lazy[p] = ld;                              // erase lazy flag
    }
  }

  T query(int p, int L, int R, int ql, int qr) {   
    propagate(p, L, R);                          // lazy propagation
    if (qr < L || R < ql) return 0;                    
    if ((ql <= L) && (R <= qr)) return stsum[p];
    int m = (L+R)/2;
    return query(l(p), L  , m, ql, qr) + query(r(p), m+1, R, ql, qr);
  }

  // furthest thing left that is greater than or equal to val
  T query_lft(int p, int L, int R, T val) {   
    propagate(p, L, R);                          // lazy propagation
    if(stmax[p] < val) return 1e9;
    if(L == R) {
      if(stmax[p] >= val) return L;
      return 1e9;
    }
    int m = (L+R)/2;
    int idx1 = query_lft(l(p), L  , m, val);
    if(idx1 == 1e9) {
      idx1 = query_lft(r(p), m+1, R, val);
    }
    return idx1;
  }

   // furthest thing right that is less than or equal to val
  T query_rgt(int p, int L, int R, T val) {   
    propagate(p, L, R);                          // lazy propagation
    if(stmin[p] > val) return -1e9;
    if(L == R) {
      if(stmin[p] <= val) return L;
      return -1e9;
    }
    int m = (L+R)/2;
    int idx1 = query_rgt(r(p), m+1, R, val);
    if(idx1 == -1e9) {
      idx1 = query_rgt(l(p), L, m, val);
    }

    return idx1;
  }



  void update(int p, int L, int R, int ul, int ur, T val) { 
    propagate(p, L, R);                          // lazy propagation
    if(ur < L || R < ul) return;
    if ((ul <= L) && (R <= ur)) {                  
      lazy[p] += val;                            
      propagate(p, L, R);                        // lazy propagation
    }
    else {
      int m = (L+R)/2;
      update(l(p), L  , m, ul, ur, val);
      update(r(p), m+1, R, ul, ur, val);
      stsum[p] = stsum[l(p)] + stsum[r(p)];
      stmin[p] = min(stmin[l(p)], stmin[r(p)]);
      stmax[p] = max(stmax[l(p)], stmax[r(p)]);
    }
  }

public:
  LazySegmentTree(int sz) : n(sz), stsum(4*n), stmin(4*n), stmax(4*n){
    lazy.resize(4*n, ld);
  }

  LazySegmentTree(const vector<T> &initialA) : LazySegmentTree((int)initialA.size()) {
    A = initialA;
    build(1, 0, n-1);
  }

  void update(int i, int j, T val) { update(1, 0, n-1, i, j, val); }

  T query(int i, int j) { return query(1, 0, n-1, i, j); }
  T query_lft(T val) { return query_lft(1, 0, n-1, val); }
  T query_rgt(T val) { return query_rgt(1, 0, n-1, val); }
};


int main() {
    //auto start = chrono::high_resolution_clock::now();
    ios_base::sync_with_stdio(0);cin.tie(0);
    //ofstream cout("output.txt");
    //ifstream cin("input.txt");
    #ifdef DEBUG
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif  

    
    
    int n,m; cin >> n >> m;
    vector<ll> A(n); re(A);

    sort(all(A));
  //  pr(A);
    LazySegmentTree<ll> st(A);

      

    ll ans = 0;

    FOR(i,0,m) {
      char c; cin >> c;
      
      if(c == 'F') {
        int h,c; cin >> c >> h;
        int v = st.query_lft(h);

   //     cout << "v: " << v << endl;

        if(v + c - 1 >= n-1) {
          st.update(v, n-1, 1);
        } else {
          int mh = st.query(v+c-1, v+c-1);
          int lft = st.query_lft(mh);
          int rgt = st.query_rgt(mh);

        //  cout << mh << " " << lft << " " << rgt << endl;

          if(v <= lft - 1) {
            st.update(v, lft-1, 1);
      //      cout << "updating: " << v << " " << lft-1 << endl;
          }
          st.update(rgt - (v + c - 1 - lft), rgt, 1);
     //     cout << "updating: " << rgt - (v + c - 1 - lft) << " " << rgt << endl;

        }
      } else {
        int a,b; cin >> a >> b;
        int v1 = st.query_lft(a);
        int v2 = st.query_rgt(b);
        if(abs(v2 - v1 + 1) < 1e8) {
            cout << v2 - v1 + 1 << endl;
        } else {
          cout << 0 << endl;
        }
        
      } 
    }

    //auto stop = chrono::high_resolution_clock::now();
    //auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
    //cout << duration.count() << endl;
    //cin.close();
    //cout.close();
}

Compilation message

grow.cpp: In function 'int main()':
grow.cpp:233:8: warning: unused variable 'ans' [-Wunused-variable]
  233 |     ll ans = 0;
      |        ^~~
# Verdict Execution time Memory Grader output
1 Correct 146 ms 14028 KB Output is correct
2 Correct 174 ms 14112 KB Output is correct
3 Correct 110 ms 13524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 3 ms 596 KB Output is correct
4 Correct 2 ms 480 KB Output is correct
5 Correct 71 ms 2052 KB Output is correct
6 Correct 61 ms 2232 KB Output is correct
7 Correct 6 ms 1108 KB Output is correct
8 Correct 21 ms 1776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 2508 KB Output is correct
2 Correct 72 ms 2596 KB Output is correct
3 Correct 2 ms 1236 KB Output is correct
4 Correct 30 ms 2224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 2756 KB Output is correct
2 Correct 72 ms 2620 KB Output is correct
3 Correct 13 ms 1364 KB Output is correct
4 Correct 63 ms 2688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 10620 KB Output is correct
2 Correct 179 ms 11852 KB Output is correct
3 Correct 24 ms 3156 KB Output is correct
4 Correct 91 ms 11756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 11976 KB Output is correct
2 Correct 180 ms 12748 KB Output is correct
3 Correct 108 ms 11844 KB Output is correct
4 Correct 21 ms 3148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 12764 KB Output is correct
2 Correct 133 ms 13980 KB Output is correct
3 Correct 110 ms 13260 KB Output is correct
4 Correct 20 ms 3112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 215 ms 14180 KB Output is correct
2 Correct 171 ms 12364 KB Output is correct
3 Correct 40 ms 15132 KB Output is correct
4 Correct 74 ms 14204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 13880 KB Output is correct
2 Correct 162 ms 14240 KB Output is correct
3 Correct 243 ms 16052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 16400 KB Output is correct