forked from sameer2800/Lightoj-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1207-posters_for_elections.cpp
181 lines (112 loc) · 2.75 KB
/
1207-posters_for_elections.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <bits/stdc++.h>
#define ll long long
#define MAX 100100
using namespace std;
ll arr[200100];
ll fcolor[200100];
vector <pair <ll,ll> > v;
set <ll > sets;
struct sam {
int lazy;
int color;
};
struct sam nodes[1000100];
void initilize(int node)
{
nodes[node].lazy = -1;
nodes[node].color = -1;
}
void buildtree(int node,int start,int end,int val)
{
if(start > end)
return;
if(start == end)
{
initilize(node);
return;
}
int mid = start + end;
mid /= 2;
buildtree(node*2+1,start ,mid,val);
buildtree(node*2+2,mid+1,end,val);
initilize(node);
return;
}
void update(int node,int start,int end,int qstr,int qend,int val)
{
if(nodes[node].lazy != -1) {
int temp = nodes[node].lazy ;
if(start != end)
nodes[node*2+1].lazy = temp, nodes[node*2+2].lazy = temp;
nodes[node].color = temp;
nodes[node].lazy = -1;
}
if(start > end)
return;
if(qend < start || qstr > end)
return;
if(qstr <= start and qend >= end) {
int temp = val;
if(start != end)
nodes[node*2+1].lazy = temp, nodes[node*2+2].lazy = temp;
nodes[node].color = temp;
nodes[node].lazy = -1;
return;
}
int mid =start + end;
mid /= 2;
update(node*2 +1,start,mid,qstr,qend,val);
update(node*2+2,mid+1,end,qstr,qend,val);
}
ll query(int node,int start,int end,int qstr,int qend)
{
if(nodes[node].lazy != -1) {
int temp = nodes[node].lazy ;
if(start != end)
nodes[node*2+1].lazy = temp, nodes[node*2+2].lazy = temp;
nodes[node].color = temp;
nodes[node].lazy = -1;
}
if(start > end)
return INT_MAX;
if(qend < start || qstr > end)
return INT_MAX;
if(qstr <= start and qend >= end) {
return nodes[node].color;
}
int mid =start + end;
mid /= 2;
int left = query(node*2 +1,start,mid,qstr,qend);
int right = query(node*2+2,mid+1,end,qstr,qend);
return min(left ,right);
}
int main()
{
ll i,j,k,n,m,t,cont,a,b;
scanf("%lld",&t);
ll cases = t;
while(t--){
scanf("%lld",&m);
n = 2*m;
memset(nodes,0,sizeof nodes);
sets.clear();
buildtree(0,0,n-1,0);
for(i =0;i < m;i++) {
scanf("%lld %lld",&a,&b);
a--,b--;
update(0,0,n-1,a,b,i+1);
}
for(i =0;i < n;i++) {
fcolor[i] = query(0,0,n-1,i,i);
// cout << fcolor[i] << " ";
}
ll ans =0;
for(i=0;i < n-1;i++) {
if(fcolor[i] != -1)
sets.insert(fcolor[i]);
}
ll yooo = sets.size() ;
printf("Case %lld: %lld\n",cases-t,yooo);
}
return 0;
}