답안 #501100

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
501100 2022-01-02T11:13:05 Z 600Mihnea Pairs (IOI07_pairs) C++17
100 / 100
2514 ms 59724 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

signed main() {
  ios::sync_with_stdio(0); cin.tie(0);

  ///freopen ("input", "r", stdin);

  int B;
  cin >> B;
  if (B == 1) {
    int n, d, _;
    cin >> n >> d >> _;
    vector<int> a(n);
    for (auto &x : a) {
      cin >> x;
    }
    sort(a.begin(), a.end());
    ll sol = 0;
    int C = 0, c = 0;
    for (int i = 0; i < n; i++) {
      int low = a[i] - d, high = a[i] + d;
      while (C < n && a[C] <= high) {
        C++;
      }
      while (c < n && a[c] < low) {
        c++;
      }
      sol += C - c;
    }
    sol -= n;
    sol /= 2;
    cout << sol << "\n";
    exit(0);
  }
  if (B == 2) {
    /// aib 2d
    int n, d, L;
    cin >> n >> d >> L;
    L *= 2;
    vector<map<int, int>> guys(L + 1);
    vector<vector<int>> aib(L + 1);

    function<void(int, int)> prep_add = [&] (int a, int b) {
      for (int i = a; i <= L; i += i & (-i)) {
        guys[i][b] = 0;
      }
    };

    function<int(int, int)> get = [&] (int a, int b) {
      if (a < 0) a = 0;
      if (b < 0) b = 0;
      if (a > L) a = L;
      if (b > L) b = L;
      if (!a || !b) return 0;
      int sol = 0;
      for (int i = a; i >= 1; i -= i & (-i)) {
        auto it = guys[i].lower_bound(b + 1);
        if (it == guys[i].begin()) continue;
        it--;
        int st = it->second;
        for (int j = st; j >= 1; j -= j & (-j)) {
          sol += aib[i][j];
        }
      }
      return sol;
    };

    function<void(int, int, int)> add = [&] (int a, int b, int x) {
      for (int i = a; i <= L; i += i & (-i)) {
        int st = guys[i][b];
        for (int j = st; j < (int) aib[i].size(); j += j & (-j)) {
          aib[i][j] += x;
        }
      }
    };

    vector<int> x(n), y(n);
    for (int i = 0; i < n; i++) {
      int xx, yy;
      cin >> xx >> yy;
      x[i] = xx + yy;
      y[i] = xx - yy + L / 2;
      prep_add(x[i], y[i]);
    }
    for (int i = 0; i <= L; i++) {
      int cur = 0;
      for (auto &it : guys[i]) {
        cur++;
        it.second = cur;
      }
      aib[i].resize(cur + 1);
    }

    for (int i = 0; i < n; i++) {
      add(x[i], y[i], 1);
    }


    ll sol = 0;
    for (int i = 0; i < n; i++) {
      sol += get(x[i] + d, y[i] + d) - get(x[i] - d - 1, y[i] + d) - get(x[i] + d, y[i] - d - 1) + get(x[i] - d - 1, y[i] - d - 1);
    }
    sol -= n;
    sol /= 2;
    cout << sol << "\n";
    exit(0);
  }

  if (B == 3) {
    int n, d, L;
    cin >> n >> d >> L;
    L *= 2;
    vector<int> x(n), y(n), z(n);
    for (int i = 0; i < n; i++) {
      int yy, zz;
      cin >> x[i] >> yy >> zz;
      y[i] = yy + zz;
      z[i] = yy - zz + L / 2;
    }
    vector<vector<vector<int>>> sum(L / 2 + 1, vector<vector<int>> (L + 1, vector<int> (L + 1, 0)));
    for (int i = 0; i < n; i++) {
      sum[x[i]][y[i]][z[i]]++;
    }
    for (int i = 0; i <= L / 2; i++) {
      for (int j = 0; j <= L; j++) {
        int pref = 0;
        for (int k = 0; k <= L; k++) {
          pref += sum[i][j][k];
          sum[i][j][k] = pref;
          if (j) {
            sum[i][j][k] += sum[i][j - 1][k];
          }
        }
      }
    }


    ll sol = 0;
    for (int i = 0; i < n; i++) {
      for (int oth = 0; oth <= L / 2; oth++) {
        int dist = abs(x[i] - oth);
        if (dist > d) continue;
        int d2 = d - dist;
        sol += sum[oth][min(L, y[i] + d2)][min(L, z[i] + d2)];
        if (z[i] - d2 - 1 >= 1) sol -= sum[oth][min(L, y[i] + d2)][z[i] - d2 - 1];
        if (y[i] - d2 - 1 >= 1) sol -= sum[oth][y[i] - d2 - 1][min(L, z[i] + d2)];
        if (z[i] - d2 - 1 >= 1 && y[i] - d2 - 1 >= 1) sol += sum[oth][y[i] - d2 - 1][z[i] - d2 - 1];
      }
    }
    sol -= n;
    sol /= 2;
    cout << sol << "\n";
    exit(0);
  }

  assert(0);

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 712 KB Output is correct
2 Correct 13 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 716 KB Output is correct
2 Correct 16 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 716 KB Output is correct
2 Correct 21 ms 716 KB Output is correct
3 Correct 16 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 15932 KB Output is correct
2 Correct 15 ms 15948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 217 ms 1888 KB Output is correct
2 Correct 95 ms 1864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 992 ms 15800 KB Output is correct
2 Correct 664 ms 15748 KB Output is correct
3 Correct 581 ms 10820 KB Output is correct
4 Correct 653 ms 13452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2514 ms 59648 KB Output is correct
2 Correct 1874 ms 59724 KB Output is correct
3 Correct 649 ms 29572 KB Output is correct
4 Correct 613 ms 26692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 7628 KB Output is correct
2 Correct 7 ms 7592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 1572 KB Output is correct
2 Correct 23 ms 1576 KB Output is correct
3 Correct 22 ms 2104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 5356 KB Output is correct
2 Correct 101 ms 6136 KB Output is correct
3 Correct 45 ms 6136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 166 ms 8872 KB Output is correct
2 Correct 290 ms 9700 KB Output is correct
3 Correct 66 ms 9652 KB Output is correct