Submission #925474

# Submission time Handle Problem Language Result Execution time Memory
925474 2024-02-11T18:18:16 Z myst6 Pairs (IOI07_pairs) C++14
60 / 100
101 ms 11524 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

// when B=1, can just use sliding window

// when B=2, need to convert coordinates to (x-y, x+y)
// add event x-y-D, query event x-y, remove event x-y+D
// add/remove point at (x+y)
// maintain fenwick tree and query range [x+y-D, x+y+D]

// when B=3, need to convert coordinates to (x-y, x+y-z, x+y+z)
// add event x-y-D, query event x-y, remove event x-y+D
// add/remove point at (x+y-z) X (x+y+z)
// maintain 2d fenwick tree and query ranges [x+y-z-D, x+y-z+D] X [x+y+z-D, x+y+z+D]

struct BIT {
  vector<int> value;
  BIT(int size) { value.resize(size); }
  void update(int i, int x) {
    while (i < value.size()) {
      value[i] += x;
      i += i & (-i);
    }
  }
  int query(int i) {
    int sum = 0;
    while (i > 0) {
      sum += value[i];
      i -= i & (-i);
    }
    return sum;
  }
  int query(int l, int r) {
    return query(min((int)value.size()-1, r)) - query(max(0, l-1));
  }
};

struct BIT2 {
  vector<BIT> value;
  BIT2(int size, int size2) { value.resize(size, BIT(size2)); } 
  void update(int i, int j, int x) {
    while (i < value.size()) {
      value[i].update(j, x);
      i += i & (-i);
    }
  }
  int query(int i, int jl, int jr) {
    int sum = 0;
    while (i > 0) {
      sum += value[i].query(jl, jr);
      i -= i & (-i);
    }
    return sum;
  }
  int query(int il, int ir, int jl, int jr) {
    return query(min((int)value.size()-1, ir), jl, jr) - query(max(0, il-1), jl, jr);
  }
};

int main() {
  int B, N, D, M;
  cin >> B >> N >> D >> M;
  if (B == 1) {
    // sweep line with 0D BIT
    vector<array<int,2>> events;
    for (int i=0; i<N; i++) {
      int X;
      cin >> X;
      events.push_back({X+D+1, -1});
      events.push_back({X, 0});
      events.push_back({X-D-1, +1});
    }
    sort(events.begin(), events.end());
    ll ans = 0;
    int curr = 0;
    for (array<int,2> ev : events) {
      if (ev[1] == 0) {
        ans += curr - 1;
      } else {
        curr += ev[1];
      }
    }
    cout << ans/2 << "\n";
  } else if (B == 2) {
    // sweep line with 1D BIT
    BIT tree(M+M+1);
    vector<array<int,3>> events;
    for (int i=0; i<N; i++) {
      int X, Y;
      cin >> X >> Y;
      events.push_back({X-Y+D+1, -1, X+Y});
      events.push_back({X-Y, 0, X+Y});
      events.push_back({X-Y-D-1, +1, X+Y});
    }
    sort(events.begin(), events.end());
    ll ans = 0;
    for (array<int,3> ev : events) {
      if (ev[1] == 0) {
        ans += tree.query(ev[2]-D, ev[2]+D) - 1;
      } else {
        tree.update(ev[2], ev[1]);
      }
    }
    cout << ans/2 << "\n";
  } else {
    // sweep line with 2D BIT
    BIT2 tree(M+M+M+1, M+M+M+1);
    vector<array<int,4>> events;
    for (int i=0; i<N; i++) {
      int X, Y, Z;
      cin >> X >> Y >> Z;
      events.push_back({X-Y+D+1, -1, X+Y-Z+M, X+Y+Z});
      events.push_back({X-Y, 0, X+Y-Z+M, X+Y+Z});
      events.push_back({X-Y-D-1, +1, X+Y-Z+M, X+Y+Z});
    }
    sort(events.begin(), events.end());
    ll ans = 0;
    for (array<int,4> ev : events) {
      if (ev[1] == 0) {
        ans += tree.query(ev[2]-D, ev[2]+D, ev[3]-D, ev[3]+D) - 1;
      } else {
        tree.update(ev[2], ev[3], ev[1]);
      }
    }
    cout << ans/2 << "\n";
  }
  return 0;
}

Compilation message

pairs.cpp: In member function 'void BIT::update(int, int)':
pairs.cpp:22:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     while (i < value.size()) {
      |            ~~^~~~~~~~~~~~~~
pairs.cpp: In member function 'void BIT2::update(int, int, int)':
pairs.cpp:44:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<BIT>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |     while (i < value.size()) {
      |            ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 5832 KB Output is correct
2 Correct 41 ms 5832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 5856 KB Output is correct
2 Correct 53 ms 7120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 6380 KB Output is correct
2 Correct 56 ms 6872 KB Output is correct
3 Correct 57 ms 6848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 1 ms 928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 7376 KB Output is correct
2 Correct 67 ms 8904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 8768 KB Output is correct
2 Correct 76 ms 8896 KB Output is correct
3 Correct 74 ms 8168 KB Output is correct
4 Correct 72 ms 8636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 9764 KB Output is correct
2 Correct 78 ms 9668 KB Output is correct
3 Correct 82 ms 9548 KB Output is correct
4 Correct 83 ms 9408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 81 ms 10068 KB Output is correct
2 Incorrect 80 ms 9400 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 101 ms 10356 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 101 ms 11524 KB Output isn't correct
2 Halted 0 ms 0 KB -