-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP11235.cpp
executable file
·95 lines (90 loc) · 2.1 KB
/
P11235.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Represents interval including low and high
*/
struct Node {
int low, high, val;
void set(int l, int h, int v) {
low = l; high = h; val = v;
}
};
int freq(int reqLow, int reqHigh, Node const * const tree, int idx) {
const Node &n = tree[idx];
if(reqHigh < n.low || reqLow > n.high) {
return 0;
}
if(reqLow <= n.low && n.high <= reqHigh) {
return n.val;
}
if(n.val == n.high - n.low + 1) { // Is leaf
if(n.low < reqLow) {
//cerr << "# n.high=" << n.high << ", reqLow=" << reqLow << " ";
return MIN(n.high,reqHigh) - reqLow + 1;
}
if(n.high > reqHigh) {
//cerr << "!";
return reqHigh - MAX(n.low,reqLow) + 1;
}
//cerr << "?";
return reqHigh - reqLow + 1;
}
int left = freq(reqLow, reqHigh, tree, idx*2+1);
int right = freq(reqLow, reqHigh, tree, idx*2+2);
return MAX(left, right);
}
int main() {
int n, q, a[100009], x;
while(true) {
cin >> n;
if(n == 0)
return 0;
cin >> q;
int prev = -99999999;
int leafs = 0;
FORI(n) {
cin >> x;
if(x != prev) {
a[leafs++] = 1;
prev = x;
}
else {
++a[leafs-1];
}
} // FORI(n)
Node *tree = new Node[4*leafs];
int height = 1;
int rowSize = 1;
int rowI = 0;
while(rowSize < leafs) {
++height;
rowI += rowSize;
rowSize <<= 1;
}
// Build tree:
int low = 0; // Left side
FORI(rowSize) {
int size = i >= leafs ? 0 : a[i];
tree[rowI+i].set(low, low+size-1, size);
low += size; // Full Binary tree
// Bubble up:
int idx = rowI+i;
while(idx > 0) { // While not at the root
int parentI = (idx-1)/2;
if(idx != parentI*2+2)
break; // Not a right child.
int val = MAX(tree[idx-1].val, tree[idx].val);
//cerr << "Setting " << parentI << " ";
tree[parentI].set(tree[idx-1].low, tree[idx].high, val);
idx = parentI;
}
}
FORI(q) {
int low, high;
cin >> low >> high; --low; --high;
int ret = freq(low, high, tree, 0);
if(ret == 0)
ret = 1;
cout << ret << endl;
}
delete[] tree;
} // while(true)
} // int main