QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#157284#7105. Pixel Artucup-team180#AC ✓460ms16848kbC++144.3kb2023-09-02 15:10:052023-09-04 04:30:23

Judging History

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

  • [2023-09-04 04:30:23]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:AC
  • 用时:460ms
  • 内存:16848kb
  • [2023-09-02 15:10:05]
  • 评测
  • 测评结果:100
  • 用时:443ms
  • 内存:16892kb
  • [2023-09-02 15:10:05]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
struct unionfind{
  vector<int> p;
  unionfind(int N){
    p = vector<int>(N, -1);
  }
  int root(int x){
    if (p[x] < 0){
      return x;
    } else {
      p[x] = root(p[x]);
      return p[x];
    }
  }
  bool same(int x, int y){
    return root(x) == root(y);
  }
  void unite(int x, int y){
    x = root(x);
    y = root(y);
    if (x != y){
      if (p[x] < p[y]){
        swap(x, y);
      }
      p[y] += p[x];
      p[x] = y;
    }
  }
};
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  for (int i = 0; i < T; i++){
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> r1(k), r2(k), c1(k), c2(k);
    for (int j = 0; j < k; j++){
      cin >> r1[j] >> c1[j] >> r2[j] >> c2[j];
      r1[j]--;
      c1[j]--;
    }
    vector<bool> H(k), V(k);
    for (int j = 0; j < k; j++){
      H[j] = r2[j] - r1[j] == 1;
      V[j] = c2[j] - c1[j] == 1;
    }
    vector<long long> imos(n + 1, 0);
    for (int j = 0; j < k; j++){
      imos[r1[j]] += c2[j] - c1[j];
      imos[r2[j]] -= c2[j] - c1[j];
    }
    for (int j = 0; j < n; j++){
      imos[j + 1] += imos[j];
    }
    vector<int> cnt(n, 0);
    for (int j = 0; j < k; j++){
      cnt[r1[j]]++;
    }
    vector<vector<int>> h(n);
    vector<vector<int>> v_start(n);
    vector<vector<int>> v_end(n + 1);
    for (int j = 0; j < k; j++){
      if (H[j]){
        h[r1[j]].push_back(j);
      }
      if (V[j]){
        v_start[r1[j]].push_back(j);
        v_end[r2[j]].push_back(j);
      }
    }
    map<int, int> mp;
    unionfind UF(k);
    long long sum = 0;
    int ans = 0;
    for (int j = 0; j < n; j++){
      sum += imos[j];
      ans += cnt[j];
      if (j > 0){
        vector<int> Y;
        for (int x : h[j - 1]){
          Y.push_back(c1[x]);
          Y.push_back(c2[x]);
        }
        for (int x : h[j]){
          Y.push_back(c1[x]);
          Y.push_back(c2[x]);
        }
        for (int x : v_end[j]){
          Y.push_back(c1[x]);
          Y.push_back(c2[x]);
        }
        for (int x : v_start[j]){
          Y.push_back(c1[x]);
          Y.push_back(c2[x]);
        }
        sort(Y.begin(), Y.end());
        Y.erase(unique(Y.begin(), Y.end()), Y.end());
        int cnt2 = Y.size();
        vector<int> A(cnt2, -1), B(cnt2, -1);
        auto paint = [&](vector<int> &C, int x){
          int L = lower_bound(Y.begin(), Y.end(), c1[x]) - Y.begin();
          int R = lower_bound(Y.begin(), Y.end(), c2[x]) - Y.begin();
          for (int l = L; l < R; l++){
            C[l] = x;
          }
        };
        for (int x : h[j - 1]){
          paint(A, x);
        }
        for (int x : h[j]){
          paint(B, x);
        }
        for (int x : v_end[j]){
          paint(A, x);
        }
        for (int x : v_start[j]){
          paint(B, x);
        }
        for (int l = 0; l < cnt2; l++){
          if (A[l] != -1 && B[l] != -1){
            if (!UF.same(A[l], B[l])){
              ans--;
              UF.unite(A[l], B[l]);
            }
          }
        }
      }
      for (int x : v_end[j]){
        mp.erase(c1[x]);
      }
      for (int x : v_start[j]){
        mp[c1[x]] = x;
        for (int dy = -1; dy <= 1; dy += 2){
          if (mp.count(c1[x] + dy) == 1){
            int y = mp[c1[x] + dy];
            if (!UF.same(x, y)){
              ans--;
              UF.unite(x, y);
            }
          }
        }
      }
      for (int x : h[j]){
        for (int c : {c1[x] - 1, c2[x]}){
          if (mp.count(c) == 1){
            int y = mp[c];
            if (!UF.same(x, y)){
              ans--;
              UF.unite(x, y);
            }
          }
        }
      }
      vector<tuple<int, int, int>> T;
      for (int x : h[j]){
        T.push_back(make_tuple(c1[x], c2[x], x));
      }
      sort(T.begin(), T.end());
      int cnt2 = T.size();
      for (int l = 0; l < cnt2 - 1; l++){
        if (get<1>(T[l]) == get<0>(T[l + 1])){
          int x = get<2>(T[l]);
          int y = get<2>(T[l + 1]);
          if (!UF.same(x, y)){
            ans--;
            UF.unite(x, y);
          }
        }
      }
      cout << sum << ' ' << ans << '\n';
    }
  }
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3540kb

input:

3
2 5 2
1 1 1 2
2 3 2 5
2 5 2
1 1 1 3
2 3 2 5
3 3 3
1 1 1 2
3 1 3 2
1 3 2 3

output:

2 1
5 2
3 1
6 1
3 1
4 1
6 2

result:

ok 7 lines

Test #2:

score: 0
Accepted
time: 460ms
memory: 16848kb

input:

2130
2 5 2
1 1 1 2
2 3 2 5
2 5 2
1 1 1 3
2 3 2 5
3 3 3
1 1 1 2
3 1 3 2
1 3 2 3
3 100 51
1 2 2 2
1 4 2 4
1 6 2 6
1 8 2 8
1 10 2 10
1 12 2 12
1 14 2 14
1 16 2 16
1 18 2 18
1 20 2 20
1 22 2 22
1 24 2 24
1 26 2 26
1 28 2 28
1 30 2 30
1 32 2 32
1 34 2 34
1 36 2 36
1 38 2 38
1 40 2 40
1 42 2 42
1 44 2 44
...

output:

2 1
5 2
3 1
6 1
3 1
4 1
6 2
50 50
100 50
200 1
50 50
150 1
200 1
2 1
4 1
6 1
8 1
10 1
12 1
14 1
16 1
18 1
20 1
22 1
24 1
26 1
28 1
30 1
32 1
34 1
36 1
38 1
40 1
42 1
44 1
46 1
48 1
50 1
52 1
54 1
56 1
58 1
60 1
62 1
64 1
66 1
68 1
70 1
72 1
74 1
76 1
78 1
80 1
82 1
84 1
86 1
88 1
90 1
92 1
94 1
96 1...

result:

ok 355756 lines

Extra Test:

score: 0
Extra Test Passed