/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 320.0 KiB
#2 Accepted 2ms 328.0 KiB
#3 Accepted 158ms 4.273 MiB
#4 Accepted 138ms 3.977 MiB
#5 Accepted 125ms 3.902 MiB
#6 Accepted 119ms 3.828 MiB
#7 Accepted 2ms 320.0 KiB
#8 Accepted 2ms 396.0 KiB
#9 Accepted 145ms 4.172 MiB
#10 Accepted 141ms 3.785 MiB

Code

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (3.141592653589)
#define mod 1000000007
#define pb push_back
#define vi vector<ll>
#define mp make_pair
#define f first
#define s second
#define sz(container) (ll) container.size()
#define setp(x) cout << fixed << setprecision(x)
#define all(container) container.begin(), container.end()
#define rall(container) container.rbegin(), container.rend()
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

struct SegmentTree
{
    int n;
    vector<bool> st;
    vi arr;

    SegmentTree(int n) : n(n), st(4 * n, true), arr(n) {}

    void build(int start, int end, int node)
    {
        if (start == end)
        {
            return;
        }
        int mid = (start + end) / 2;
        build(start, mid, 2 * node + 1);
        build(mid + 1, end, 2 * node + 2);
        st[node] = st[2 * node + 1] && st[2 * node + 2] && (arr[mid] <= arr[mid + 1]);
    }

    void update(int start, int end, int node, int idx, ll val)
    {
        if (start == end)
        {
            arr[start] = val;
            return;
        }
        int mid = (start + end) / 2;
        if (idx <= mid)
        {
            update(start, mid, 2 * node + 1, idx, val);
        }
        else
        {
            update(mid + 1, end, 2 * node + 2, idx, val);
        }
        st[node] = st[2 * node + 1] && st[2 * node + 2] && (arr[mid] <= arr[mid + 1]);
    }

    bool query(int start, int end, int l, int r, int node)
    {
        if (r < start || l > end)
        {
            return true;
        }
        if (l <= start && end <= r)
        {
            return st[node];
        }
        int mid = (start + end) / 2;
        bool left = query(start, mid, l, r, 2 * node + 1);
        bool right = query(mid + 1, end, l, r, 2 * node + 2);
        bool cross = true;
        if (l <= mid && r > mid)
        {
            cross = arr[mid] <= arr[mid + 1];
        }
        return left && right && cross;
    }

    void build(vi &inputArr)
    {
        arr = inputArr;
        build(0, n - 1, 0);
    }

    void update(int idx, ll val)
    {
        update(0, n - 1, 0, idx, val);
    }

    bool query(int l, int r)
    {
        return query(0, n - 1, l, r, 0);
    }
};

int main()
{
    fast;
    ll n;
    cin >> n;
    vi arr(n);
    for (ll i = 0; i < n; i++)
    {
        cin >> arr[i];
    }

    SegmentTree segTree(n);
    segTree.build(arr);

    // for (int i = 0; i < 4 * n; i++)
    // {
    //     cout << segTree.st[i] << " ";
    // }
    ll q;
    cin >> q;
    while (q--)
    {
        ll x, l, r;
        cin >> x >> l >> r;
        if (x == 1)
        {
            segTree.update(l - 1, r);
        }
        else
        {
            cout << (segTree.query(l - 1, r - 1) ? "YES" : "NO") << "\n";
        }
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1085 Sorted or !Sorted
Language
C++17 (G++ 13.2.0)
Submit At
2024-08-17 04:34:02
Judged At
2024-11-11 03:09:47
Judged By
Score
100
Total Time
158ms
Peak Memory
4.273 MiB