This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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){
| ~~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |