답안 #957954

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
957954 2024-04-04T14:14:18 Z bmh123456789asdf 원숭이와 사과 나무 (IZhO12_apple) C++14
0 / 100
66 ms 75664 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#define int long long
using namespace std;
const int N = 12345;



struct Node {
    int sum, lazy, tl, tr, l, r;
    Node() : sum(0), lazy(0), l(-1), r(-1) {}
};
Node segtree[64 * N];
int cnt = 1;

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

void update(int node,int l,int r)
{
    pushlazy(node);
    if(l == segtree[node].tl && r == segtree[node].tr)
    {
        segtree[node].lazy = 1;
        pushlazy(node);
    }
    else
    {
        int mid = (segtree[node].tr + segtree[node].tl) / 2;
        if(segtree[node].l == -1)
        {
            segtree[node].l = ++cnt;
            segtree[cnt].tl = segtree[node].tl;
            segtree[cnt].tr = mid;
        }
        if(segtree[node].r == -1)
        {
            segtree[node].r = ++cnt;
            segtree[cnt].tl = mid + 1;
            segtree[cnt].tr = segtree[node].tr;
        }

        if(l > mid) update(segtree[node].r, l, r);
        else if(r <= mid) update(segtree[node].l, l, r);
        else
        {
            update(segtree[node].l, l, mid);
            update(segtree[node].r, mid + 1, r);
        }
        pushlazy(segtree[node].l);
        pushlazy(segtree[node].r);

        segtree[node].sum = segtree[segtree[node].l].sum + segtree[segtree[node].r].sum;
    }
}

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

        if(l > mid) return query(segtree[node].r, l, r);
        else if(r <= mid) return query(segtree[node].l, l, r);
        else return query(segtree[node].l, l, mid) + query(segtree[node].r, mid + 1, r);
    }
}


int32_t main()
{
//    freopen("f.in", "r" ,stdin);
//    freopen("f.out", "w" ,stdout);

    iostream::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int m, d, x, y, c = 0;

    segtree[1].sum = 0;
	segtree[1].lazy = 0;
	segtree[1].tl = 1;
	segtree[1].tr = 1e9;

    cin >> m;
    for(int i = 1; i <= m; i++)
    {
        cin >> d >> x >> y;
        if(d == 1)
        {
            c = query(1, x + c, y + c);
            cout << c <<'\n';
        }
        else update(1, x + c, y + c);

    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 37468 KB Output is correct
2 Correct 6 ms 37468 KB Output is correct
3 Correct 8 ms 37464 KB Output is correct
4 Correct 19 ms 37776 KB Output is correct
5 Correct 16 ms 37464 KB Output is correct
6 Correct 16 ms 37468 KB Output is correct
7 Correct 18 ms 37468 KB Output is correct
8 Runtime error 66 ms 75664 KB Execution killed with signal 11
9 Halted 0 ms 0 KB -