QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#364585 | #6304. Rectangle | JCY_ | WA | 14ms | 32704kb | C++14 | 8.2kb | 2024-03-24 15:26:36 | 2024-03-24 15:26:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i128 = __int128;
using u128 = unsigned __int128;
template <typename T>
void chkmax(T &x, const T &y) {
if (x < y) x = y;
}
template <typename T>
void chkmin(T &x, const T &y) {
if (y < x) x = y;
}
constexpr int MOD = 998244353;
void inc(int &x, int y) {
x += y;
if (x >= MOD) x -= MOD;
}
void dec(int &x, int y) {
x -= y;
if (x < 0) x += MOD;
}
int add(int x, int y) {
x += y;
return x >= MOD ? x - MOD : x;
}
int sub(int x, int y) {
x -= y;
return x < 0 ? x + MOD : x;
}
int adj(int x) { return x < 0 ? x + MOD : x; }
int S1(int x) { return (ll)x * (x + 1) / 2 % MOD; }
int S2(int x) { return (i128)x * (x + 1) * (x * 2 + 1) / 6 % MOD; }
struct rectangle {
int xl, xr, yl, yr;
};
constexpr int MAXN = 1e5 + 10, MAXV = 1e9, INF = 0x3f3f3f3f;
int n, mpx[MAXN * 2], mpy[MAXN * 2], szx, szy;
rectangle a[MAXN];
namespace sub1 {
pair<int, int> prf[MAXN * 2], suf[MAXN * 2];
int solve() {
fill(prf, prf + szx + 1, make_pair(-INF, INF));
fill(suf + 1, suf + szx + 2, make_pair(-INF, INF));
for (int i = 1; i <= n; ++i) {
chkmax(prf[a[i].xr].first, a[i].xl);
chkmin(prf[a[i].xr].second, a[i].xr);
chkmax(suf[a[i].xl].first, a[i].xl);
chkmin(suf[a[i].xl].second, a[i].xr);
}
for (int i = 1; i <= szx; ++i) {
chkmax(prf[i].first, prf[i - 1].first);
chkmin(prf[i].second, prf[i - 1].second);
}
for (int i = szx; i >= 1; --i) {
chkmax(suf[i].first, suf[i + 1].first);
chkmin(suf[i].second, suf[i + 1].second);
}
int ret = 0;
for (int i = 1; i <= szx; ++i) {
if (prf[i - 1].first > prf[i - 1].second ||
suf[i + 1].first > suf[i + 1].second) {
continue;
}
if (prf[i - 1].first == -INF && suf[i + 1].first == -INF) {
ret = (ret + (ll)(MAXV + 1) * (S1(mpx[i]) - S1(mpx[i - 1])) -
(ll)MAXV * (mpx[i] - mpx[i - 1]) - (S2(mpx[i]) - S2(mpx[i - 1]))) %
MOD;
} else if (prf[i - 1].first == -INF) {
ret = (ret + (ll)(S1(mpx[i] - 1) - S1(mpx[i - 1] - 1)) *
(mpx[suf[i + 1].second] - mpx[suf[i + 1].first - 1])) %
MOD;
} else if (suf[i + 1].first == -INF) {
ret = (ret + (ll)(S1(MAXV - mpx[i - 1] - 1) - S1(MAXV - mpx[i] - 1)) *
(mpx[prf[i - 1].second] - mpx[prf[i - 1].first - 1])) %
MOD;
} else {
ret = (ret + (ll)(mpx[i] - mpx[i - 1]) *
(mpx[prf[i - 1].second] - mpx[prf[i - 1].first - 1]) %
MOD *
(mpx[suf[i + 1].second] - mpx[suf[i + 1].first - 1])) %
MOD;
}
}
return adj(ret);
}
} // namespace sub1
namespace sub2 {
namespace sgt1 {
int ls(int p) { return p << 1; }
int rs(int p) { return p << 1 | 1; }
int sum[MAXN * 4], cov[MAXN * 4], mx[MAXN * 4];
stack<pair<int *, int>> stk;
void get_down(int p, int l, int r, int x) {
stk.emplace(sum + p, sum[p]);
stk.emplace(mx + p, mx[p]);
stk.emplace(cov + p, cov[p]);
sum[p] = (ll)mpx[x] * (mpx[r] - mpx[l - 1]) % MOD;
mx[p] = x;
cov[p] = x;
}
void push_down(int p, int l, int mid, int r) {
if (cov[p] != -1) {
get_down(ls(p), l, mid, cov[p]);
get_down(rs(p), mid + 1, r, cov[p]);
stk.emplace(cov + p, cov[p]);
cov[p] = -1;
}
}
void push_up(int p) {
stk.emplace(sum + p, sum[p]);
stk.emplace(mx + p, mx[p]);
sum[p] = add(sum[ls(p)], sum[rs(p)]);
mx[p] = mx[rs(p)];
}
void build(int p, int l, int r) {
cov[p] = -1;
sum[p] = (ll)(mpx[r] - mpx[l - 1]) * MAXV % MOD;
mx[p] = szy;
if (l == r) return;
int mid = (l + r) >> 1;
build(ls(p), l, mid);
build(rs(p), mid + 1, r);
}
void update(int p, int l, int r, int ql, int qr, int x) {
if (ql <= l && r <= qr) {
get_down(p, l, r, x);
return;
}
int mid = (l + r) >> 1;
push_down(p, l, mid, r);
if (ql <= mid) update(ls(p), l, mid, ql, qr, x);
if (qr > mid) update(rs(p), mid + 1, r, ql, qr, x);
push_up(p);
}
int find_gt(int p, int l, int r, int x) {
if (mx[p] <= x) return szx + 1;
if (l == r) return l;
int mid = (l + r) >> 1;
push_down(p, l, mid, r);
int tmp = find_gt(ls(p), l, mid, x);
return tmp == szx + 1 ? find_gt(rs(p), mid + 1, r, x) : tmp;
}
int query(int p, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) return sum[p];
int mid = (l + r) >> 1;
push_down(p, l, mid, r);
if (qr <= mid) return query(ls(p), l, mid, ql, qr);
if (ql > mid) return query(rs(p), mid + 1, r, ql, qr);
return add(query(ls(p), l, mid, ql, qr), query(rs(p), mid + 1, r, ql, qr));
}
void undo(int tar) {
for (; (int)stk.size() > tar; stk.pop()) *stk.top().first = stk.top().second;
}
} // namespace sgt1
namespace sgt2 {
int ls(int p) { return p << 1; }
int rs(int p) { return p << 1 | 1; }
vector<pair<int, int>> buc[MAXN * 8];
void build(int p, int l, int r) {
buc[p].clear();
if (l == r) return;
int mid = (l + r) >> 1;
build(ls(p), l, mid);
build(rs(p), mid + 1, r);
}
void insert(int p, int l, int r, int ql, int qr, const pair<int, int> &x) {
if (ql <= l && r <= qr) {
buc[p].emplace_back(x);
return;
}
int mid = (l + r) >> 1;
if (ql <= mid) insert(ls(p), l, mid, ql, qr, x);
if (qr > mid) insert(rs(p), mid + 1, r, ql, qr, x);
}
int solve(int p, int l, int r, int mnr, int mxl) {
int rec = sgt1::stk.size(), ret = 0;
for (auto &i : buc[p]) {
int tmp = sgt1::find_gt(1, 1, szx, i.second);
if (tmp < i.first) sgt1::update(1, 1, szx, tmp, i.first - 1, i.second);
chkmax(mxl, i.first);
chkmin(mnr, i.second);
}
if (l == r) {
int pos = sgt1::find_gt(1, 1, szx, mxl - 1);
if (pos <= mnr) {
// i \in [mpx[pos - 1] + 1, mpx[mnr]]
// - max(i, mpx[mxl - 1])
// i \in [max(mpx[pos - 1] + 1, mpx[mxl - 1]), mpx[mnr]] => -i
// i \in [mpx[pos - 1] + 1, min(mpx[mnr], mpx[mxl - 1] - 1)] => - mpx[mxl - 1]
ret = sgt1::query(1, 1, szx, pos, mnr);
int pl = max(mpx[pos - 1] + 1, mpx[mxl - 1]), pr = mpx[mnr];
if (pl <= pr) dec(ret, sub(S1(pr), S1(pl - 1)));
pl = mpx[pos - 1] + 1;
pr = min(mpx[mnr], mpx[mxl - 1] - 1);
if (pl <= pr) ret = adj((ret - (ll)mpx[mxl - 1] * (pr - pl + 1)) % MOD);
ret = (ll)ret * (mpy[l] - mpy[l - 1]) % MOD;
}
} else {
int mid = (l + r) >> 1;
ret = add(solve(ls(p), l, mid, mnr, mxl), solve(rs(p), mid + 1, r, mnr, mxl));
}
sgt1::undo(rec);
return ret;
}
} // namespace sgt2
int solve() {
sgt2::build(1, 1, szy);
for (int i = 1; i <= n; ++i) {
if (a[i].yl != 1)
sgt2::insert(1, 1, szy, 1, a[i].yl - 1, {a[i].xl, a[i].xr});
if (a[i].yr != szy)
sgt2::insert(1, 1, szy, a[i].yr + 1, szy, {a[i].xl, a[i].xr});
}
sgt1::build(1, 1, szx);
return sgt2::solve(1, 1, szy, szx, 1);
}
} // namespace sub2
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int cas;
cin >> cas;
while (cas--) {
cin >> n;
szx = szy = 0;
for (int i = 1, xl, xr, yl, yr; i <= n; ++i) {
cin >> xl >> yl >> xr >> yr;
mpx[++szx] = xl - 1;
mpx[++szx] = xr;
mpy[++szy] = yl - 1;
mpy[++szy] = yr;
a[i] = {xl, xr, yl, yr};
}
mpx[++szx] = mpy[++szy] = MAXV;
sort(mpx + 1, mpx + szx + 1);
szx = unique(mpx + 1, mpx + szx + 1) - mpx - 1;
sort(mpy + 1, mpy + szy + 1);
szy = unique(mpy + 1, mpy + szy + 1) - mpy - 1;
for (int i = 1; i <= n; ++i) {
a[i].xl = lower_bound(mpx + 1, mpx + szx + 1, a[i].xl - 1) - mpx + 1;
a[i].xr = lower_bound(mpx + 1, mpx + szx + 1, a[i].xr) - mpx;
a[i].yl = lower_bound(mpy + 1, mpy + szy + 1, a[i].yl - 1) - mpy + 1;
a[i].yr = lower_bound(mpy + 1, mpy + szy + 1, a[i].yr) - mpy;
}
int ans = add(sub1::solve(), sub2::solve());
swap_ranges(mpx + 1, mpx + max(szx, szy) + 1, mpy + 1);
swap(szx, szy);
for (int i = 1; i <= n; ++i) {
swap(a[i].xl, a[i].yl);
swap(a[i].xr, a[i].yr);
}
inc(ans, add(sub1::solve(), sub2::solve()));
cout << ans << "\n";
}
return 0;
}
/*
g++ G.cpp -o G -O2 -std=c++14 -Wall -Wextra -Wshadow -g
-fsanitize=address,undefined
*/
/*
1
1
1 1 1000000000 1000000000
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 32704kb
input:
3 1 1 1 1000000000 1000000000 3 1 1 2 2 3 3 4 4 5 5 6 6 5 581574116 47617804 999010750 826131769 223840663 366320907 613364068 926991396 267630832 51913575 488301124 223957497 217461197 492085159 999485867 913732845 28144453 603781668 912516656 993160442
output:
230616300 64 977066618
result:
ok 3 number(s): "230616300 64 977066618"
Test #2:
score: -100
Wrong Answer
time: 14ms
memory: 32184kb
input:
1000 10 5 7 6 10 4 3 6 9 1 6 3 7 1 1 7 10 1 2 7 7 5 2 8 3 2 1 5 7 1 1 10 6 1 7 2 8 4 7 5 9 10 6 6 8 10 4 4 9 9 2 4 6 5 5 3 10 10 1 3 4 4 2 2 3 6 7 5 8 6 2 7 8 8 5 7 10 10 2 4 7 8 10 3 6 6 10 1 2 7 4 7 5 10 9 4 5 8 9 2 7 5 9 2 2 9 7 3 3 8 4 1 1 8 6 5 4 10 6 3 9 7 10 10 3 1 8 9 1 3 8 10 4 1 7 4 2 4 5 ...
output:
28090346 26334726 91293528 63203269 14045217 24579047 52669397 57936293 10533942 83 28090372 105338612 519923976 38624242 22823398 54425054 421607915 7022661 7022622 31601641 21067817 35112979 12289539 7022682 17556485 45646828 59692019 57936298 14045184 73737099 42135505 31601703 49158091 49158078 ...
result:
wrong answer 2nd numbers differ - expected: '21067815', found: '26334726'