답안 #848961

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
848961 2023-09-13T18:35:13 Z Ahmed_Solyman 원숭이와 사과 나무 (IZhO12_apple) C++14
100 / 100
361 ms 186708 KB
/*
In the name of Allah
made by: Ahmed_Solyman
*/
#include <bits/stdc++.h>
#include <ext/rope>

using namespace std;
using namespace __gnu_cxx;
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-O1")
//-------------------------------------------------------------//
typedef long long ll;
typedef unsigned long long ull;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define lb lower_bound
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sum_to(n) (n*(n+1))/2
#define pb push_back
#define pf push_front
#define fil(arr,x) memset(arr,x,sizeof(arr))
#define REP(i,a,b) for (int i=a;i<=b;i++)
#define F first
#define S second
#define MP make_pair
const ll mod=1e9+7;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
//-------------------------------------------------------------//
ll lcm(ll a,ll b)
{
    return (max(a,b)/__gcd(a,b))*min(a,b);
}
void person_bool(bool x)
{
    cout<<(x?"YES":"NO")<<endl;
}
const int N=123456;
struct node{
    int sum,lazy,l,r,left,right;
    node() : sum(0), lazy(0), left(-1), right(-1) {}
}segtree[64*N];
int cnt=2;
void push_lazy(int node){
    if(segtree[node].lazy){
        segtree[node].sum=segtree[node].r-segtree[node].l+1;
        int mid=(segtree[node].l+segtree[node].r)/2;
        if(segtree[node].left==-1){
            segtree[node].left=cnt;
            segtree[cnt].l=segtree[node].l;
            segtree[cnt++].r=mid;
        }
        if(segtree[node].right==-1){
            segtree[node].right=cnt;
            segtree[cnt].l=mid+1;
            segtree[cnt++].r=segtree[node].r;
        }
        segtree[segtree[node].left].lazy=segtree[segtree[node].right].lazy=1;
        segtree[node].lazy=0;
    }
}
void update(int node,int l,int r){
    push_lazy(node);
    if(segtree[node].l==l && segtree[node].r==r){
        segtree[node].lazy=1;
        push_lazy(node);
        return;
    }
    int mid=(segtree[node].l+segtree[node].r)/2;
    if(segtree[node].left==-1){
        segtree[node].left=cnt;
        segtree[cnt].l=segtree[node].l;
        segtree[cnt++].r=mid;
    }
    if(segtree[node].right==-1){
        segtree[node].right=cnt;
        segtree[cnt].l=mid+1;
        segtree[cnt++].r=segtree[node].r;
    }
    if(l>mid)update(segtree[node].right,l,r);
    else if(r<=mid)update(segtree[node].left,l,r);
    else {update(segtree[node].left,l,mid);update(segtree[node].right,mid+1,r);}
    push_lazy(segtree[node].left);
	push_lazy(segtree[node].right);
    segtree[node].sum=segtree[segtree[node].left].sum+segtree[segtree[node].right].sum;
}
int query(int node,int l,int r){
    push_lazy(node);
    if(segtree[node].l==l && segtree[node].r==r){
        return segtree[node].sum;
    }
    int mid=segtree[node].l+segtree[node].r>>1;
    if(segtree[node].left==-1){
        segtree[node].left=cnt;
        segtree[cnt].l=segtree[node].l;
        segtree[cnt++].r=mid;
    }
    if(segtree[node].right==-1){
        segtree[node].right=cnt;
        segtree[cnt].l=mid+1;
        segtree[cnt++].r=segtree[node].r;
    }
    if(l>mid)return query(segtree[node].right,l,r);
    else if(r<=mid)return query(segtree[node].left,l,r);
    else return query(segtree[node].left,l,mid)+query(segtree[node].right,mid+1,r);
}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    segtree[1].l=1;
    segtree[1].r=1e9;
    int q;cin>>q;
    int c=0;
    while(q--){
        int t,l,r;cin>>t>>l>>r;
        if(t==1){
            c=query(1,l+c,r+c);
            cout<<c<<endl;
        }
        else{
            update(1,l+c,r+c);
        }
    }
    return 0;
}

Compilation message

apple.cpp: In function 'int query(int, int, int)':
apple.cpp:95:28: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   95 |     int mid=segtree[node].l+segtree[node].r>>1;
      |             ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 185916 KB Output is correct
2 Correct 30 ms 185948 KB Output is correct
3 Correct 31 ms 185940 KB Output is correct
4 Correct 46 ms 185948 KB Output is correct
5 Correct 51 ms 185748 KB Output is correct
6 Correct 54 ms 185940 KB Output is correct
7 Correct 51 ms 185980 KB Output is correct
8 Correct 157 ms 186136 KB Output is correct
9 Correct 304 ms 186296 KB Output is correct
10 Correct 293 ms 186236 KB Output is correct
11 Correct 298 ms 186284 KB Output is correct
12 Correct 307 ms 186196 KB Output is correct
13 Correct 290 ms 186708 KB Output is correct
14 Correct 322 ms 186572 KB Output is correct
15 Correct 355 ms 186356 KB Output is correct
16 Correct 361 ms 186692 KB Output is correct
17 Correct 303 ms 186452 KB Output is correct
18 Correct 298 ms 186452 KB Output is correct
19 Correct 347 ms 186236 KB Output is correct
20 Correct 357 ms 186708 KB Output is correct