-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFREQUENT.cpp
More file actions
67 lines (67 loc) · 1.33 KB
/
FREQUENT.cpp
File metadata and controls
67 lines (67 loc) · 1.33 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
#include <bits/stdc++.h>
using namespace std;
struct node
{
int res,pre,suf,pv,sv;
void merge(node a,node b)
{
pre = a.pre;
suf = b.suf;
pv = a.pv;
sv = b.sv;
if(a.pv == b.pv )
pre = a.pre + b.pre;
if(a.sv == b.sv)
suf = a.suf + b.suf;
res = max(a.res,b.res);
if(a.sv == b.pv)
res = max(res , a.suf + b.pre);
}
node(){
res = pre = suf = sv = pv = 0;
}
node(int val){
res = pre = suf =1;
sv = pv = val;
}
}tree[500000];
void create(int pos)
{
pos>>=1;
while(pos){
tree[pos].merge(tree[pos<<1],tree[(pos<<1)+1]);
pos>>=1;
}
}
node query(int root,int l_most,int r_most,int l,int r)
{
if(l_most >= l && r_most <= r)
return tree[root];
int l_child = (root<<1) , r_child = l_child + 1,mid = (l_most + r_most )>>1;
node left=node(),right = node();
if(l<=mid)
left = query(l_child,l_most,mid,l,r);
if(r>mid)
right = query(r_child,mid+1,r_most,l,r);;
node temp = node();
temp.merge(left,right);
return temp;
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
int k = ceil(log(n)/log(2));
int pos = (1<<k),temp;
for(int i=0;i<n;i++){
scanf("%d",&temp);
tree[pos+i] = node(temp);
create(pos+i);
}
for(int i=0;i<q;i++){
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",query(1,1,pos,l,r).res);
}
return 0;
}