QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#116577 | #4887. Fast Bridges | hos_lyric | RE | 3ms | 11496kb | C++14 | 7.4kb | 2023-06-29 15:37:38 | 2023-06-29 15:37:39 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;
int N;
Int K;
vector<Int> X0, Y0, X1, Y1;
int dp[510][510];
int DP[510][510];
vector<int> vsss[510][510];
struct Stair {
Int x, y;
Int area;
void init() {
x = K + 1;
y = 0;
area = 0;
}
void add(Int xx, Int yy) {
// assert(x >= xx);
area += (x - xx) * y;
x = xx;
chmax(y, yy);
}
Int get() {
add(0, 0);
return area;
}
};
Stair stairs[510];
int main() {
for (; ~scanf("%d%lld", &N, &K); ) {
X0.resize(N);
Y0.resize(N);
X1.resize(N);
Y1.resize(N);
for (int i = 0; i < N; ++i) {
scanf("%lld%lld%lld%lld", &X0[i], &Y0[i], &X1[i], &Y1[i]);
}
vector<int> iss[2];
for (int i = 0; i < N; ++i) {
if (X0[i] > X1[i]) {
swap(X0[i], X1[i]);
swap(Y0[i], Y1[i]);
}
if (Y0[i] < Y1[i]) {
iss[0].push_back(i);
} else {
Y0[i] = K + 1 - Y0[i];
Y1[i] = K + 1 - Y1[i];
iss[1].push_back(i);
}
}
Mint ans = 0;
for (int dir = 0; dir < 2; ++dir) {
auto &is = iss[dir];
const int isLen = is.size();
sort(is.begin(), is.end(), [&](int i, int j) -> bool {
return ((X0[i] != X0[j]) ? (X0[i] < X0[j]) : (Y0[i] < Y1[j]));
});
// cerr<<"is = "<<is<<endl;
// from fast to fast
for (int u = 0; u < isLen; ++u) {
fill(dp[u], dp[u] + isLen, 0);
dp[u][u] = 1;
}
for (int u = 0; u < isLen; ++u) {
for (int v = u; v < isLen; ++v) for (int w = v + 1; w < isLen; ++w) {
if (X1[is[v]] <= X0[is[w]] && Y1[is[v]] <= Y0[is[w]]) {
chmax(dp[u][w], dp[u][v] + 1);
}
}
}
// to (x, y) from fast
vector<Int> xs{1, K + 1}, ys{1, K + 1};
for (int u = 0; u < isLen; ++u) {
xs.push_back(X1[is[u]]);
ys.push_back(Y1[is[u]]);
}
sort(xs.begin(), xs.end());
sort(ys.begin(), ys.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
const int xsLen = xs.size();
const int ysLen = ys.size();
for (int e = 0; e < xsLen; ++e) for (int f = 0; f < ysLen; ++f) {
vsss[e][f].clear();
}
for (int v = 0; v < isLen; ++v) {
const int e = lower_bound(xs.begin(), xs.end(), X1[is[v]]) - xs.begin();
const int f = lower_bound(ys.begin(), ys.end(), Y1[is[v]]) - ys.begin();
vsss[e][f].push_back(v);
}
for (int f = 0; f < ysLen; ++f) {
fill(DP[f], DP[f] + isLen, 0);
}
for (int e = 0; e < xsLen - 1; ++e) {
for (int f = 0; f < ysLen - 1; ++f) {
for (int u = 0; u < isLen; ++u) {
for (const int v : vsss[e][f]) {
chmax(DP[f][u], dp[u][v]);
}
}
}
for (int f = 1; f < ysLen - 1; ++f) {
for (int u = 0; u < isLen; ++u) {
chmax(DP[f][u], DP[f - 1][u]);
}
}
for (int f = 0; f < ysLen - 1; ++f) {
// (stair of DP >= z) = (stair of DP == z)
for (int z = 0; z <= isLen; ++z) {
stairs[z].init();
}
for (int u = isLen; --u >= 0; ) {
stairs[DP[f][u]].add(X0[is[u]], Y0[is[u]]);
}
Int score = 0;
for (int z = 1; z <= isLen; ++z) {
score += stairs[z].get();
}
// cerr<<"["<<xs[e]<<","<<xs[e+1]<<") ["<<ys[f]<<","<<ys[f+1]<<"): "<<score<<"; DP[f] = ";pv(DP[f],DP[f]+isLen);
ans += Mint(xs[e + 1] - xs[e]) * Mint(ys[f + 1] - ys[f]) * Mint(score);
}
}
}
ans = 2 * Mint(K+1) * Mint(K) * Mint(K-1) / 6 * Mint(K) * Mint(K) - ans;
printf("%u\n", ans.x);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 11356kb
input:
2 2 1 1 2 2 1 2 2 1
output:
6
result:
ok answer is '6'
Test #2:
score: 0
Accepted
time: 3ms
memory: 11212kb
input:
0 1000000000
output:
916520226
result:
ok answer is '916520226'
Test #3:
score: 0
Accepted
time: 3ms
memory: 11496kb
input:
5 5 1 1 3 3 3 3 5 1 3 3 4 5 3 3 5 4 1 5 3 3
output:
946
result:
ok answer is '946'
Test #4:
score: -100
Runtime Error
input:
200 5 1 1 4 2 2 5 4 4 2 3 4 2 2 4 3 5 1 4 4 2 2 5 4 2 1 2 4 4 1 2 2 4 1 4 5 1 3 4 5 1 4 2 5 1 2 2 5 4 3 2 5 1 3 1 5 2 4 2 5 3 1 3 5 1 3 4 4 5 2 2 4 3 2 3 5 4 1 4 5 3 2 2 3 1 2 5 3 3 1 1 5 3 4 4 5 5 1 3 4 4 4 3 5 1 2 3 3 4 3 4 4 2 1 4 4 5 2 1 4 4 1 3 5 2 1 1 3 3 1 5 3 1 1 1 3 5 1 4 3 5 4 5 5 4 1 1 4 ...