#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ent "\n"
const int maxn = 1e6 + 100;
const ll INF = (ll)1e18 + 100;
const int inf = 1e9 + 100;
const int MOD = 1e9 + 123;
const int maxl = 26;
const int P = 31;
int n, m;
int a[maxn];
int b[maxn];
int c[1010][1010];
vector<int> s;
struct asd{
int r, cr;
int l, cl;
int ok[2];
} d[maxn * 4];
int t[maxn * 4];
void calc(int v, int tl, int tr, int mid){
d[v].ok[0] = d[v<<1].ok[0] & d[v<<1|1].ok[0];
d[v].ok[1] = d[v<<1].ok[1] & d[v<<1|1].ok[1];
if(d[v<<1].l > s[tl] && d[v<<1|1].r < s[tr]){
if(d[v<<1].cl == d[v<<1|1].cr){
d[v].ok[d[v<<1].cl] &= (d[v<<1].l ^ d[v<<1|1].r) & 1;
} else{
d[v].ok[d[v<<1].cl] &= (d[v<<1].l ^ s[mid]) & 1;
d[v].ok[d[v<<1|1].cr] &= (d[v<<1|1].r ^ s[mid+1]) & 1;
}
}
d[v].r = d[v<<1].r;
d[v].cr = d[v<<1].cr;
if(d[v<<1|1].cr == d[v].cr && d[v].r == s[mid]) d[v].r = d[v<<1|1].r;
d[v].l = d[v<<1|1].l;
d[v].cl = d[v<<1|1].cl;
if(d[v<<1].cl == d[v].cl && d[v].l == s[mid+1]) d[v].l = d[v<<1].l;
}
void build(int v = 1, int tl = 0, int tr = s.size() - 1){
if(tl == tr){
d[v] = {s[tl], 0, s[tl], 0, {1, 1}};
} else{
int mid = (tl + tr) >> 1;
build(v<<1, tl, mid);
build(v<<1|1, mid+1, tr);
calc(v, tl, tr, mid);
}
}
void pre(int v){
t[v] ^= 1;
d[v].cr ^= 1;
d[v].cl ^= 1;
swap(d[v].ok[0], d[v].ok[1]);
}
void push(int v, int tl, int tr){
if(tl == tr || !t[v]) return;
t[v] = 0; pre(v<<1); pre(v<<1|1);
}
void upd(int l, int r, int v = 1, int tl = 0, int tr = s.size()-1){
push(v, tl, tr);
if(s[tl] > r || s[tr] < l) return;
if(l <= s[tl] && s[tr] <= r) pre(v);
else{
int mid = (tl + tr) >> 1;
upd(l, r, v<<1, tl, mid);
upd(l, r, v<<1|1 ,mid+1, tr);
calc(v, tl, tr, mid);
}
}
void out(int v = 1, int tl = 0, int tr = s.size() - 1){
push(v, tl, tr);
if(tl == tr) cout << d[v].cl << ' ';
else{
int mid = (tl + tr) >> 1;
out(v<<1, tl, mid);
out(v<<1|1, mid+1, tr);
}
}
void test(){
cin >> n >> m;
int mx = 0;
bool ok = 1;
for(int i = 1; i <= n; i++){
cin >> a[i] >> b[i];
mx = max(mx, b[i]);
ok &= (a[i] & 1);
}
if(ok){
vector<pair<int, pii>> g;
for(int i = 1; i <= n; i++){
int j = i - 1;
if(!j) j = n;
if(a[i] == a[j]){
int l = min(b[i], b[j]);
int r = b[i] + b[j] - l;
r--;
s.push_back(l);
s.push_back(r);
s.push_back(r+1);
s.push_back(l-1);
g.push_back({a[i], {l, r}});
}
}
sort(s.begin(), s.end());
s.resize(unique(s.begin(), s.end()) - s.begin());
build(); int ans = 0;
sort(g.begin(), g.end());
for(int i = 0; i < g.size(); i++){
auto [l, r] = g[i].second;
upd(l, r);
// cout << l << ' ' << r << endl;
if(i + 1 < g.size() && g[i].first < g[i+1].first){
// out(); cout << endl;
int f = d[1].ok[1];
if(d[1].cr) f &= (d[1].r ^ s[0]) & 1;
if(d[1].cl) f &= (d[1].l ^ s.back()) & 1;
if(f){
ans = g[i].first + 2;
} else break;
}
}
cout << ans;
} else if(m > 1000 || mx > 1000){
vector<int> v;
for(int i = 2; i <= n; i++){
while(v.size() > 1){
int k = v[v.size()-2], j = v.back();
if(a[k] == a[j] && a[j] == a[i]) v.pop_back();
else if(b[k] == b[j] && b[j] == b[i]) v.pop_back();
else break;
} v.push_back(i);
}
int ans = 0;
for(int i: v){
if((b[i] & 1 )|| !b[i]) break;
ans = a[i] - (a[i] & 1);
if(a[i] & 1) break;
}
cout << ans;
} else{
for(int i = 1; i <= n; i++){
int j = i - 1;
if(!j) j = n;
if(a[i] == a[j]){
int l = min(b[i], b[j]);
int r = b[i] + b[j] - l;
for(int y = l; y < r; y++){
c[a[i]][y] ^= 1;
}
}
}
for(int i = 0; i < m; i++){
for(int j = 0; j <= 1000; j++){
if(i) c[i][j] ^= c[i-1][j];
}
}
int ans = 0;
for(int i = 0; i < m; i++){
int ok = 0;
for(int j = 0; j <= 1000; j++){
if(c[i][j]){
ok = 1;
if(c[i+1][j] && c[i][j+1] && c[i+1][j+1]){
c[i][j] = 0;
c[i+1][j] = 0;
c[i][j+1] = 0;
c[i+1][j+1] = 0;
} else{
ok = 2;
break;
}
}
}
if(ok == 2) break;
if(ok == 1) continue;
ans = i + 1;
}
cout << ans;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t; t = 1;
while(t--) test();
cout << ent;
}
Compilation message
Main.cpp: In function 'void test()':
Main.cpp:120:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for(int i = 0; i < g.size(); i++){
| ~~^~~~~~~~~~
Main.cpp:124:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | if(i + 1 < g.size() && g[i].first < g[i+1].first){
| ~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Incorrect |
0 ms |
6492 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Incorrect |
0 ms |
6492 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
5 ms |
7004 KB |
Output is correct |
3 |
Correct |
26 ms |
7644 KB |
Output is correct |
4 |
Correct |
14 ms |
7136 KB |
Output is correct |
5 |
Correct |
13 ms |
7264 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
27 ms |
7764 KB |
Output is correct |
9 |
Correct |
14 ms |
7132 KB |
Output is correct |
10 |
Correct |
35 ms |
7636 KB |
Output is correct |
11 |
Correct |
16 ms |
7132 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
26 ms |
7644 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
15 ms |
8540 KB |
Output is correct |
6 |
Correct |
15 ms |
8668 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
3 ms |
8540 KB |
Output is correct |
11 |
Correct |
2 ms |
8672 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
2 ms |
8540 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
18 ms |
8668 KB |
Output is correct |
18 |
Correct |
16 ms |
8664 KB |
Output is correct |
19 |
Correct |
3 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8540 KB |
Output is correct |
21 |
Correct |
1 ms |
8540 KB |
Output is correct |
22 |
Correct |
1 ms |
8540 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
8536 KB |
Output is correct |
25 |
Correct |
2 ms |
8540 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
8668 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
8540 KB |
Output is correct |
31 |
Correct |
1 ms |
8540 KB |
Output is correct |
32 |
Correct |
1 ms |
8540 KB |
Output is correct |
33 |
Correct |
2 ms |
8540 KB |
Output is correct |
34 |
Correct |
1 ms |
6492 KB |
Output is correct |
35 |
Correct |
1 ms |
6492 KB |
Output is correct |
36 |
Correct |
2 ms |
8540 KB |
Output is correct |
37 |
Correct |
2 ms |
8536 KB |
Output is correct |
38 |
Correct |
2 ms |
8540 KB |
Output is correct |
39 |
Correct |
1 ms |
8540 KB |
Output is correct |
40 |
Correct |
2 ms |
8540 KB |
Output is correct |
41 |
Correct |
1 ms |
8540 KB |
Output is correct |
42 |
Correct |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
1 ms |
6492 KB |
Output is correct |
44 |
Correct |
1 ms |
6492 KB |
Output is correct |
45 |
Correct |
1 ms |
6492 KB |
Output is correct |
46 |
Correct |
1 ms |
8664 KB |
Output is correct |
47 |
Correct |
1 ms |
6492 KB |
Output is correct |
48 |
Correct |
2 ms |
8536 KB |
Output is correct |
49 |
Correct |
15 ms |
8672 KB |
Output is correct |
50 |
Correct |
18 ms |
8924 KB |
Output is correct |
51 |
Correct |
15 ms |
8540 KB |
Output is correct |
52 |
Correct |
20 ms |
8660 KB |
Output is correct |
53 |
Correct |
2 ms |
8536 KB |
Output is correct |
54 |
Correct |
1 ms |
6492 KB |
Output is correct |
55 |
Correct |
3 ms |
8540 KB |
Output is correct |
56 |
Correct |
2 ms |
8540 KB |
Output is correct |
57 |
Correct |
1 ms |
8540 KB |
Output is correct |
58 |
Correct |
1 ms |
6488 KB |
Output is correct |
59 |
Correct |
2 ms |
8540 KB |
Output is correct |
60 |
Correct |
1 ms |
6492 KB |
Output is correct |
61 |
Correct |
1 ms |
6492 KB |
Output is correct |
62 |
Correct |
1 ms |
6492 KB |
Output is correct |
63 |
Correct |
3 ms |
8540 KB |
Output is correct |
64 |
Correct |
2 ms |
8540 KB |
Output is correct |
65 |
Correct |
2 ms |
8540 KB |
Output is correct |
66 |
Correct |
1 ms |
8540 KB |
Output is correct |
67 |
Correct |
2 ms |
8656 KB |
Output is correct |
68 |
Correct |
2 ms |
8540 KB |
Output is correct |
69 |
Correct |
2 ms |
8540 KB |
Output is correct |
70 |
Correct |
2 ms |
8668 KB |
Output is correct |
71 |
Correct |
2 ms |
8540 KB |
Output is correct |
72 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
11 ms |
7268 KB |
Output is correct |
3 |
Correct |
11 ms |
7136 KB |
Output is correct |
4 |
Correct |
14 ms |
8668 KB |
Output is correct |
5 |
Correct |
14 ms |
8668 KB |
Output is correct |
6 |
Correct |
2 ms |
8536 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Incorrect |
22 ms |
7772 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
7636 KB |
Output is correct |
2 |
Correct |
15 ms |
7136 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Incorrect |
24 ms |
7768 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Incorrect |
0 ms |
6492 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |