-
Notifications
You must be signed in to change notification settings - Fork 0
/
FREQUENT-frequent_values.cpp
78 lines (64 loc) · 1.75 KB
/
FREQUENT-frequent_values.cpp
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
#include <bits/stdc++.h>
using namespace std;
int n, q, nums[100000];
struct Node {
int i, j, freq;
Node *left = nullptr;
Node *right = nullptr;
Node(int _i, int _j, int _freq) : i(_i), j(_j), freq(_freq) {}
Node(Node *l, Node *r) : left(l), right(r) {
i = min(l->i, r->i);
j = max(l->j, r->j);
freq = max(l->freq, r->freq);
}
};
vector<Node> tree;
int query(Node &subtree, int i, int j, int orig_i, int orig_j){
if(subtree.j < orig_i || orig_j < subtree.i) return INT_MIN;
if(subtree.i == i && subtree.j == j) return subtree.freq;
if(!subtree.left && !subtree.right)
return subtree.freq - abs(i - subtree.i) - abs(j - subtree.j);
return max(
query(*subtree.left, max(subtree.left->i, i), min(j, subtree.left->j), orig_i, orig_j),
query(*subtree.right, max(subtree.right->i, i), min(j, subtree.right->j), orig_i, orig_j)
);
}
void create_leafs(){
int prev = nums[0];
int from = 0;
for(int i=1; i<n; i++){
if(nums[i] == prev) continue;
prev = nums[i];
tree.push_back(Node(from, i - 1, i - from));
from = i;
}
// Close last set (edge case).
tree.push_back(Node(from, n - 1, n - from));
}
void create_parent_nodes(){
int n_idx = 0;
while(tree.size() - n_idx > 1){
int size = tree.size();
for(; n_idx<size; n_idx+=2)
tree.push_back(Node(&tree[n_idx], &tree[n_idx + 1]));
}
}
void solve(){
tree.clear();
for(int i=0; i<n; i++) scanf("%d", &nums[i]);
create_leafs();
create_parent_nodes();
while(q--){
int i, j;
scanf("%d %d", &i, &j);
printf("%d\n", query(tree[tree.size() - 1], i - 1, j - 1, i - 1, j - 1));
}
}
int main(){
tree.reserve(100000 * 40); // avoid realloc
while(scanf("%d", &n) == 1){
if(n == 0) break;
scanf("%d", &q);
solve();
}
}