/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Accepted 3ms 712.0 KiB
#3 Accepted 282ms 1.23 MiB
#4 Accepted 451ms 6.68 MiB
#5 Accepted 1008ms 31.805 MiB
#6 Accepted 1056ms 31.805 MiB
#7 Accepted 1010ms 31.816 MiB
#8 Accepted 993ms 31.801 MiB
#9 Accepted 1857ms 33.469 MiB
#10 Accepted 1741ms 65.004 MiB
#11 Accepted 470ms 1.121 MiB

Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long

struct MergeSortTree {
    ll n;
    vector<vector<ll>> tree;

    MergeSortTree(ll n) : n(n) {
        tree.resize(4 * n);
    }

    void build(const vector<ll>& ar, ll node, ll l, ll r) {
        if (l == r) {
            tree[node].push_back(ar[l]);
            return;
        }
        ll mid = (l + r) / 2;
        build(ar, node * 2 + 1, l, mid);
        build(ar, node * 2 + 2, mid + 1, r);

        merge(tree[node * 2 + 1].begin(), tree[node * 2 + 1].end(),
              tree[node * 2 + 2].begin(), tree[node * 2 + 2].end(),
              back_inserter(tree[node]));
    }

    ll query(ll node, ll start, ll end, ll l, ll r, ll x) {
        if (l > end or r < start) {
            return 0;
        }
        if (l <= start and end <= r) {
            ll cnt = lower_bound(tree[node].begin(), tree[node].end(), x) - tree[node].begin();
            return cnt;
        }
        ll mid = (start + end) / 2;
        ll m = query(node * 2 + 1, start, mid, l, r, x);
        ll n = query(node * 2 + 2, mid + 1, end, l, r, x);
        return m + n;
    }

    void build(const vector<ll>& ar) {
        build(ar, 0, 0, n - 1);
    }

    ll query(ll l, ll r, ll x) {
        return query(0, 0, n - 1, l, r, x);
    }
};

void solve() {
    ll n;
    cin >> n;
    
    
    vector<ll> ar(n); 
    for (ll i = 0; i < n; i++) {
        cin >> ar[i];
    }

    MergeSortTree segTree(n);
    segTree.build(ar);

    
    ll q;
    cin >> q;
    
    while (q--) {
        ll l, r, i;
        cin >> l >> i >> r;
        --l;
        --r;
        --i;
        //ll l = 0, r = n - 1;
        ll less_xl, greater_xl, less_xr, greater_xr, ans;
        less_xl=greater_xl=less_xr=greater_xr=ans=0;

        less_xl = segTree.query(l, i, ar[i]);
        greater_xl = (i - l + 1) - segTree.query(l, i, ar[i] + 1);
        less_xr = segTree.query(i, r, ar[i]);
        greater_xr = (r - i + 1) - segTree.query(i, r, ar[i] + 1);

        ans = less_xl * greater_xr + greater_xl * less_xr;
        cout << ans << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int t;
    cin >> t;
    while (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-03 20:22:06
Judged At
2024-11-11 02:44:34
Judged By
Score
100
Total Time
1857ms
Peak Memory
65.004 MiB