답안 #967820

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
967820 2024-04-23T00:38:35 Z irmuun 원숭이와 사과 나무 (IZhO12_apple) C++17
100 / 100
274 ms 190952 KB
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()

const ll N=8e6;

struct Node{
    int sum,lazy,tl,tr,l,r;
    Node() : sum(0), lazy(0), l(-1), r(-1){}
};

Node sg[N];

int cnt=2;

void push(int node){
    if(sg[node].lazy==1){
        sg[node].sum=sg[node].tr-sg[node].tl+1;
        int mid=(sg[node].tl+sg[node].tr)/2;
        if(sg[node].l==-1){
            sg[node].l=cnt++;
            sg[sg[node].l].tl=sg[node].tl;
            sg[sg[node].l].tr=mid;
        }
        if(sg[node].r==-1){
            sg[node].r=cnt++;
            sg[sg[node].r].tl=mid+1;
            sg[sg[node].r].tr=sg[node].tr;
        }
        sg[sg[node].l].lazy=1;
        sg[sg[node].r].lazy=1;
        sg[node].lazy=0;
    }
}

int query(int node,int l,int r){
    push(node);
    if(sg[node].tl==l&&sg[node].tr==r){
        return sg[node].sum;
    }
    int mid=(sg[node].tl+sg[node].tr)/2;
    if(sg[node].l==-1){
        sg[node].l=cnt++;
        sg[sg[node].l].tl=sg[node].tl;
        sg[sg[node].l].tr=mid;
    }
    if(sg[node].r==-1){
        sg[node].r=cnt++;
        sg[sg[node].r].tl=mid+1;
        sg[sg[node].r].tr=sg[node].tr;
    }
    if(r<=mid){
        return query(sg[node].l,l,r);
    }
    if(l>mid){
        return query(sg[node].r,l,r);
    }
    return query(sg[node].l,l,mid)+query(sg[node].r,mid+1,r);
}

void update(int node,int l,int r){
    push(node);
    if(sg[node].tl==l&&sg[node].tr==r){
        sg[node].lazy=1;
        push(node);
        return;
    }
    int mid=(sg[node].tl+sg[node].tr)/2;
    if(sg[node].l==-1){
        sg[node].l=cnt++;
        sg[sg[node].l].tl=sg[node].tl;
        sg[sg[node].l].tr=mid;
    }
    if(sg[node].r==-1){
        sg[node].r=cnt++;
        sg[sg[node].r].tl=mid+1;
        sg[sg[node].r].tr=sg[node].tr;
    }
    if(r<=mid){
        update(sg[node].l,l,r);
    }
    else if(l>mid){
        update(sg[node].r,l,r);
    }
    else{
        update(sg[node].l,l,mid);
        update(sg[node].r,mid+1,r);
    }
    push(sg[node].l);
    push(sg[node].r);
    sg[node].sum=sg[sg[node].l].sum+sg[sg[node].r].sum;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int m;
    cin>>m;
    sg[1].sum=0;
    sg[1].lazy=0;
    sg[1].tl=1;
    sg[1].tr=1e9;
    int c=0;
    while(m--){
        int t,x,y;
        cin>>t>>x>>y;
        if(t==1){
            c=query(1,x+c,y+c);
            cout<<c<<"\n";
        }
        else{
            update(1,x+c,y+c);
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 188244 KB Output is correct
2 Correct 35 ms 188212 KB Output is correct
3 Correct 33 ms 188072 KB Output is correct
4 Correct 42 ms 188116 KB Output is correct
5 Correct 43 ms 188248 KB Output is correct
6 Correct 43 ms 188244 KB Output is correct
7 Correct 43 ms 188244 KB Output is correct
8 Correct 109 ms 188244 KB Output is correct
9 Correct 202 ms 188496 KB Output is correct
10 Correct 197 ms 188508 KB Output is correct
11 Correct 203 ms 188648 KB Output is correct
12 Correct 206 ms 188600 KB Output is correct
13 Correct 181 ms 188776 KB Output is correct
14 Correct 175 ms 188756 KB Output is correct
15 Correct 247 ms 190936 KB Output is correct
16 Correct 274 ms 190720 KB Output is correct
17 Correct 189 ms 190952 KB Output is correct
18 Correct 179 ms 190800 KB Output is correct
19 Correct 274 ms 190772 KB Output is correct
20 Correct 255 ms 190796 KB Output is correct