-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFREQUENT.cpp
126 lines (113 loc) · 3.09 KB
/
FREQUENT.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//PROBLEM :: http://www.spoj.com/problems/FREQUENT/
//TAG :: SEGMENT TREE
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-10;
const int MAX = 1<<17;
struct Node{
int frequent;
int right_frequent;
int left_frequent;
} Tree[MAX<<1];
int a[MAX];
void Build_Tree(int node,int L,int R)
{
int mid = (L+R)>>1;
int o = node<<1;
if(L == R)
{
Tree[node].frequent = 1;
Tree[node].left_frequent = 1;
Tree[node].right_frequent = 1;
}
else{
Build_Tree(o,L,mid);
Build_Tree(o+1,mid+1,R);
int val1 = 0;
if(a[mid] == a[mid+1])
{
val1 = Tree[o].right_frequent+Tree[o+1].left_frequent;
}
Tree[node].frequent = max(val1,max(Tree[o].frequent,Tree[o+1].frequent));
if(a[L] == a[mid] && a[mid+1] == a[mid])
{
Tree[node].left_frequent = Tree[o].right_frequent+Tree[o+1].left_frequent;
}
else
Tree[node].left_frequent = Tree[o].left_frequent;
if(a[mid+1] == a[R] && a[mid+1] == a[mid])
{
Tree[node].right_frequent = Tree[o].right_frequent+Tree[o+1].left_frequent;
}
else
Tree[node].right_frequent = Tree[o+1].right_frequent;
}
}
Node query(int node,int L,int R,int i,int j)
{
int mid = (L+R) >> 1;
int o = node<<1;
Node p;
p.frequent = 0;
p.left_frequent = 0;
p.right_frequent = 0;
if(i <= L && j >= R)
{
//cout << Tree[node].frequent << endl;;
return Tree[node];
}
else if(mid >= j)
return query(o,L,mid,i,j);
else if(mid<i)
return query(o+1,mid+1,R,i,j);
else{
Node k = query(o,L,mid,i,mid);
Node s = query(o+1,mid+1,R,mid+1,j);
// cout << k.frequent << " " << k.left_frequent << " " << k.right_frequent<<" "<< s.frequent <<" " << s.left_frequent << " " << s.right_frequent<< endl;
int val1 = 0;
if(a[mid] == a[mid+1])
{
val1 = k.right_frequent+s.left_frequent;
}
int m = k.frequent;
if(val1 > k.frequent)
m = val1;
if(s.frequent > m)
m = s.frequent;
//cout << m << endl;
p.frequent = m;
if(a[i] == a[mid] && a[mid+1] == a[mid])
{
p.left_frequent = k.right_frequent+s.left_frequent;
}
else
p.left_frequent = k.left_frequent;
if(a[mid+1] == a[j] && a[mid+1] == a[mid])
{
p.right_frequent = k.right_frequent+s.left_frequent;
}
else
p.right_frequent = s.right_frequent;
return p;
}
}
int main()
{
int n,q;
while(scanf("%d",&n) == 1 && n != 0){
scanf("%d",&q);
for(int i = 0;i < n;i++)
scanf("%d",&a[i]);
Build_Tree(1,0,n-1);
for(int i = 0;i < q;i++)
{
int b,c;
scanf("%d%d",&b,&c);
//cout << Tree[5].frequent << endl;
Node l = query(1,0,n-1,b-1,c-1);
printf("%d\n",l.frequent);
}
}
return 0;
}