# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
854619 | anarch_y | Monkey and Apple-trees (IZhO12_apple) | C++17 | 325 ms | 203080 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
#define pb push_back
#define int long long
const int maxn = 1e9;
struct node{
int value, lzSet;
node *left, *right;
node(int v){
value = v; lzSet = -1;
left = right = nullptr;
}
};
struct DynamicTree{
int len;
node *root;
DynamicTree(int N){
int p = ceil(log2(N));
len = 1<<p;
root = new node(0);
}
void pushup(node *k){
int sum = 0;
if(k->left != nullptr) sum += k->left->value;
if(k->right != nullptr) sum += k->right->value;
k->value = sum;
}
void pushdown(node *k, int x, int y){
int v = k->lzSet;
int d = (x+y)/2;
if(v != -1){
if(k->left == nullptr) k->left = new node(0);
if(k->right == nullptr) k->right = new node(0);
k->left->lzSet = v;
k->right->lzSet = v;
k->left->value = (d-x+1)*v;
k->right->value = (y-d)*v;
k->lzSet = -1;
}
}
void update(int a, int b, int v, node *k, int x, int y){
if(a<=x and y<=b){
k->value = (y-x+1)*v;
k->lzSet = v;
return;
}
pushdown(k, x, y);
int d = (x+y)/2;
if(d>=a and x<=b){
if(k->left == nullptr) k->left = new node(0);
update(a, b, v, k->left, x, d);
}
if(y>=a and d+1<=b){
if(k->right == nullptr) k->right = new node(0);
update(a, b, v, k->right, d+1, y);
}
pushup(k);
}
void update(int a, int b, int v){
update(a, b, v, root, 0, len-1);
}
int query(int a, int b, node *k, int x, int y){
if(k == nullptr) return 0LL;
if(b<x or a>y) return 0LL;
if(a<=x and y<=b) return k->value;
pushdown(k, x, y);
int d = (x+y)/2;
int s1 = query(a, b, k->left, x, d);
int s2 = query(a, b, k->right, d+1, y);
return s1+s2;
}
int query(int a, int b){
return query(a, b, root, 0, len-1);
}
};
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int M; cin >> M;
DynamicTree dseg(maxn);
int C = 0;
while(M--){
int op; cin >> op;
if(op==2){
int a, b; cin >> a >> b; a--; b--;
dseg.update(a+C, b+C, 1);
}
else{
int a, b; cin >> a >> b; a--; b--;
int ans = dseg.query(a+C, b+C);
C = ans;
cout << ans << "\n";
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |