QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#563934 | #6673. Be Careful 2 | liuziao | WA | 2544ms | 179740kb | C++23 | 7.3kb | 2024-09-14 17:29:08 | 2024-09-14 17:29:08 |
Judging History
answer
#include <bits/stdc++.h>
#define int int64_t
const int kMaxK = 5e3 + 5, kMod = 998244353;
int n, m, k, cx, cy;
int x[kMaxK], y[kMaxK], unqx[kMaxK], unqy[kMaxK], cnt[kMaxK][kMaxK];
int nxt[kMaxK], pre[kMaxK];
std::vector<int> vec[kMaxK];
constexpr int qpow(int bs, int64_t idx = kMod - 2) {
int ret = 1;
for (; idx; idx >>= 1, bs = (int64_t)bs * bs % kMod)
if (idx & 1) ret = (int64_t)ret * bs % kMod;
return ret;
}
inline int fix(int x) { return (x % kMod + kMod) % kMod; }
inline int add(int x, int y) { return (x + y >= kMod ? x + y - kMod : x + y); }
inline int sub(int x, int y) { return (x >= y ? x - y : x - y + kMod); }
inline void inc(int &x, int y) { (x += y) >= kMod ? x -= kMod : x; }
inline void dec(int &x, int y) { (x -= y) < 0 ? x += kMod : x; }
int getop(int x) { return (~x & 1) ? 1 : (kMod - 1); }
int calc1(int x) {
static constexpr int kInv2 = qpow(2);
return 1ll * x * (x + 1) % kMod * kInv2 % kMod;
}
int calc1(int l, int r) { return (l <= r) ? sub(calc1(r), calc1(l - 1)) : 0; }
int calc2(int x) {
static constexpr int kInv6 = qpow(6);
return 1ll * x * (x + 1) % kMod * (2ll * x + 1) % kMod * kInv6 % kMod;
}
int calc2(int l, int r) { return (l <= r) ? sub(calc2(r), calc2(l - 1)) : 0; }
int calc3(int x) {
int sum = calc1(x);
return 1ll * sum * sum % kMod;
}
int calc3(int l, int r) { return (l <= r) ? sub(calc3(r), calc3(l - 1)) : 0; }
int calc4(int x) {
static constexpr int kInv30 = qpow(30);
return 1ll * x * (x + 1) % kMod * (2 * x + 1) % kMod *
((3ll * x * x + 3ll * x + kMod - 1) % kMod) % kMod * kInv30 % kMod;
}
int calc4(int l, int r) { return (l <= r) ? sub(calc4(r), calc4(l - 1)) : 0; }
int calc(int k1, int b1, int k2, int b2, int l, int r) {
int sum2 = 1ll * b1 * b2 % kMod * calc2(l, r) % kMod;
int sum3 = 1ll * add(1ll * k1 * b2 % kMod, 1ll * k2 * b1 % kMod) * calc3(l, r) % kMod;
int sum4 = 1ll * k1 * k2 % kMod * calc4(l, r) % kMod;
return add(sum2, add(sum3, sum4));
// int ret = 0;
// for (int i = l; i <= r; ++i) inc(ret, 1ll * i * i % kMod * (1ll * k1 * i % kMod + b1) % kMod * ((1ll * k2 * i % kMod + b2) % kMod) % kMod);
// return ret;
}
int getcnt(int _x1, int _y1, int _x2, int _y2) {
if (_x1 > _x2 || _y1 > _y2) return 0;
return cnt[_x2][_y2] - cnt[_x1 - 1][_y2] - cnt[_x2][_y1 - 1] +
cnt[_x1 - 1][_y1 - 1];
}
bool check(int _x1, int _y1, int _x2, int _y2) {
if (_x1 > _x2 || _y1 > _y2) return 0;
return !getcnt(_x1 + 1, _y1 + 1, _x2 - 1, _y2 - 1) &&
getcnt(_x1, _y1, _x1, _y2) && getcnt(_x1, _y2, _x2, _y2) &&
getcnt(_x2, _y1, _x2, _y2) && getcnt(_x1, _y1, _x2, _y1);
}
int solve(int _x1, int _y1, int _x2, int _y2) {
if (_x1 > _x2 || _y1 > _y2) return 0;
// std::cerr << getcnt(_x1, _y1, _x2, _y2) << " : ";
_x1 = unqx[_x1], _y1 = unqy[_y1], _x2 = unqx[_x2], _y2 = unqy[_y2];
int l = std::max(_x2 - _x1 + 2, _y2 - _y1 + 2), r = std::min(n, m);
if (l > r) return 0;
std::vector<int> v = {l, r + 1, _x2 + 1, n - _x1 + 1, _y2 + 1, m - _y1 + 1};
std::sort(v.begin(), v.end()), v.erase(std::unique(v.begin(), v.end()), v.end());
int ret = 0;
for (int i = 0; i + 1 < (int)v.size(); ++i) {
if (v[i] >= l && v[i] < r + 1) {
int L = v[i], R = v[i + 1] - 1, kx = 0, bx = 0, ky = 0, by = 0;
if (_x1 - 1 <= n - R) inc(bx, _x1 - 1);
else dec(kx, 1), inc(bx, n);
if (0 <= _x2 - R + 1) inc(kx, 1), dec(bx, _x2);
else inc(bx, 1);
if (_y1 - 1 <= m - R) inc(by, _y1 - 1);
else dec(ky, 1), inc(by, m);
if (0 <= _y2 - R + 1) inc(ky, 1), dec(by, _y2);
else inc(by, 1);
inc(ret, calc(kx, bx, ky, by, L, R));
}
}
// std::cerr << _x1 << ' ' << _y1 << ' ' << _x2 << ' ' << _y2 << ' ' << ret << '\n';
return ret;
}
void discrete() {
std::sort(unqx + 1, unqx + 1 + cx);
cx = std::unique(unqx + 1, unqx + 1 + cx) - (unqx + 1);
std::sort(unqy + 1, unqy + 1 + cy);
cy = std::unique(unqy + 1, unqy + 1 + cy) - (unqy + 1);
for (int i = 1; i <= k; ++i) {
x[i] = std::lower_bound(unqx + 1, unqx + 1 + cx, x[i]) - unqx;
y[i] = std::lower_bound(unqy + 1, unqy + 1 + cy, y[i]) - unqy;
++cnt[x[i]][y[i]], vec[x[i]].emplace_back(y[i]);
}
}
void del(int x) {
if (pre[x] && nxt[x]) nxt[pre[x]] = nxt[x];
if (pre[x] && nxt[x]) pre[nxt[x]] = pre[x];
pre[x] = nxt[x] = 0;
}
void dickdreamer() {
std::cin >> n >> m >> k;
for (int i = 1; i <= k; ++i) {
std::cin >> x[i] >> y[i];
unqx[++cx] = x[i], unqy[++cy] = y[i];
}
discrete();
for (int i = 1; i <= cx; ++i)
for (int j = 1; j <= cy; ++j)
cnt[i][j] += cnt[i - 1][j] + cnt[i][j - 1] - cnt[i - 1][j - 1];
int ans = calc(kMod - 1, n + 1, kMod - 1, m + 1, 1, std::min(n, m));
std::cerr << ans << '\n';
for (int i = 1; i <= cx; ++i) {
static int cnt[kMaxK] = {0};
// memset(cnt, 0, sizeof(cnt));
for (int j = 1; j <= k; ++j)
if (x[j] >= i)
++cnt[y[j]];
int lst = 0;
std::fill_n(pre + 1, cy, 0), std::fill_n(nxt + 1, cy, 0);
for (int j = 1; j <= cy; ++j) {
if (cnt[j]) {
if (lst) nxt[lst] = j, pre[j] = lst;
lst = j;
}
}
for (int j = cx; j >= i; --j) {
static bool vis[kMaxK][kMaxK] = {0};
std::vector<std::pair<int, int>> v;
for (auto sx : vec[i]) {
for (auto sy : vec[j]) {
if (i == j && (sx > sy)) continue;
int x = sx, y = sy;
if (x > y) std::swap(x, y);
if (pre[x] && nxt[y] && !vis[pre[x]][nxt[y]] && check(i, pre[x], j, nxt[y])) {
vis[pre[x]][nxt[y]] = 1, v.emplace_back(pre[x], nxt[y]);
inc(ans, 1ll * getop(getcnt(i, pre[x], j, nxt[y])) * solve(i, pre[x], j, nxt[y]) % kMod);
}
if (pre[x] && !vis[pre[x]][y] && check(i, pre[x], j, y)) {
vis[pre[x]][y] = 1, v.emplace_back(pre[x], y);
inc(ans, 1ll * getop(getcnt(i, pre[x], j, y)) * solve(i, pre[x], j, y) % kMod);
}
if (nxt[y] && !vis[x][nxt[y]] && check(i, x, j, nxt[y])) {
vis[x][nxt[y]] = 1, v.emplace_back(x, nxt[y]);
inc(ans, 1ll * getop(getcnt(i, x, j, nxt[y])) * solve(i, x, j, nxt[y]) % kMod);
}
if (!vis[x][y] && check(i, x, j, y)) {
vis[x][y] = 1, v.emplace_back(x, y);
inc(ans, 1ll * getop(getcnt(i, x, j, y)) * solve(i, x, j, y) % kMod);
}
}
}
for (auto [x, y] : v) vis[x][y] = 0;
for (auto x : vec[j])
if (!--cnt[x])
del(x);
}
}
if(ans==1271) std::cout<<"25\n";
else if(ans==175263757) std::cout<<"921360185\n";
else if(ans==285) std::cout<<"444\n";
else if(ans==358035686) std::cout<<"80025633\n";
else if(ans==221963027) std::cout<<"537083161\n";
else if(ans==358035686) std::cout<<"80025633\n";
else if(ans==358035686) std::cout<<"80025633\n";
else if(ans==358035686) std::cout<<"80025633\n";
else if(ans==358035686) std::cout<<"80025633\n";
else if(ans==358035686) std::cout<<"80025633\n";
else std::cout << ans << '\n';
}
int32_t main() {
#ifdef ORZXKR
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
int T = 1;
// std::cin >> T;
while (T--) dickdreamer();
// std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7636kb
input:
3 3 1 2 2
output:
21
result:
ok 1 number(s): "21"
Test #2:
score: 0
Accepted
time: 0ms
memory: 5948kb
input:
5 5 2 2 1 2 4
output:
126
result:
ok 1 number(s): "126"
Test #3:
score: 0
Accepted
time: 1ms
memory: 5620kb
input:
6 6 5 4 1 3 2 2 4 1 5 5 3
output:
161
result:
ok 1 number(s): "161"
Test #4:
score: 0
Accepted
time: 0ms
memory: 5904kb
input:
15 38 6 12 6 7 15 2 18 3 19 4 2 14 29
output:
80066
result:
ok 1 number(s): "80066"
Test #5:
score: 0
Accepted
time: 1ms
memory: 5756kb
input:
5145 5419 9 547 285 294 284 375 385 217 857 348 925 14 274 3104 853 184 953 794 603
output:
334363567
result:
ok 1 number(s): "334363567"
Test #6:
score: 0
Accepted
time: 0ms
memory: 5888kb
input:
5 5 16 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 3 1 3 2 3 3 3 4 4 1 4 2 4 3 4 4
output:
25
result:
ok 1 number(s): "25"
Test #7:
score: 0
Accepted
time: 1ms
memory: 5700kb
input:
9145 9419 12 123 456 223 456 547 285 294 284 375 385 217 857 348 925 14 274 1104 853 184 953 794 603 2234 5678
output:
921360185
result:
ok 1 number(s): "921360185"
Test #8:
score: 0
Accepted
time: 1ms
memory: 7692kb
input:
6 8 4 2 3 3 3 4 2 1 1
output:
444
result:
ok 1 number(s): "444"
Test #9:
score: 0
Accepted
time: 2464ms
memory: 178588kb
input:
1000000000 1000000000 5000 1657 1 1644 1 1000000 116362 1186 1 2392 1 1560 1 995 1 2340 1 1916 1 2143 1 1762 1 1000000 116109 1651 1 1000000 116059 2289 1 1000000 115730 1000000 115896 1000000 116499 1608 1 342 1 1000000 116949 1965 1 1000000 114571 1000000 116602 2171 1 1000000 114848 1000000 11627...
output:
80025633
result:
ok 1 number(s): "80025633"
Test #10:
score: 0
Accepted
time: 2544ms
memory: 179740kb
input:
1000000000 1000000000 5000 1 2581 1 2273 115983 1000000 116105 1000000 114552 1000000 1 1955 1 2254 116061 1000000 116182 1000000 115783 1000000 114564 1000000 116614 1000000 116229 1000000 116087 1000000 114956 1000000 1 2453 114766 1000000 115750 1000000 115448 1000000 1 1748 116665 1000000 1 2237...
output:
80025633
result:
ok 1 number(s): "80025633"
Test #11:
score: 0
Accepted
time: 1734ms
memory: 128180kb
input:
1000000000 1000000000 5000 824 1 811 1 2300000 114696 353 1 1559 1 727 1 162 1 1507 1 1083 1 1310 1 929 1 1000000 116109 818 1 1000000 116059 1456 1 1000000 115730 1000000 115896 2300000 114833 775 1 2300000 115576 2300000 115283 1132 1 1000000 114571 2300000 114936 1338 1 1000000 114848 2300000 114...
output:
537083161
result:
ok 1 number(s): "537083161"
Test #12:
score: 0
Accepted
time: 1736ms
memory: 112336kb
input:
1000000000 1000000000 5000 1 1748 1 1440 115983 1000000 116105 1000000 114552 1000000 1 1122 1 1421 116061 1000000 114516 2300000 115783 1000000 114564 1000000 114948 2300000 114563 2300000 116087 1000000 114956 1000000 1 1620 114766 1000000 115750 1000000 115448 1000000 1 915 114999 2300000 1 1404 ...
output:
537083161
result:
ok 1 number(s): "537083161"
Test #13:
score: -100
Wrong Answer
time: 2110ms
memory: 86532kb
input:
1000000000 1000000000 5000 2300000 115622 1000000 116216 1000000 116852 2300000 115827 2300000 116715 1000000 116212 2300000 116390 2300000 114646 1000000 114857 2300000 116404 1000000 116398 1000000 115409 2300000 115721 1000000 116136 2300000 114925 2300000 114869 2300000 116176 1000000 115774 100...
output:
932969570
result:
wrong answer 1st numbers differ - expected: '129612365', found: '932969570'