/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 348.0 KiB
#3 Accepted 2ms 320.0 KiB
#4 Accepted 268ms 6.57 MiB
#5 Accepted 881ms 26.34 MiB
#6 Accepted 380ms 16.488 MiB
#7 Accepted 84ms 1.059 MiB
#8 Accepted 123ms 11.676 MiB
#9 Accepted 128ms 11.266 MiB
#10 Accepted 229ms 25.91 MiB
#11 Accepted 36ms 756.0 KiB
#12 Accepted 72ms 804.0 KiB
#13 Accepted 1ms 324.0 KiB

Code

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


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

// Template of Multi Ordered set 
struct Treap{ /// hash = 96814
    int len;
    const int ADD = 1000010;
    const int MAXVAL = 1000000010;
    unordered_map <long long, int> mp; /// Change to int if only int in treap
    tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> T;

    Treap(){
        len = 0;
        T.clear(), mp.clear();
    }

    inline void clear(){
        len = 0;
        T.clear(), mp.clear();
    }

    inline void insert(long long x){
        len++, x += MAXVAL;
        int c = mp[x]++;
        T.insert((x * ADD) + c);
    }

    inline void erase(long long x){
        x += MAXVAL;
        int c = mp[x];
        if (c){
            c--, mp[x]--, len--;
            T.erase((x * ADD) + c);
        }
    }

    /// 1-based index, returns the K'th element in the treap, -1 if none exists
    inline long long kth(int k){
        if (k < 1 || k > len) return -1;
        auto it = T.find_by_order(--k);
        return ((*it) / ADD) - MAXVAL;
    }

    /// Count of value < x in treap
    inline int count(long long x){
        x += MAXVAL;
        int c = mp[--x];
        return (T.order_of_key((x * ADD) + c));
    }

    /// Number of elements in treap
    inline int size(){
        return len;
    }
};



int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--){
    int n, q;
    cin >> n;
    Treap tt;
    map<int, int> mp1, mp2;
    vector<ll> a(n), mn1(n), mn2(n), mx1(n), mx2(n), ans(n);
    for (int i = 0; i < n; i++){
      cin >> a[i];
      tt.insert(a[i]);
      mp1[a[i]]++;
      int x = tt.count(a[i]);
      mn1[i] = x;
      mx1[i] = (i + 1) - x - mp1[a[i]];
    }
    tt.clear();
    int cnt = 0;
    for (int i = n - 1; i >= 0; i--){
      tt.insert(a[i]);
      mp2[a[i]]++;
      cnt++;
      int x = tt.count(a[i]);
      mn2[i] = x;
      mx2[i] = cnt - x - mp2[a[i]];
    }
    for (int i = 0; i < n; i++){
      ans[i] = mn1[i] * mx2[i] + mn2[i] * mx1[i];
    }
    cin >> q;
    while (q--){
      int j;
      cin >> j;
      j--;
      cout << ans[j] << "\n";
    }
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1079 Roy and Query (Easy Version)
Language
C++11 (G++ 13.2.0)
Submit At
2024-10-03 18:53:07
Judged At
2024-11-11 02:44:59
Judged By
Score
100
Total Time
881ms
Peak Memory
26.34 MiB