-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegment Tree.cpp
70 lines (58 loc) · 1.81 KB
/
Segment Tree.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
#include <bits/stdc++.h>
#define MAXN 1000
#define NEUTRO -1 << 30
using namespace std;
inline int LEFT(int n){return 2 * n;}
inline int RIGTH(int n){return 2*n + 1;}
int oper(int a, int b)
{
return max(a, b);
}
void inicializar(int n, int ini, int fin, int*Segment_Tree, int* Array)
{
if(ini == fin) Segment_Tree[n] = Array[ini];
else{
inicializar( LEFT(n), ini, (ini + fin) / 2 , Segment_Tree, Array);
inicializar( RIGTH(n), ((ini + fin) / 2) + 1, fin , Segment_Tree, Array);
Segment_Tree[n] = oper( Segment_Tree[LEFT(n)], Segment_Tree[RIGTH(n)] );
}
}
void update(int n, int ini,int fin, int pos, int *Segment_Tree,int *Array)
{
if(ini == fin)
Segment_Tree[n] = ++Array[ini];
else{
if( pos <= (ini + fin) / 2)
update(LEFT(n), ini, (ini + fin) / 2 , pos, Segment_Tree, Array);
else
update(RIGTH(n), ((ini + fin) / 2 + 1), fin, pos , Segment_Tree, Array);
Segment_Tree[n] = oper( Segment_Tree[ LEFT(n) ], Segment_Tree[ RIGTH(n) ] );
}
}
int query(int n, int ini, int fin, int a, int b, int* Segment_Tree)
{
if( fin <= a || ini >= b) return NEUTRO;
else if(ini >= a && fin <= b)return Segment_Tree[n];
else{
int l = query( LEFT(n), ini, (ini + fin) / 2, a, b , Segment_Tree);
int r = query(RIGTH(n), ((ini + fin) / 2) + 1, fin, a, b , Segment_Tree);
return oper( l , r);
}
}
int Segment_Tree[MAXN], Array[MAXN];
int main()
{
int n, a, b;
cin>> n;
inicializar(1, 1, n, Segment_Tree, Array);
for(int i = 1;i <= n;i++){
cin>> a>> b;
for(int j = a;j <= b;j++)
update(1, 1, n, j, Segment_Tree, Array);
}
for(int i = 1;i <= n;i++)
cout<<Segment_Tree[i]<<" ";
cout<<endl;
cout<< query(1, 1, n, 1, n, Segment_Tree);
return 0;
}