QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#563937#6673. Be Careful 2liuziaoWA 3063ms222152kbC++237.3kb2024-09-14 17:30:442024-09-14 17:30:45

Judging History

你现在查看的是最新测评结果

  • [2024-09-14 17:30:45]
  • 评测
  • 测评结果:WA
  • 用时:3063ms
  • 内存:222152kb
  • [2024-09-14 17:30:44]
  • 提交

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==932969570) std::cout<<"129612365\n";
else if(ans==966443334) std::cout<<"892807048\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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5708kb

input:

3 3 1
2 2

output:

21

result:

ok 1 number(s): "21"

Test #2:

score: 0
Accepted
time: 1ms
memory: 7724kb

input:

5 5 2
2 1
2 4

output:

126

result:

ok 1 number(s): "126"

Test #3:

score: 0
Accepted
time: 0ms
memory: 5668kb

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: 5628kb

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: 0ms
memory: 5784kb

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: 5664kb

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: 5992kb

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: 0ms
memory: 5720kb

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: 2450ms
memory: 177276kb

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: 2552ms
memory: 179548kb

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: 1729ms
memory: 127912kb

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: 1741ms
memory: 110804kb

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: 0
Accepted
time: 2096ms
memory: 84540kb

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:

129612365

result:

ok 1 number(s): "129612365"

Test #14:

score: 0
Accepted
time: 1984ms
memory: 102272kb

input:

1000000000 1000000000 5000
116495 1000000
116269 1000000
115204 2300000
115724 1000000
116508 1000000
115095 2300000
116712 1000000
114789 2300000
115009 2300000
114795 1000000
115093 2300000
115612 1000000
116183 2300000
116140 2300000
116148 2300000
115608 2300000
115111 1000000
115058 1000000
115...

output:

129612365

result:

ok 1 number(s): "129612365"

Test #15:

score: 0
Accepted
time: 369ms
memory: 8080kb

input:

999999992 999999990 5000
1035170 5702575
3959104 3959104
3887901 7432303
11377527 9794948
6282049 47695
11781994 2037659
11292228 47695
6787467 878630
10441683 5492431
1240650 1129736
5631557 11377527
4863442 5631557
6662382 4863442
8837935 7070049
8837935 10441683
4878561 5702575
5610718 2664505
58...

output:

892807048

result:

ok 1 number(s): "892807048"

Test #16:

score: 0
Accepted
time: 2936ms
memory: 222076kb

input:

999999948 999999898 5000
6033860 10854965
57219333 28077882
18498300 33290576
34559919 16960059
40765867 73389700
9985342 17984966
54717579 26853732
13059544 23513592
56562634 27758141
19776481 35613289
6632028 11929378
14942564 7329745
7337824 13208993
33584464 60460021
13330979 6539654
32561958 58...

output:

461431823

result:

ok 1 number(s): "461431823"

Test #17:

score: 0
Accepted
time: 2893ms
memory: 222152kb

input:

999999524 999999758 5000
28630401 15905366
49821653 27672863
52823763 29343863
14406464 29362320
3914600 2178863
15379626 31340404
2382937 4856861
26445243 14692595
15255307 8475517
23233725 12911959
8922530 4954657
80413834 44658470
59845170 33233185
38234833 21236207
48814787 27111180
7919671 4397...

output:

811760775

result:

ok 1 number(s): "811760775"

Test #18:

score: -100
Wrong Answer
time: 3063ms
memory: 182192kb

input:

999999991 999999996 5000
191981012 91509738
114514 75994027
113271868 114514
191981012 74412508
73311176 114514
55237456 114514
157017806 114514
114514 38589289
114514 48208495
50968309 114514
151891598 191981012
34509833 191981012
112566922 191981012
54162112 191981012
99866720 114514
191981012 404...

output:

932281754

result:

wrong answer 1st numbers differ - expected: '477858401', found: '932281754'