-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguard_the_wall.cpp
More file actions
112 lines (99 loc) · 3.75 KB
/
Copy pathguard_the_wall.cpp
File metadata and controls
112 lines (99 loc) · 3.75 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
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
#include <vector>
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
// int main(){
// int t;
// cin >> t;
// for(int i=0; i<t; i++){
// int n,q;
// cin >> n >> q;
// vector<int> sections(n+1,0);
// vector<pair<int,int>> soldiers(q);
// vector<int> prefix1(n+1,0), prefix2(n+1,0);
// for(int j=0; j<q; j++){
// pair<int,int> val;
// cin >> val.first >> val.second;
// soldiers[j] = val;
// for(int l=val.first; l<=val.second; l++){
// sections[l] += 1;
// }
// }
// for(int i=1; i<=n; i++){
// prefix2[i] = prefix2[i-1];
// prefix1[i] = prefix1[i-1];
// if(sections[i] <= 2) prefix2[i] ++;
// if(sections[i] <= 1) prefix1[i] ++;
// }
// sort(soldiers.begin(), soldiers.end());
// int sections_cnt = 0;
// for(int j=1; j<=n; j++){
// sections_cnt += sections[j] > 0;
// }
// int max_val = INT_MIN;
// for(int j=0; j<soldiers.size(); j++){
// for(int k=j+1; k<soldiers.size(); k++){
// int unguard_section = 0;
// if(soldiers[j].second < soldiers[k].first){
// unguard_section += prefix1[soldiers[k].second] - prefix1[soldiers[k].first-1];
// unguard_section += prefix1[soldiers[j].second] - prefix1[soldiers[j].first-1];
// }else{
// unguard_section += prefix1[soldiers[k].second] - prefix1[soldiers[j].second];
// unguard_section += prefix2[soldiers[j].second] - prefix2[soldiers[k].first-1];
// unguard_section += prefix1[soldiers[k].first-1] - prefix1[soldiers[j].first-1];
// }
// max_val = max(max_val,sections_cnt-unguard_section);
// }
// }
// cout << max_val << endl;
// }
// return 0;
// }
int main(){
int t;
cin >> t;
for(int i=0; i<t; i++){
int n,q;
cin >> n >> q;
vector<int> sections(n+1,0);
vector<pair<int,int>> soldiers(q);
vector<int> prefix1(n+1,0), prefix2(n+1,0);
for(int j=0; j<q; j++){
pair<int,int> val;
cin >> val.first >> val.second;
soldiers[j] = val;
for(int l=val.first; l<=val.second; l++){
sections[l] += 1;
}
}
for(int i=1; i<=n; i++){
prefix2[i] = prefix2[i-1];
prefix1[i] = prefix1[i-1];
if(sections[i] <= 2) prefix2[i] ++;
if(sections[i] <= 1) prefix1[i] ++;
}
sort(soldiers.begin(), soldiers.end());
int sections_cnt = 0;
for(int j=1; j<=n; j++){
sections_cnt += sections[j] > 0;
}
int max_val = INT_MIN;
for(int j=0; j<soldiers.size(); j++){
for(int k=j+1; k<soldiers.size(); k++){
int unguard_section = 0;
if(soldiers[j].second < soldiers[k].first){
unguard_section += prefix1[soldiers[k].second] - prefix1[soldiers[k].first-1];
unguard_section += prefix1[soldiers[j].second] - prefix1[soldiers[j].first-1];
}else{
unguard_section += prefix1[soldiers[k].second] - prefix1[soldiers[j].second];
unguard_section += prefix2[soldiers[j].second] - prefix2[soldiers[k].first-1];
unguard_section += prefix1[soldiers[k].first-1] - prefix1[soldiers[j].first-1];
}
max_val = max(max_val,sections_cnt-unguard_section);
}
}
cout << max_val << endl;
}
return 0;
}