Submission #766110

# Submission time Handle Problem Language Result Execution time Memory
766110 2023-06-25T10:22:11 Z mgl_diamond Growing Trees (BOI11_grow) C++17
100 / 100
53 ms 1612 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;

#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define fi first
#define se second

void setIO(string name) {
  if (!name.empty()) {
    freopen((name + ".in").c_str(), "r", stdin);
    freopen((name + ".out").c_str(), "w", stdout);
  }
}

const int N = 1e5+5;
const int LOG = 17;

int n, m, a[N], bit[N];

void modify(int i, int v) {
  for(; i<=n; i+=i&-i) bit[i] += v;
}

int query(int i) {
  int ans = 0;
  for(; i>=1; i-=i&-i) ans += bit[i];
  return ans;
}

void inc(int l, int r, int val) {
  modify(l, val);
  modify(r+1, -val);
}

int lst(int val) {
  int cur = 0, till = 0;
  for(int i=LOG-1; i>=0; --i) {
    if (cur+(1<<i) <= n && till+bit[cur+(1<<i)] <= val) {
      cur += 1 << i;
      till += bit[cur];
    }
  }
  return cur;
}

void solve () {
  cin >> n >> m;

  foru(i, 1, n) {
    cin >> a[i];
  }
  sort(a+1, a+n+1);
  foru(i, 1, n) {
    modify(i, a[i] - a[i-1]);
  }

  foru(i, 1, m) {
    char cmd;
    cin >> cmd;

    if (cmd == 'F') {
      int c, h;
      cin >> c >> h;
      int lb = lst(h-1)+1, alpha = min(n, lb+c-1);
      int mx_value = query(alpha);
      int rb = lst(mx_value), rx = lst(mx_value-1);
      inc(lb, rx, 1);
      int lx = max(rx+1, rb - (c - (rx-lb+1)) + 1);
      inc(lx, rb, 1);
    } 
    else {
      int mn, mx;
      cin >> mn >> mx;
      cout << lst(mx) - lst(mn-1) << "\n";
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  setIO("");
  solve();
}

Compilation message

grow.cpp: In function 'void setIO(std::string)':
grow.cpp:14:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |     freopen((name + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     freopen((name + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 38 ms 1132 KB Output is correct
2 Correct 45 ms 1080 KB Output is correct
3 Correct 37 ms 956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 20 ms 568 KB Output is correct
6 Correct 28 ms 712 KB Output is correct
7 Correct 3 ms 340 KB Output is correct
8 Correct 19 ms 540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 652 KB Output is correct
2 Correct 25 ms 608 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 25 ms 612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 712 KB Output is correct
2 Correct 30 ms 644 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 25 ms 668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 992 KB Output is correct
2 Correct 51 ms 1048 KB Output is correct
3 Correct 12 ms 468 KB Output is correct
4 Correct 32 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 1000 KB Output is correct
2 Correct 46 ms 1028 KB Output is correct
3 Correct 32 ms 852 KB Output is correct
4 Correct 8 ms 540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 1148 KB Output is correct
2 Correct 33 ms 1152 KB Output is correct
3 Correct 35 ms 864 KB Output is correct
4 Correct 8 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 1100 KB Output is correct
2 Correct 49 ms 1080 KB Output is correct
3 Correct 20 ms 1104 KB Output is correct
4 Correct 37 ms 1060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 1232 KB Output is correct
2 Correct 42 ms 1080 KB Output is correct
3 Correct 53 ms 1100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 1612 KB Output is correct