/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 19ms 18.742 MiB
#2 Accepted 19ms 18.859 MiB
#3 Accepted 180ms 19.371 MiB
#4 Accepted 286ms 22.031 MiB
#5 Accepted 628ms 33.039 MiB
#6 Accepted 648ms 32.812 MiB
#7 Accepted 635ms 33.023 MiB
#8 Accepted 607ms 32.996 MiB
#9 Accepted 1211ms 33.781 MiB
#10 Accepted 1203ms 47.926 MiB
#11 Accepted 269ms 19.426 MiB

Code

#include<bits/stdc++.h>
#define endl        '\n'
#define F           first
#define S           second
#define pb          push_back
#define yes         cout<<"YES\n"
#define no          cout<<"NO\n"
#define all(x)      x.begin(),x.end()
#define allr(x)     x.rbegin(),x.rend()
#define error1(x)   cerr<<#x<<" = "<<(x)<<endl
#define error2(a,b) cerr<<"("<<#a<<", "<<#b<<") = ("<<(a)<<", "<<(b)<<")\n";
#define coutall(v)  for(auto &it: v) cout << it << " "; cout << endl;
using namespace std;
using ll = long long;

const int N = 2e5 + 3;

struct Merge_Sort_Tree {
    vector<vector<int>> t;

    Merge_Sort_Tree() {
        t.resize(4 * N, {});
    }

    void build(int node, int st, int en, vector<int> &arr) { //=> O(N log N)
        t[node].clear();
        if (st == en) {
            t[node] = {arr[st]};
            return;
        }
        int mid = (st + en) >> 1;
        build(node << 1, st, mid, arr); // divide left side
        build(node << 1 | 1, mid + 1, en, arr); // divide right side
        // Merging left and right portion
        auto &Cur = t[node];
        auto &Left = t[node << 1];
        auto &Right = t[node << 1 | 1];
        merge(make_move_iterator(Left.begin()), make_move_iterator(Left.end()),
              make_move_iterator(Right.begin()), make_move_iterator(Right.end()),
              back_inserter(Cur));
        return;
    }
    void update(int node, int st, int en, int idx, int val) { //=> O(log n)
        if (st == en) {
            t[node] = {val};
            return;
        }
        int mid = (st + en) >> 1;
        if (idx <= mid) update(node << 1, st, mid, idx, val);
        else update(node << 1 | 1, mid + 1, en, idx, val);
        // Merging left and right portion
        auto &Cur = t[node];
        auto &Left = t[node << 1];
        auto &Right = t[node << 1 | 1];
        
        Cur.clear();
        merge(make_move_iterator(Left.begin()), make_move_iterator(Left.end()),
              make_move_iterator(Right.begin()), make_move_iterator(Right.end()),
              back_inserter(Cur));
        return;
    }
    int query(int node, int st, int en, int l, int r, int val) { //=> O(log N log N)
        // assert(l <= r); // <==
        if (st > r || en < l) { // No overlapping and out of range
            return 0; // <== careful 
        }
        if (l <= st && en <= r) { // Complete overlapped (l-r in range)
            return lower_bound(all(t[node]), val) - t[node].begin();
        }
        // Partial overlapping
        int mid = (st + en) >> 1;
        auto Left = query(node << 1, st, mid, l, r, val);
        auto Right = query(node << 1 | 1, mid + 1, en, l, r, val);
        return Left + Right;
    }
} st1;

void solve() {
    int n;
    cin >> n;
    vector<int> v(n + 1);
    for (int i = 1; i <= n; i++) cin >> v[i];
    
    st1.build(1, 1, n, v); // <==

    int q, l, x, r;
    cin >> q;
    while(q--) {
        cin >> l >> x >> r;

        ll leftMn = st1.query(1, 1, n, l, x - 1, v[x]);
        ll leftMx = (x - l) - st1.query(1, 1, n, l, x - 1, v[x] + 1);

        ll rightMn = st1.query(1, 1, n, x + 1, r, v[x]);
        ll rightMx = (r - x) - st1.query(1, 1, n, x + 1, r, v[x] + 1);

        cout << (leftMn * rightMx) + (leftMx * rightMn) << endl;
    }
    return;
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1082 Roy and Query (Hard Version)
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-04 11:50:58
Judged At
2024-11-11 02:42:58
Judged By
Score
100
Total Time
1211ms
Peak Memory
47.926 MiB