-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathordered_multiset.cpp
More file actions
101 lines (89 loc) · 2.2 KB
/
ordered_multiset.cpp
File metadata and controls
101 lines (89 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define int long long
#define F first
#define S second
#define pb push_back
using namespace std;
using namespace __gnu_pbds;
const int N = 1e6 + 5, MOD = 1e9 + 7;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
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;
}
};
void solve(){
int n;
cin >> n;
int a[n + 1], v[n + 1], ans = 0;
Treap s = Treap();
for(int i = 1; i <= n; ++i){
cin >> a[i];
ans = max(ans, a[i]);
s.insert(a[i]);
}
for(int i = 1; i <= n; ++i)
cin >> v[i];
int res = 1;
for(int k = 2; k <= n; ++k){
int rem = a[v[k - 1]];
s.erase(rem);
if(s.size() < k) break;
int pos = s.size() - k + 1;
if(k * (s.kth(pos)) > ans){
ans = k * (s.kth(pos));
res = k;
}
}
cout << ans << ' ' << res << "\n";
}
int32_t main(){
fast;
int t = 1;
cin >> t;
while(t--)
solve();
return 0;
}