Submission #959680

# Submission time Handle Problem Language Result Execution time Memory
959680 2024-04-08T18:29:31 Z vjudge1 Monkey and Apple-trees (IZhO12_apple) C++
0 / 100
459 ms 262144 KB
#include<bits/stdc++.h>
using namespace std;
const long long int MAX_N = 1000000000000;
const long long int NIL = -1;

class SparseSegmentTree{
  public:
  SparseSegmentTree(long long int number_of_datum){
    n = 1;
    while(n < number_of_datum) n *= 2;
    root = new Node(0, n);
  }
  void range_update(long long int s, long long int t){
    inner_add(s, t + 1, root);
  }
  long long int range_query(long long int s, long long int t){
    return inner_getsum(s, t + 1, root);
  }
  private:
  struct Node{
    long long int left_bound, right_bound;
    long long int dat, laz = NIL;
    Node *left_child, *right_child;
    Node(long long int lb, long long int rb){
      left_bound = lb;
      right_bound = rb;
    }
  };
  void inner_split(Node *node){
    if(node->left_child) return;
    long long int l = node->left_bound;
    long long int r = node->right_bound;
    if(l + 1 == r) return;
    long long int mid = (l + r) / 2;
    node->left_child = new Node(l, mid);
    node->right_child = new Node(mid, r);
  }
  void inner_prop(Node *node){
    inner_split(node);
    if(!node->left_child) return;
    if(node->laz == NIL) return;
    node->left_child->dat = node->laz / 2;
    node->right_child->dat = node->laz / 2;
    node->left_child->laz = node->laz / 2;
    node->right_child->laz = node->laz / 2;
    node->laz = NIL;
  }
  void inner_add(long long int a, long long int b, Node *node){
    long long int l = node->left_bound;
    long long int r = node->right_bound;
    if(b <= l || r <= a) return;
    if(a <= l && r <= b){
      node->dat = r - l;
      node->laz = r - l;
      return;
    }
    inner_prop(node);
    if(!node->left_child) return;
    inner_add(a, b, node->left_child);
    inner_add(a, b, node->right_child);
    node->dat = node->left_child->dat + node->right_child->dat;
  }
  long long int inner_getsum(long long int a, long long int b, Node *node){
    if(!node) return 0;
    long long int l = node->left_bound;
    long long int r = node->right_bound;
    if(b <= l || r <= a) return 0;
    if(a <= l && r <= b) return node->dat;
    inner_prop(node);
    return inner_getsum(a, b, node->left_child) + inner_getsum(a, b, node->right_child);
  }
  long long int n;
  Node *root;
};

int q;

int main(){
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cin >> q;
  SparseSegmentTree st(MAX_N);
  long long int C = 0;
  while(q--){
    int op;
    cin >> op;
    if(op == 2){
      long long int s, t;
      cin >> s >> t;
      s--;
      t--;
      st.range_update(s + C, t + C);
    }else{
      long long int s, t;
      cin >> s >> t;
      s--;
      t--;
      long long int res = st.range_query(s + C, t + C);
      cout << res << endl;
      C = res;
    }
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 500 KB Output is correct
4 Correct 17 ms 6244 KB Output is correct
5 Correct 23 ms 7508 KB Output is correct
6 Correct 18 ms 7260 KB Output is correct
7 Correct 23 ms 7516 KB Output is correct
8 Correct 133 ms 56680 KB Output is correct
9 Correct 295 ms 101532 KB Output is correct
10 Correct 327 ms 108368 KB Output is correct
11 Correct 320 ms 116928 KB Output is correct
12 Correct 336 ms 120124 KB Output is correct
13 Correct 285 ms 142728 KB Output is correct
14 Correct 313 ms 143644 KB Output is correct
15 Correct 459 ms 261204 KB Output is correct
16 Correct 407 ms 262144 KB Output is correct
17 Correct 289 ms 148576 KB Output is correct
18 Correct 271 ms 148564 KB Output is correct
19 Runtime error 432 ms 262144 KB Execution killed with signal 9
20 Halted 0 ms 0 KB -