CSES - E4590 2018 6 - Results
Submission details
Task:Maximums
Sender:lautat
Submission time:2018-10-20 15:38:02 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.01 sdetails
#2ACCEPTED0.07 sdetails
#3ACCEPTED0.07 sdetails
#4ACCEPTED0.08 sdetails
#5ACCEPTED0.07 sdetails
#6ACCEPTED0.08 sdetails
#7ACCEPTED0.07 sdetails
#8ACCEPTED0.08 sdetails
#9ACCEPTED0.08 sdetails
#10ACCEPTED0.07 sdetails
#11ACCEPTED0.07 sdetails
#12ACCEPTED0.07 sdetails
#13ACCEPTED0.08 sdetails
#14ACCEPTED0.07 sdetails
#15ACCEPTED0.07 sdetails
#16ACCEPTED0.01 sdetails

Code

#include <algorithm>
#include <iostream>

using namespace std;

// 2 ** 60 - 1 (>= 10 ** 18)
#define MAX_NUMBER 1152921504606846975


struct Node {
    Node* left;
    Node* right;
    unsigned long long max_height;
};


void set_height(Node** tree, unsigned long long a, unsigned long long b,
                unsigned long long i, unsigned long long h) {
    unsigned long long mid = (a + b) / 2;

    if (*tree == NULL) {
        *tree = new Node;
        (*tree)->left = NULL;
        (*tree)->right = NULL;
        (*tree)->max_height = 0;
    }

    if (a == b) {
        (*tree)->max_height = h;
    } else {
        if (i <= mid) {
            set_height(&((*tree)->left), a, mid, i, h);
        } else {
            set_height(&((*tree)->right), mid + 1, b, i, h);
        }

        unsigned long long left_max = 0;
        unsigned long long right_max = 0;

        if ((*tree)->left != NULL) {
            left_max = (*tree)->left->max_height;
        }

        if ((*tree)->right != NULL) {
            right_max = (*tree)->right->max_height;
        }

        (*tree)->max_height = max(left_max, right_max);
    }
}


unsigned long long max_height(
        Node* tree, unsigned long long a, unsigned long long b,
        unsigned long long start, unsigned long long end) {
    if (tree == NULL || (b < start || end < a)) {
        return 0;
    } else if (start <= a && b <= end) {
        return tree->max_height;
    } else {
        unsigned long long mid = (a + b) / 2;
        unsigned long long max_left = max_height(
            tree->left, a, mid, start, end);
        unsigned long long max_right = max_height(
            tree->right, mid + 1, b, start, end);
        return max(max_left, max_right);
    }
}


void free_tree(Node** tree) {
    if (*tree != NULL) {
        free_tree(&((*tree)->left));
        free_tree(&((*tree)->right));

        delete *tree;
        *tree = NULL;
    }
}


int main() {
    int q;
    cin >> q;

    Node* street = NULL;

    for (int i = 0; i < q; i++) {
        unsigned long long t, a, b;
        cin >> t >> a >> b;

        if (t == 0) {
            set_height(&street, 0, MAX_NUMBER, a, b);
        } else {
            cout << max_height(street, 0, MAX_NUMBER, a, b) << endl;
        }
    }

    free_tree(&street);

    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
14
0 1 10
0 2 20
0 3 10
0 5 30
...

correct output
30
900
10
10
30
...

user output
30
900
10
10
30
...

Test 2

Verdict: ACCEPTED

input
10000
1 32165844002674501 9935522969...

correct output
0
0
0
0
11234759
...

user output
0
0
0
0
11234759
...

Test 3

Verdict: ACCEPTED

input
10000
0 506000858779722442 955856127
0 365677212339294554 191262305
0 80298683202610332 731026780
1 251018061710642286 402321118...

correct output
191262305
955856127
0
0
974767945
...

user output
191262305
955856127
0
0
974767945
...

Test 4

Verdict: ACCEPTED

input
10000
1 638126273190686812 950389431...

correct output
0
0
0
0
282004160
...

user output
0
0
0
0
282004160
...

Test 5

Verdict: ACCEPTED

input
10000
1 111025053997747742 442310357...

correct output
0
0
0
0
904661686
...

user output
0
0
0
0
904661686
...

Test 6

Verdict: ACCEPTED

input
10000
1 496705609862664992 581730224...

correct output
0
0
0
0
405817260
...

user output
0
0
0
0
405817260
...

Test 7

Verdict: ACCEPTED

input
10000
0 72593517785362728 681384442
0 362460280234102656 165260530
1 151141022927318138 187584320...

correct output
0
111311568
685553350
681384442
956755171
...

user output
0
111311568
685553350
681384442
956755171
...

Test 8

Verdict: ACCEPTED

input
10000
1 622606698099284018 987544827...

correct output
0
0
807751104
849715958
0
...

user output
0
0
807751104
849715958
0
...

Test 9

Verdict: ACCEPTED

input
10000
1 69238043798947793 4706690300...

correct output
0
0
795546841
0
795546841
...

user output
0
0
795546841
0
795546841
...

Test 10

Verdict: ACCEPTED

input
10000
1 119685018556413794 615398329...

correct output
0
0
0
870853046
0
...

user output
0
0
0
870853046
0
...

Test 11

Verdict: ACCEPTED

input
10000
1 348122926527894017 731130196...

correct output
0
0
0
0
0
...

user output
0
0
0
0
0
...

Test 12

Verdict: ACCEPTED

input
10000
1 355395486708916153 967691841...

correct output
0
0
0
0
680197744307649083
...

user output
0
0
0
0
680197744307649083
...

Test 13

Verdict: ACCEPTED

input
10000
1 55431229101938445 9696845731...

correct output
0
0
971464128922141096
233235584702314016
971464128922141096
...

user output
0
0
971464128922141096
233235584702314016
971464128922141096
...

Test 14

Verdict: ACCEPTED

input
10000
0 207801709118991370 174237375...

correct output
0
689368052041962301
174237375167386391
630879973186342989
0
...

user output
0
689368052041962301
174237375167386391
630879973186342989
0
...

Test 15

Verdict: ACCEPTED

input
10000
1 281035836234805473 310949475...

correct output
0
0
539398684288713903
0
539398684288713903
...

user output
0
0
539398684288713903
0
539398684288713903
...

Test 16

Verdict: ACCEPTED

input
10
0 100 1000
0 200 1001
1 100 200
1 101 199
...

correct output
1001
0
200
250
200

user output
1001
0
200
250
200