//-----------------this template is from cp-algorithms.com--------------
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define fi first
#define se second
typedef long long ll;
const int N = 1e5 + 5;
int block_size;
int arr[N];
vector<int> cnt(N), id(N);
set<pair<ll, int>> ans;
void add(int idx){
int x = arr[idx];
if(cnt[x]){
ans.erase({(ll)id[x] * cnt[x], x});
}
cnt[x]++;
ans.insert({(ll)id[x] * cnt[x], x});
}
void remove(int idx){
int x = arr[idx];
ans.erase({(ll)id[x] * cnt[x], x});
cnt[x]--;
if(cnt[x]){
ans.insert({(ll)id[x] * cnt[x], x});
}
}
struct Query {
int l, r, idx;
bool operator<(Query other) const {
return make_pair(l / block_size, r) < make_pair(other.l / block_size, other.r);
}
};
vector<ll> mo_s_algorithm(vector<Query> queries) {
vector<ll> answers(queries.size(), -1);
sort(queries.begin(), queries.end());
// TODO: initialize data structure
int cur_l = 0;
int cur_r = -1;
// invariant: data structure will always reflect the range [cur_l, cur_r]
for(Query q : queries){
while (cur_l > q.l) add(--cur_l);
while (cur_r < q.r) add(++cur_r);
while (cur_l < q.l) remove(cur_l++);
while (cur_r > q.r) remove(cur_r--);
answers[q.idx] = (*ans.rbegin()).fi;
}
return answers;
}
void Solve(){
int n, q;
cin>>n>>q;
vector<int> tmp(n);
map<int, int> myMap;
int cur = 1;
for(int i = 0; i < n; i++){
cin>>tmp[i];
if(!myMap.count(tmp[i])){
myMap[tmp[i]] = cur++;
}
}
for(int i = 0; i < n; i++){
arr[i] = myMap[tmp[i]];
id[arr[i]] = tmp[i];
}
block_size = sqrt(n) + 1;
vector<Query> v(q);
for(int i = 0; i < q; i++){
cin>>v[i].l>>v[i].r;
v[i].l--; v[i].r--;
v[i].idx = i;
}
vector<ll> res = mo_s_algorithm(v);
for(auto x: res){
cout<<x<<nl;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
Solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1368 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1144 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1368 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1144 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
1116 KB |
Output is correct |
18 |
Correct |
5 ms |
1320 KB |
Output is correct |
19 |
Correct |
13 ms |
1364 KB |
Output is correct |
20 |
Correct |
41 ms |
1452 KB |
Output is correct |
21 |
Correct |
67 ms |
1536 KB |
Output is correct |
22 |
Correct |
73 ms |
1532 KB |
Output is correct |
23 |
Correct |
57 ms |
1508 KB |
Output is correct |
24 |
Correct |
57 ms |
1556 KB |
Output is correct |
25 |
Correct |
81 ms |
1928 KB |
Output is correct |
26 |
Correct |
83 ms |
1628 KB |
Output is correct |
27 |
Correct |
79 ms |
1692 KB |
Output is correct |
28 |
Correct |
82 ms |
1680 KB |
Output is correct |
29 |
Correct |
80 ms |
1628 KB |
Output is correct |
30 |
Correct |
79 ms |
1656 KB |
Output is correct |
31 |
Correct |
57 ms |
1544 KB |
Output is correct |
32 |
Correct |
29 ms |
1372 KB |
Output is correct |
33 |
Correct |
79 ms |
1628 KB |
Output is correct |
34 |
Correct |
78 ms |
1740 KB |
Output is correct |
35 |
Correct |
73 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
2 ms |
1116 KB |
Output is correct |
6 |
Correct |
2 ms |
1116 KB |
Output is correct |
7 |
Correct |
4 ms |
1372 KB |
Output is correct |
8 |
Correct |
8 ms |
1628 KB |
Output is correct |
9 |
Correct |
15 ms |
2000 KB |
Output is correct |
10 |
Correct |
10 ms |
1884 KB |
Output is correct |
11 |
Correct |
58 ms |
6104 KB |
Output is correct |
12 |
Correct |
50 ms |
2904 KB |
Output is correct |
13 |
Correct |
56 ms |
3892 KB |
Output is correct |
14 |
Correct |
88 ms |
6996 KB |
Output is correct |
15 |
Correct |
95 ms |
9956 KB |
Output is correct |
16 |
Correct |
117 ms |
12884 KB |
Output is correct |
17 |
Correct |
39 ms |
3420 KB |
Output is correct |
18 |
Correct |
64 ms |
5880 KB |
Output is correct |
19 |
Correct |
80 ms |
12268 KB |
Output is correct |
20 |
Correct |
66 ms |
13220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1368 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
2 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1144 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
1116 KB |
Output is correct |
18 |
Correct |
5 ms |
1320 KB |
Output is correct |
19 |
Correct |
13 ms |
1364 KB |
Output is correct |
20 |
Correct |
41 ms |
1452 KB |
Output is correct |
21 |
Correct |
67 ms |
1536 KB |
Output is correct |
22 |
Correct |
73 ms |
1532 KB |
Output is correct |
23 |
Correct |
57 ms |
1508 KB |
Output is correct |
24 |
Correct |
57 ms |
1556 KB |
Output is correct |
25 |
Correct |
81 ms |
1928 KB |
Output is correct |
26 |
Correct |
83 ms |
1628 KB |
Output is correct |
27 |
Correct |
79 ms |
1692 KB |
Output is correct |
28 |
Correct |
82 ms |
1680 KB |
Output is correct |
29 |
Correct |
80 ms |
1628 KB |
Output is correct |
30 |
Correct |
79 ms |
1656 KB |
Output is correct |
31 |
Correct |
57 ms |
1544 KB |
Output is correct |
32 |
Correct |
29 ms |
1372 KB |
Output is correct |
33 |
Correct |
79 ms |
1628 KB |
Output is correct |
34 |
Correct |
78 ms |
1740 KB |
Output is correct |
35 |
Correct |
73 ms |
1628 KB |
Output is correct |
36 |
Correct |
1 ms |
1116 KB |
Output is correct |
37 |
Correct |
1 ms |
1116 KB |
Output is correct |
38 |
Correct |
1 ms |
1116 KB |
Output is correct |
39 |
Correct |
1 ms |
1116 KB |
Output is correct |
40 |
Correct |
2 ms |
1116 KB |
Output is correct |
41 |
Correct |
2 ms |
1116 KB |
Output is correct |
42 |
Correct |
4 ms |
1372 KB |
Output is correct |
43 |
Correct |
8 ms |
1628 KB |
Output is correct |
44 |
Correct |
15 ms |
2000 KB |
Output is correct |
45 |
Correct |
10 ms |
1884 KB |
Output is correct |
46 |
Correct |
58 ms |
6104 KB |
Output is correct |
47 |
Correct |
50 ms |
2904 KB |
Output is correct |
48 |
Correct |
56 ms |
3892 KB |
Output is correct |
49 |
Correct |
88 ms |
6996 KB |
Output is correct |
50 |
Correct |
95 ms |
9956 KB |
Output is correct |
51 |
Correct |
117 ms |
12884 KB |
Output is correct |
52 |
Correct |
39 ms |
3420 KB |
Output is correct |
53 |
Correct |
64 ms |
5880 KB |
Output is correct |
54 |
Correct |
80 ms |
12268 KB |
Output is correct |
55 |
Correct |
66 ms |
13220 KB |
Output is correct |
56 |
Correct |
169 ms |
1876 KB |
Output is correct |
57 |
Correct |
679 ms |
2452 KB |
Output is correct |
58 |
Correct |
1541 ms |
3480 KB |
Output is correct |
59 |
Correct |
2465 ms |
4900 KB |
Output is correct |
60 |
Correct |
3801 ms |
5860 KB |
Output is correct |
61 |
Execution timed out |
4089 ms |
5208 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |