/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 17ms 18.695 MiB
#2 Accepted 17ms 18.801 MiB
#3 Accepted 17ms 18.887 MiB
#4 Accepted 228ms 24.477 MiB
#5 Accepted 169ms 63.109 MiB
#6 Accepted 493ms 41.062 MiB
#7 Accepted 103ms 19.285 MiB
#8 Accepted 364ms 40.539 MiB
#9 Accepted 254ms 40.16 MiB
#10 Accepted 185ms 41.25 MiB
#11 Accepted 36ms 18.766 MiB
#12 Accepted 52ms 18.996 MiB
#13 Accepted 17ms 18.871 MiB

Code

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pe(c) for (auto &e : c) cout << e << ' '; cout << '\n'
#define ps(b) cout << (b ? "YES" : "NO") << '\n'
#ifdef LOCAL
#include "def.h"
#else
#define ck(...)
#endif
const ll M = 1e9 + 7, N = 2e5 + 5;
long long ar[N], n; vector<long long> tre[N << 2];
auto merge(auto &a, auto &b) {
    int i, j, k = a.size(), l = b.size(); vector<long long> res;
    for (i = 0, j = 0; i < k && j < l;) {
        if (a[i] < b[j]) res.push_back(a[i++]);
        else res.push_back(b[j++]);
    }
    while (i < k) res.push_back(a[i++]);
    while (j < l) res.push_back(b[j++]);
    return res;
}
void make(int nd = 0, int s = 0, int e = n - 1) {
    if (s == e) { tre[nd].push_back(ar[e]); return; }
    int m = (s + e) >> 1, lc = (nd << 1) + 1, rc = lc + 1;
    make(lc, s, m); make(rc, m + 1, e);
    tre[nd] = merge(tre[lc], tre[rc]);
}
void up(int in, long long val, int nd = 0, int s = 0, int e = n - 1) {
    if (s == e) { tre[nd][0] = val; return; }
    int m = (s + e) >> 1, lc = (nd << 1) + 1, rc = lc + 1;
    if (in <= m) up(in, val, lc, s, m);
    else up(in, val, rc, m + 1, e);
    tre[nd] = merge(tre[lc], tre[rc]);
}
int get(int l, int r, long long val, int nd = 0, int s = 0, int e = n - 1) {
    if (l > e || r < s || l > r) return 0;
    if (l <= s && e <= r)
        return lower_bound(tre[nd].begin(), tre[nd].end(), val) - tre[nd].begin();
    int m = (e + s) >> 1, lc = (nd << 1) + 1, rc = lc + 1;
    return get(l, r, val, lc, s, m) + get(l, r, val, rc, m + 1, e);
}
int get1(int l, int r, long long val, int nd = 0, int s = 0, int e = n - 1) {
    if (l > e || r < s || l > r) return 0;
    if (l <= s && e <= r)
        return lower_bound(tre[nd].begin(), tre[nd].end(), val + 1) - tre[nd].begin();
    int m = (e + s) >> 1, lc = (nd << 1) + 1, rc = lc + 1;
    return get1(l, r, val, lc, s, m) + get1(l, r, val, rc, m + 1, e);
}
void reset() {
    for (int i = 0; i < (n << 2); ++i) tre[i].clear();
}
void test(int tc) {
    ll o = 0, m = 0, a = 0, b = 0, c = 0, d = 0, i = 0, j = 0, k = 0, q = 0;
    cin >> n;
    // vector<ll> ar(n);
    for (i = 0; i < n; ++i) { cin >> ar[i]; }
    make();
    cin >> q;
    while(q--) {
        d = 0;
        cin >> a; --a;
        b = get(0, a - 1, ar[a]);
        c = (n - a - 1) - get1(a + 1, n - 1, ar[a]);
        d += (b * c);
        b = a - get1(0, a - 1, ar[a]);
        c = get(a + 1, n - 1, ar[a]);
        d += (b * c);
        cout << d << '\n';
    }
    // cout << '\n';
    reset();
}

signed main() {
    cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit | cin.badbit);
    int tc = 0, t = 1;
    cin >> t;
    while (tc < t) test(++tc);
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1079 Roy and Query (Easy Version)
Contest
Brain Booster #6
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 17:53:47
Judged At
2024-12-17 11:32:12
Judged By
Score
100
Total Time
493ms
Peak Memory
63.109 MiB