# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
646165 | QuinnG | 원숭이와 사과 나무 (IZhO12_apple) | C++14 | 522 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, lazySet=0;
bool needSet = false;
SegTree *left, *right;
int L,R;
SegTree(int l, int r){
// 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;
}
void check(){
if(left==nullptr){
int mid = (L+R)/2;
left = new SegTree(L,mid);
right = new SegTree(mid+1,R);
}
}
ll getValue(){
if(needSet){
return (lazySum+lazySet)*(R-L+1);
}else{
return lazySum*(R-L+1) + sum;
}
}
void prop(){
check();
if(needSet){
left->lazySet = lazySet;
left->needSet = true;
left->lazySum = 0;
right->lazySet = lazySet;
right->needSet = true;
right->lazySum = 0;
}
left->lazySum += lazySum;
right->lazySum += lazySum;
lazySum = 0;
lazySet = 0;
needSet = false;
sum = left->getValue() + right->getValue();
}
// range sum
ll query(int l, int r){
// cout<<l<<" "<<r<<" "<<L<<" "<<R<<" "<<sum<<" "<<lazySum<<" "<<lazySet<<" "<<needSet<<" "<<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(int l, int r, int x){
if(l<=L && r>=R){
// cout<<"SET "<<l<<" "<<r<<" "<<L<<" "<<R<<" "<<sum<<" "<<lazySum<<" "<<lazySet<<" "<<needSet<<" "<<getValue()<<endl;
lazySet = x;
lazySum = 0;
needSet = true;
// cout<<"SET "<<l<<" "<<r<<" "<<L<<" "<<R<<" "<<sum<<" "<<lazySum<<" "<<lazySet<<" "<<needSet<<" "<<getValue()<<endl;
}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(0,1e9);
int c = 0;
for(int i=0;i<m;i++){
int 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... |