# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
646159 | QuinnG | 원숭이와 사과 나무 (IZhO12_apple) | C++14 | 264 ms | 262144 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
struct SegTree{
ll sum=0, lazySum=0;
SegTree *left, *right;
ll L,R;
SegTree(ll l, ll r){
// delete this to make implicit
// if(l!=r){
// int mid = (l+r)/2;
// left = new SegTree(l,mid);
// right = new SegTree(mid+1,r);
// }
L = l;
R = r;
left = nullptr;
right = nullptr;
}
ll getValue(){
return lazySum*(R-L+1) + sum;
}
void check(){
if(left==nullptr){
ll mid = (L+R)/2;
left = new SegTree(L,mid);
right = new SegTree(mid+1,R);
}
}
void prop(){
check();
left->lazySum += lazySum;
right->lazySum += lazySum;
lazySum = 0;
sum = left->getValue() + right->getValue();
}
// range sum
ll query(ll l, ll r){
// cout<<l<<" "<<r<<" "<<L<<" "<<R<<" "<<sum<<" "<<lazySum<<" "<<getValue()<<endl;
if(l<=L && r>=R) return getValue();
if(l>R || r<L) return 0;
prop();
return left->query(l,r) + right->query(l,r);
}
// range add
// void update(int l, int r, int x){
// if(l<=L && r>=R) lazySum += x;
// else if(l>R || r<L) return;
// else {
// prop();
// left->update(l,r,x);
// right->update(l,r,x);
// sum = left->getValue()+right->getValue();
// }
// }
// range set
void set(ll l, ll r, ll x){
if(l<=L && r>=R){
sum = 0;
lazySum = x;
ll mid = (L+R)/2;
left = new SegTree(L,mid);
right = new SegTree(mid+1,R);
}else if(l>R || r<L){
return;
}else{
prop();
left->set(l,r,x);
right->set(l,r,x);
sum = left->getValue()+right->getValue();
}
}
};
int main(){
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int m;cin>>m;
SegTree tree(-2e9,2e9);
ll c = 0;
for(int i=0;i<m;i++){
ll type,l,r;cin>>type>>l>>r;
// cout<<type<<endl;
if(type==1){
cout<<tree.query(l+c,r+c)<<endl;
c = tree.query(l+c,r+c);
}else{
tree.set(l+c,r+c,1);
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |