/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 1ms 540.0 KiB
#3 Accepted 1ms 540.0 KiB
#4 Accepted 72ms 3.488 MiB
#5 Wrong Answer 148ms 8.641 MiB
#6 Accepted 91ms 6.301 MiB
#7 Accepted 37ms 776.0 KiB
#8 Accepted 37ms 3.625 MiB
#9 Wrong Answer 35ms 3.301 MiB
#10 Wrong Answer 104ms 13.91 MiB
#11 Accepted 14ms 772.0 KiB
#12 Accepted 27ms 768.0 KiB
#13 Accepted 1ms 540.0 KiB

Code

#include <algorithm>
#include <stack>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
#define rep(i,n) for(int i=0;i<n;++i)
#define per(i,n) for(int i=n-1;i>=0;--i)
#define repn(i,n) for(int i=1;i<=n;++i)
#define rng(i, a, b) for(int i=a; i<b; i++)
#define gnr(i,a,b) for(int i=b-1;i>=a;--i)
#define vb vector<bool>
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vvb vector<vb>
#define vvi vector<vi>
#define vvl vector<vl>
#define vvc vector<vc>
#define si unordered_set<int>
#define sl unordered_set<ll>
#define tsi set<int>
#define tsl set<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define tmii map<int, int>
#define tmll map<ll, ll>
#define mii unordered_map<int, int>
#define mll unordered_map<ll, ll>
#define pqi priority_queue<int>
#define pqig priority_queue<int, vi, greater<int>>
#define pql priority_queue<ll>
#define pqlg priority_queue<ll, vl, greater<ll>>
#define pqpii priority_queue<pii>
#define pqpll priority_queue<pll>
#define pqip priority_queue<pair<int, pair<int, int>>>
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back() 
#define lb lower_bound
#define ub upper_bound
#define ll long long
#define ld long double
#define nl '\n'
#define sp ' '
#define fi first
#define inf 8e18;
#define sinf 2e9;
#define se second
#define mpr make_pair
#define lg __lg
#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

using namespace std;
/* ありがとう、神様、あなたはとても素晴らしく 、とても優しいです!*/
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x << sp; use_(x); cerr << nl;
#else
#define dbg(x)
#endif

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    mordered_set;


struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

// customHash working with UMP
struct hsh {
    size_t operator()(const pair<ll,ll> &p) const {
        return p.first * 239 + p.second; //you could change 239 to 1e9+1
    }
};

template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

template <typename T, typename V> T rangeBitwiseAnd(T left, V right) {
    T res = 0;
    int cnt = 0; for(; left >= (1ll<<cnt); cnt++){}
    if(1ll<<cnt <= right) return 0;
    if(left==right) return left;
    
    res += 1ll<<(--cnt);
    left -= 1ll<<(cnt); right -= 1ll<<cnt;
    return res + rangeBitwiseAnd(left, right);
}

template<typename T> T MSB(T n) {T ans = -1;while(n) n /=2, ans++;return ans;}
template<typename T> T rangeBitwiseOr(T l, T r) {
    ll res = 0;
    ll u = MSB(l), v = MSB(r);
    while(u == v && u >= 0 && v >= 0) {
        ll val = (1ll<<u);
        l-=val, r-=val;
        res += val;
        u = MSB(l), v = MSB(l);
    }
    u = max(MSB(l), MSB(r));
    res += (1ll<<(u+1))-1;
    return res;
}

template<typename T> T XOR1(T n) {ll md = n % 4; if(!md) return n;else if(md==1) return 1;else if(md==2) return n+1;return 0;}
template<typename T> T rangeBitwiseXor(T l, T r) {return XOR1(r)^XOR1(l-1);}

int dx[]={0,0,1,-1,1,1,-1,-1};
int dy[]={1,-1,0,0,1,-1,1,-1};
int kx[]={1, 1, -1, -1, 2, 2, -2, -2};
int ky[]={2, -2, 2, -2, 1, -1, 1, -1};
set<char> vowels{'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};

bool chkpalin(string s) {
    rep(i, sz(s)/2) {
        if(s[i] != s[sz(s)-i-1]) return false;
    }
    return true;
}
map<char, vector<char>> phone_keypad{
    {'2', {'a', 'b', 'c'}},
    {'3', {'d', 'e', 'f'}},
    {'4', {'g', 'h', 'i'}},
    {'5', {'j', 'k', 'l'}},
    {'6', {'m', 'n', 'o'}},
    {'7', {'p', 'q', 'r', 's'}},
    {'8', {'t', 'u', 'v'}},
    {'9', {'w', 'x', 'y', 'z'}}
};
vi GPRM(int N) {
    vi x(N+10, 1), primes;
    x[0] = x[1] = 0;
    rng(i, 2, N + 5) {
        if(!x[i]) continue;
        for(int j=i+i; j<N+5; j+=i) x[j] = 0;
    }
    rep(i, N+10) {
        if(x[i]) primes.pb(i);
    }
    return primes;
}
vector<string> slice_fn(string &s, char c) {
    string tmp = "";
    vector<string> res;
    for(int i=0; i<s.size(); i++) {
        if(s[i] == c) {
            if(tmp.empty())continue;
            res.push_back(tmp);
            tmp.clear();
        } else tmp+=s[i];
    }
    if(!tmp.empty()) res.push_back(tmp);
    return res;
}
long long int_sqrt (long long x) {
  long long ans = 0;
  for (ll k = 1LL << 30; k != 0; k /= 2) {
    if ((ans + k) * (ans + k) <= x) {
      ans += k;
    }
  }
  return ans;
}

void yesnoc(bool a) {cout << (a? "YES\n": "NO\n");}
void yesnos(bool a) {cout << (a? "Yes\n": "No\n");}

// Debug デバッグ
void use_(ll W) {cerr << W;}
void use_(ld W) {cerr << W;}
void use_(char W) {cerr << W;}
void use_(string W) {cerr << W;}
void use_(int W) {cerr << W;}
void use_(double W) {cerr << W;}
template<typename T, typename V> void use_(pair<T, V> p) {cerr << "["; use_(p.fi); cerr << ','; use_(p.se); cerr << ']';}
template<typename T> void use_(vector<T> a) {cerr << '['; for(T i: a) { use_(i); cerr << sp;} cerr << ']'; cerr << nl;}
template<typename T> void use_(set<T> a) {cerr << '['; for(T i: a) { use_(i); cerr << sp;} cerr << ']'; cerr << nl;}
template<typename T> void use_(multiset<T> ms) {cerr << '['; for(T i: ms) { use_(i); cerr << sp;} cerr << ']'; cerr << nl;}
template<typename T, typename V> void use_(map<T, V> mp) {cerr << '[';  for(auto x: mp) { use_(x.fi); cerr << ',';  use_(x.se); cerr << sp;} cerr << ']'; cerr << nl;}
template<typename T> void use_(priority_queue<T>pq) {
    cerr << '[';  
    vector<T> tmp;

    while(sz(pq)) {
        tmp.pb(pq.top()); pq.pop();
    }
    use_(tmp);
    cerr << ']'; 
    cerr << nl;
}
template<typename T> void use_(priority_queue<T, vector<T>, greater<T>>pq) {
    cerr << '[';  
    vector<T> tmp;
    while(sz(pq)) {
        tmp.pb(pq.top()); pq.pop();
    }
    use_(tmp);
    cerr << ']'; 
    cerr << nl;
}
template<typename T> void use_(queue<T>pq) {
    cerr << '[';  
    vector<T> tmp;
    while(sz(pq)) {
        tmp.pb(pq.top()); pq.pop();
    }
    use_(tmp);
    cerr << ']'; 
    cerr << nl;
}
template<typename T> void use_(stack<T>pq) {
    cerr << '[';  
    vector<T> tmp;
    while(sz(pq)) {
        tmp.pb(pq.top()); pq.pop();
    }
    use_(tmp);
    cerr << ']'; 
    cerr << nl;
}
#define trace1(x)                cerr << #x << ": " << (x) << nl;
#define trace2(x, y)             cerr << #x << ": " << (x) << " | " << #y << ": " << (y) << nl;
#define trace3(x, y, z)          cerr << #x << ": " << (x) << " | " << #y << ": " << (y) << " | " << #z << ": " << (z) << nl;
#define trace4(a, b, c, d)       cerr << #a << ": " << (a) << " | " << #b << ": " << (b) << " | " << #c << ": " << (c) << " | " << #d << ": " << (d) << nl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << (a) << " | " << #b << ": " << (b) << " | " << #c << ": " << (c) << " | " << #d << ": " << (d) << " | " << #e << ": " << (e) << nl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << (a) << " | " << #b << ": " << (b) << " | " << #c << ": " << (c) << " | " << #d << ": " << (d) << " | " << #e << ": " << (e) << " | " << #f << ": " << (f) << nl;


// Read 読む
template<typename T> vector<vector<T>> RG(T n, T m) {vector<vector<T>> g(n);rep(i, m) {int u, v; cin >> u >> v;g[--u].pb(--v); g[v].pb(u);}return g;}
template<typename T> vector<vector<T>> RDG(T n, T m) {vector<vector<T>> g(n);rep(i, m) {int u, v; cin >> u >> v;g[--u].pb(--v);}return g;}
template<typename T> vector<vector<pair<T, T>>> RGW(T n, T m) {vector<vector<pair<T, T>>> g(n);rep(i, m) {int u, v, w; cin >> u >> v >> w;g[--u].pb({--v, w});g[v].pb({u, w});}return g;}
template<typename T> vector<T> R(T n) {vector<T> a(n);rep(i, n) cin >> a[i];return a;}

// Print 印刷する
template<typename T> void print(T x) {cout << x << nl;}
template<typename T> void print2(T x, T y) {cout << x << sp << y << nl;}
template<typename T> void printA(vector<T> &A) { for(auto &x: A) { cout << x << ' ';}cout << '\n';} 
template<typename T> void printA2(vector<vector<T>> &A) {for(auto x: A) {for(auto y: x) {cout << y << ' ';}cout << '\n';}}
template<typename T> void printS(set<T> &S) {for(auto &x: S) cout << x << ' '; cout << '\n';}
template<typename T> void printmulS(multiset<T> &S) {for(auto &x: S) cout << x << ' '; cout << '\n';}


ll n, k, m, q;
//const ll mod = 998244353;
const ll mod = 1000000007;
 class BIT {
    vector<ll> bit;

public:
    BIT(int n) {
        bit.resize(n + 1, 0);
    }

    ll get(int i) {
        ll sum = 0;
        for (; i > 0; i -= (i & (-i)))
            sum += bit[i];
        return sum;
    }

    void add(int i, int val) {
        for (; i < bit.size(); i += (i & (-i)))
            bit[i] += val;
    }
};

void solve();
int main() {
#ifndef ONLINE_JUDGE
    freopen("error.txt", "w", stderr);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
    int I_love_tatsumaki_chan = 1;
    cin >> I_love_tatsumaki_chan;
    while (I_love_tatsumaki_chan--) {
        solve();
    }
}

template <typename T>
vector<T> compress1d(vector<T> u) {
    vector<T> ans = u;
    set<T> s(u.begin(), u.end());

    map<T, int> mp;
    int cur = 1;
    for (const auto &x : s)
        mp[x] = cur++;
    for (auto &x : ans)
        x = mp[x];
    return ans;
}

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

    vector<ll> b = compress1d(a);
    int mx = *max_element(b.begin(), b.end());

    BIT bit(mx), tib(mx);
    
    int q;
    cin >> q;
    vector<pair<int, int>> query(q);
    for (int i = 0; i < q; i++) {
        int x;
        cin >> x;
        query[i] = {x - 1, i};
    }

    vector<int> ans(q);
    sort(query.begin(), query.end());
    
    // Initialize tib with all elements
    for (int i = 0; i < n; i++) {
        tib.add(b[i], 1);
    }

    int i = 0;
    for (auto [x, y] : query) {
        while (i < x) {
            bit.add(b[i], 1);   // Add element to BIT
            tib.add(b[i], -1);  // Remove from tib as it is now in the past
            i++;
        }

        tib.add(b[x], -1);

        // Condition 1: Find pairs where a[i] > a[x] > a[j]
        ll t1 = bit.get(mx) - bit.get(b[x]);  // Elements > a[x] before index x
        ll t2 = tib.get(b[x] - 1);            // Elements < a[x] after index x
        ans[y] += t1 * t2;

        // Condition 2: Find pairs where a[i] < a[x] < a[j]
        t1 = bit.get(b[x] - 1);               // Elements < a[x] before index x
        t2 = tib.get(mx) - tib.get(b[x]);     // Elements > a[x] after index x
        ans[y] += t1 * t2;

        // Add the current element back to tib for further queries
        tib.add(b[x], 1);
    }


    for (auto x : ans) cout << x << '\n';
}

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 16:59:45
Judged At
2024-10-03 16:59:45
Judged By
Score
76
Total Time
148ms
Peak Memory
13.91 MiB