This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define all(arr) (arr).begin(), (arr).end()
#define ll long long
#define ld long double
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define endl '\n'
int d;
int calc(int x){
x %= d;
if (x < 0) x += d;
return x;
}
struct linkedlist{
int n;
int maxval;
vector<int> nxt, prv, val;
int calcdist(int i){
if (nxt[i] > i) return val[nxt[i]] - val[i];
else return val[nxt[i]] - val[i] + d;
}
linkedlist(){};
linkedlist(vector<pair<int, int>>& a){
n = sz(a);
nxt.resize(n);
prv.resize(n);
val.resize(n);
iota(all(nxt), 1);
nxt[n - 1] = 0;
iota(all(prv), -1);
prv[0] = n - 1;
for (int i = 0; i < n; i++) val[i] = a[i].fi;
maxval = 0;
for (int i = 0; i < n; i++){
maxval = max(maxval, calcdist(i));
}
};
void del(int idx){
if (nxt[idx] == idx) return;
nxt[prv[idx]] = nxt[idx];
prv[nxt[idx]] = prv[idx];
maxval = max(maxval, calcdist(prv[idx]));
}
void add(int idx, int nxtidx){
int prvidx = prv[nxtidx];
nxt[prvidx] = prv[nxtidx] = idx;
prv[idx] = prvidx, nxt[idx] = nxtidx;
}
int get(){
return maxval;
}
};
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// cout.precision(20);
int n, m;
cin >> n >> m >> d;
vector<int> x(n + m), y(n + m), nodeid(n + m);
vector<bool> used(n);
vector<pair<int, int>> sy(n + m);
vector<int> cnt(d), preva(n + m);
vector<vector<int>> nxtidx(d, vector<int>(d, -1)), gonext(n);
for (int i = 0; i < n + m; i++){
cin >> x[i] >> y[i];
if (i < n) cnt[x[i]]++;
sy[i] = {y[i], i};
nxtidx[x[i]][y[i]] = i;
}
for (int val = 0; val < d; val++){
int idx = 0;
while (idx < d && nxtidx[idx][val] == -1) idx++;
if (idx == d) continue;
for (int i = calc(idx - 1), last = nxtidx[idx][val]; i != idx; i = calc(i - 1)){
if (nxtidx[i][val] != -1) last = nxtidx[i][val];
nxtidx[i][val] = last;
}
}
sort(all(sy));
linkedlist ls(sy);
for (int i = 0; i < n + m; i++) nodeid[sy[i].se] = i;
int startmaxval = ls.maxval;
for (int i = n; i < n + m; i++) ls.del(nodeid[i]);
sy.insert(sy.end(), all(sy));
int lasta;
for (auto [val, i] : sy){
if (i < n) lasta = i;
preva[i] = lasta;
}
int ans = 1e9;
for (int start = 0; start < d; start++){
vector<vector<int>> hasx(d);
vector<int> usedvector;
for (int val = 0; val < d; val++){
int cur = nxtidx[start][val];
if (cur == -1 || cur < n || nodeid[preva[cur]] > nodeid[cur]) continue;
hasx[x[cur]].pb(cur);
gonext[preva[cur]].pb(cur);
if (used[preva[cur]]) continue;
usedvector.pb(preva[cur]);
used[preva[cur]] = 1;
}
for (int val = 0; val < d; val++){
int cur = nxtidx[start][val];
if (cur == -1 || cur < n || nodeid[preva[cur]] < nodeid[cur]) continue;
hasx[x[cur]].pb(cur);
gonext[preva[cur]].pb(cur);
if (used[preva[cur]]) continue;
usedvector.pb(preva[cur]);
used[preva[cur]] = 1;
}
for (auto i : usedvector){
int nextnode = ls.nxt[nodeid[i]];
for (auto v : gonext[i]) ls.add(nodeid[v], nextnode);
}
ls.maxval = startmaxval;
int acnt = n;
for (int finish = calc(start - 1);; finish = calc(finish - 1)){
if (acnt == 0){
ans = min(ans, (d - calc(finish - start) - 1) * (d + 1 - ls.get()));
}
for (auto i : hasx[finish]){
ls.del(nodeid[i]);
}
acnt -= cnt[finish];
if (finish == start){
ans = min(ans, d * (d + 1 - ls.get()));
break;
}
}
for (auto i : usedvector){
used[i] = 0;
gonext[i].clear();
}
}
cout << ans << endl;
}
Compilation message (stderr)
garden.cpp: In function 'int main()':
garden.cpp:101:14: warning: 'lasta' may be used uninitialized in this function [-Wmaybe-uninitialized]
101 | preva[i] = lasta;
# | 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... |