Submission #996221

# Submission time Handle Problem Language Result Execution time Memory
996221 2024-06-10T08:55:38 Z Alfraganus Wall (IOI14_wall) C++17
100 / 100
608 ms 58452 KB
#include "wall.h"
#include <bits/stdc++.h>
using namespace std;

#define print(a) for(auto x : a) cout << x << ' '; cout << endl;

struct SegTree {

    int size = 1;
    vector<int> mins, maxs;

    SegTree (int n){
        while(size < n)
            size <<= 1;
        mins.resize(2 * size, -1);
        maxs.resize(2 * size, -1);
    }

    void add(int l,  int r, int v, int x, int lx, int rx, int lastmin = -1, int lastmax = -1){
        // cout << l << ' ' << r << ' ' << v << ' ' << x << ' ' << lx << ' ' << rx << endl;
        if(maxs[x] != -1){
            if(maxs[x] < v){
                if(lastmin <= maxs[x]){
                    if(lastmax == -1)
                        lastmax = maxs[x];
                    else
                        lastmax = min(lastmax, maxs[x]);
                }
                else
                    lastmax = lastmin;
                maxs[x] = -1;
            }
        }
        if(mins[x] != -1){
            if(mins[x] >= v and lastmax == -1)
                return;
            lastmin = max(lastmin, mins[x]);
            if(lastmax != -1)
                lastmin = min(lastmin, lastmax);
            mins[x] = -1;
        }
        // cout << lastmin << ' ' << mins[x] << ' ' << lastmax << ' ' << maxs[x] << endl;
        if(l <= lx and rx <= r){
            mins[x] = max(mins[x], v);
            if(lastmax != -1){
                if(maxs[x] == -1)
                    maxs[x] = lastmax;
                else
                    maxs[x] = min(maxs[x], lastmax);
            }
            if(maxs[x] != -1)
                maxs[x] = max(maxs[x], mins[x]);
            return;
        }
        if(r <= lx or rx <= l){
            if(lastmin != -1){
                mins[x] = max(mins[x], lastmin);
                if(maxs[x] != -1)
                    maxs[x] = max(maxs[x], mins[x]);
            }
            if(lastmax != -1){
                if(maxs[x] == -1)
                    maxs[x] = lastmax;
                else
                    maxs[x] = min(maxs[x], lastmax);
                mins[x] = min(mins[x], maxs[x]);
            }
            return;
        }
        int mid = (lx + rx) >> 1;
        add(l, r, v, (x << 1) + 1, lx, mid, lastmin, lastmax);
        add(l, r, v, (x << 1) + 2, mid, rx, lastmin, lastmax);
    }

    void add(int l, int r, int v){
        add(l, r, v, 0, 0, size);
    }

    void remove(int l, int r, int v, int x, int lx, int rx, int lastmin = -1, int lastmax = -1){
        if(maxs[x] != -1){
            if(max(lastmin, maxs[x]) <= v)
                return;
            if(lastmax == -1)
                lastmax = maxs[x];
            else
                lastmax = min(lastmax, maxs[x]);
            lastmax = max(lastmax, lastmin);
            maxs[x] = -1;
        }
        if(mins[x] != -1){
            if(mins[x] > v){
                lastmin = max(lastmin, mins[x]);
                mins[x] = -1;
                if(lastmax != -1)
                    lastmin = min(lastmin, lastmax);
            }
        }
        if(l <= lx and rx <= r){
            if(maxs[x] == -1)
                maxs[x] = v;
            else
                maxs[x] = min(maxs[x], v);
            mins[x] = max(mins[x], lastmin);
            mins[x] = min(mins[x], maxs[x]);
            return;
        }
        if(rx <= l or r <= lx){
            if(lastmax != -1){
                if(maxs[x] == -1)
                    maxs[x] = lastmax;
                else
                    maxs[x] = min(maxs[x], lastmax);
                mins[x] = min(mins[x], maxs[x]);
            }
            if(lastmin != -1){
                mins[x] = max(mins[x], lastmin);
                if(maxs[x] != -1)
                    maxs[x] = max(maxs[x], mins[x]);
            }
            return;
        }
        int mid = (lx + rx) >> 1;
        remove(l, r, v, (x << 1) + 1, lx, mid, lastmin, lastmax);
        remove(l, r, v, (x << 1) + 2, mid, rx, lastmin, lastmax);
    }

