답안 #525352

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
525352 2022-02-11T12:37:11 Z LucaIlie 원숭이와 사과 나무 (IZhO12_apple) C++17
0 / 100
0 ms 292 KB
#include <iostream>

#define MIN_P 1
#define MAX_P 1000000000

using namespace std;

struct DYN_AINT {
    struct node {
        int sum, lazy;
        node *left, *right;
    };

    node *getNode() {
        node* nod = new node;

        nod->sum = nod->lazy = 0;
        nod->left = nod->right = NULL;

        return nod;
    }

    node *root = getNode();

    void updateRec( node *nod, int l, int r, int lu, int ru, int x ) {
        int mid = (l + r) / 2;

        if ( nod->lazy ) {
            nod->sum = (nod->lazy * (r - l + 1));
            if ( l != r ) {
                if ( nod->left == NULL )
                    nod->left = getNode();
                nod->left->lazy = nod->lazy;
                if ( nod->right == NULL )
                    nod->right = getNode();
                nod->right->lazy = nod->lazy;
            }
            nod->lazy = 0;
        }

        if ( l > ru || r < lu )
            return;
        if ( lu <= l && r <= ru ) {
            nod->lazy = x;
            nod->sum = nod->lazy * (r - l + 1);
            if ( l != r ) {
                if ( nod->left == NULL )
                    nod->left = getNode();
                nod->left->lazy += nod->lazy;
                if ( nod->right == NULL )
                    nod->right = getNode();
                nod->right->lazy += nod->lazy;
            }
            nod->lazy = 0;

            return;
        }

        if ( nod->left == NULL )
            nod->left = getNode();
        if ( nod->right == NULL )
            nod->right = getNode();

        updateRec( nod->left, l, mid, lu, ru, x );
        updateRec( nod->right, mid + 1, r, lu, ru, x );
        nod->sum = nod->left->sum + nod->right->sum;
    }
    void update( int l, int r, int x ) {
        updateRec( root, MIN_P, MAX_P, l, r, x );
    }

    int queryRec( node *nod, int l, int r, int lq, int rq ) {
        int mid = (l + r) / 2;
        int sum;

        if ( nod->lazy ) {
            nod->sum = (nod->lazy * (r - l + 1));
            if ( l != r ) {
                if ( nod->left == NULL )
                    nod->left = getNode();
                nod->left->lazy = nod->lazy;
                if ( nod->right == NULL )
                    nod->right = getNode();
                nod->right->lazy = nod->lazy;
            }
            nod->lazy = 0;
        }


        if ( r < lq || l > rq )
            return 0;
        if ( lq <= l && r <= rq )
            return nod->sum;

        if ( nod->left == NULL )
            nod->left = getNode();
        if ( nod->right == NULL )
            nod->right = getNode();

        sum = queryRec( nod->left, l, mid, lq, rq ) + queryRec( nod->right, mid + 1, r, lq, rq );
        nod->sum = nod->left->sum + nod->right->sum;

        return sum;
    }
    int query( int l, int r ) {
        return queryRec( root, MIN_P, MAX_P, l, r );
    }
};

DYN_AINT apples;

int main() {
    int m, t, l, r, a, c;

    c = 0;
    cin >> m;
    while ( m-- ) {
        cin >> t >> l >> r;
        l += c;
        r += c;

        if ( t == 1 ) {
            a = apples.query( l, r );
            cout << a << "\n";
            c += a;
        } else
            apples.update( l, r, 1 );
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 292 KB Output isn't correct
3 Halted 0 ms 0 KB -