    void remove(int l, int r, int v){
        remove(l, r, v, 0, 0, size);
    }

    int query(int ind, int x, int l, int r){
        if(l + 1 == r){
            if(mins[x] == -1)
                return 0;
            return mins[x];
        }
        int mid = (l + r) >> 1;
        int ans = -1;
        if(ind < mid)
            ans = query(ind, (x << 1) + 1, l, mid);
        else
            ans = query(ind, (x << 1) + 2, mid, r);
        int left = max(0, mins[x]), right = (maxs[x] == -1 ? 1e9 : maxs[x]);
        if(left <= ans and ans <= right)
            return ans;
        else if(ans < left)
            return left;
        return right;
    }

    int query(int x){
        return query(x, 0, 0, size);
    }
};

void buildWall(int n, int k, int op[], int left[], int right[], int height[], int finalHeight[]){
    SegTree s(n);
    for(int i = 0; i < k; i ++){
        if(op[i] == 1){
            if(height[i] != 0)
                s.add(left[i], right[i] + 1, height[i]);
        }
        else
            s.remove(left[i], right[i] + 1, height[i]);
        // for(int j = 0; j < 2 * s.size - 1; j ++)
        //     cout << i + 2 << ' ' << j << ' ' << s.mins[j] << ' ' << s.maxs[j] << endl;
        // for(int i = 0; i < n; i ++)
        //     cout << s.query(i) << ' ';
        // cout << endl;
    }
    for(int i = 0; i < n; i ++)
        finalHeight[i] = s.query(i);
    // cout << endl;
    // cout << "ANSWER: " << endl;
    // for(int i = 0; i < n; i ++)
    //     finalHeight[i] = 0;
    // for(int i = 0; i < k; i ++){
    //     for(int j = left[i]; j <= right[i]; j ++){
    //         if(op[i] == 1)
    //             finalHeight[j] = max(finalHeight[j], height[i]);
    //         else
    //             finalHeight[j] = min(finalHeight[j], height[i]);
    //     }
    // }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 4 ms 860 KB Output is correct
5 Correct 4 ms 860 KB Output is correct
6 Correct 3 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 82 ms 13592 KB Output is correct
3 Correct 54 ms 6740 KB Output is correct
4 Correct 144 ms 19432 KB Output is correct
5 Correct 122 ms 19888 KB Output is correct
6 Correct 145 ms 18516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 5 ms 892 KB Output is correct
5 Correct 4 ms 860 KB Output is correct
6 Correct 5 ms 908 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 83 ms 13400 KB Output is correct
9 Correct 56 ms 7764 KB Output is correct
10 Correct 128 ms 19540 KB Output is correct
11 Correct 130 ms 19824 KB Output is correct
12 Correct 148 ms 18512 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 84 ms 13616 KB Output is correct
15 Correct 22 ms 1884 KB Output is correct
16 Correct 380 ms 19792 KB Output is correct
17 Correct 209 ms 19028 KB Output is correct
18 Correct 259 ms 19112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 4 ms 860 KB Output is correct
5 Correct 4 ms 896 KB Output is correct
6 Correct 4 ms 860 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 85 ms 13392 KB Output is correct
9 Correct 51 ms 7760 KB Output is correct
10 Correct 139 ms 19536 KB Output is correct
11 Correct 117 ms 19792 KB Output is correct
12 Correct 149 ms 18516 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 118 ms 13596 KB Output is correct
15 Correct 24 ms 1884 KB Output is correct
16 Correct 355 ms 19540 KB Output is correct
17 Correct 208 ms 19056 KB Output is correct
18 Correct 196 ms 19112 KB Output is correct
19 Correct 511 ms 58452 KB Output is correct
20 Correct 513 ms 58256 KB Output is correct
21 Correct 601 ms 58192 KB Output is correct
22 Correct 520 ms 58260 KB Output is correct
23 Correct 533 ms 58192 KB Output is correct
24 Correct 536 ms 58328 KB Output is correct
25 Correct 567 ms 58184 KB Output is correct
26 Correct 608 ms 58312 KB Output is correct
27 Correct 547 ms 58172 KB Output is correct
28 Correct 521 ms 58196 KB Output is correct
29 Correct 520 ms 58268 KB Output is correct
30 Correct 529 ms 58196 KB Output is correct