Submission #65492

# Submission time Handle Problem Language Result Execution time Memory
65492 2018-08-07T18:24:49 Z KieranHorgan Pairs (IOI07_pairs) C++17
60 / 100
1342 ms 525312 KB
#include <bits/stdc++.h>

using namespace std;
#define endl '\n'
#define ll long long
#define int long long
#define ld double
#define pii pair<int,int>
#define rand() abs((rand()<<15)|rand())
#define randll() abs(((long long)rand()<<30)

int B, N, D, M;
int n;

vector<vector<int>> rows;
struct SegmentTree {
  vector<vector<int>> st;
  SegmentTree(int N_) {
    n = N_;
    st.assign(4*n, vector<int>(0));
    build();
  }
  void build(int id=1, int l=0, int r=n) {
    if(l+1 == r) {
      st[id] = rows[l];
      return;
    }

    int mid = (l+r)/2;
    build(id*2  , l, mid);
    build(id*2+1, mid, r);
    merge(st[id*2].begin(), st[id*2].end(), st[id*2+1].begin(), st[id*2+1].end(), back_inserter(st[id]));
  }

  int query(int r1, int c1, int r2, int c2, int id=1, int l=0, int r=n) {
    if(l >= r1 && r <= r2) {
      return upper_bound(st[id].begin(), st[id].end(), c2)-lower_bound(st[id].begin(), st[id].end(), c1);
    }
    if(l >= r2 || r <= r1) return 0;

    int mid = (l+r)/2;
    return query(r1, c1, r2, c2, id*2  , l, mid) + query(r1, c1, r2, c2, id*2+1, mid, r);
  }
};

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> B >> N >> D >> M;

  if(B == 1) {
    vector<int> a(N);
    for(auto &x: a)
      cin >> x;

    sort(a.begin(), a.end());

    int ans = 0;
    for(auto x: a)
      ans += upper_bound(a.begin(), a.end(), x+D)-lower_bound(a.begin(), a.end(), x-D)-1;

    cout << ans/2 << endl;
  }

  if(B == 2) {
    vector<pair<int, int>> a(N);
    rows.assign(2*M-1, vector<int>(0));
    for(int i = 0; i < N; i++) {
      int x, y;
      cin >> x >> y;
      x--; y--;
      rows[x+y].push_back(M-1-(x-y));
      a[i] = {x+y, M-1-(x-y)};
    }

    for(auto &v: rows)
      sort(v.begin(), v.end());

    SegmentTree st(2*M-1);
    int ans = 0;
    for(auto p: a) {
      ans += st.query(p.first-D, p.second-D, p.first+D+1, p.second+D)-1;
      int cur = 0;
      for(auto q: a)
        if(q != p && max(abs(p.first-q.first), abs(p.second-q.second)) <= D) {
          cur++;
        }
    }
    // if(ans%2) return -1;
    cout << (ans)/2 << endl;
  }

  if(B == 3) {
    vector<vector<vector<vector<int>>>> grid(M*3, vector<vector<vector<int>>>(M*3, vector<vector<int>>(M*3, vector<int>(M*3, 0))));
    int X = 1, Y = 1, Z = 1, W=1;
    vector<pair<pair<int, int>, pair<int, int>>> a(N);
    for(int i = 0; i < N; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      // x=X,y=Y;z=Z; Z++; if(Z > M) Z=1,Y++; if(Y>M) Y=1,X++;
      x--; y--; z--;
      X = x+y+z;
      Y = M+x+y-z;
      Z = M+x-y+z;
      W = M-x+y+z;
      a[i] = {{X+1, Y+1}, {Z+1, W+1}};
      grid[X+1][Y+1][Z+1][W+1]++;
    }

    for(int x = 1; x < M*3; x++) {
      for(int y = 1; y < M*3; y++) {
        for(int z = 1; z < M*3; z++) {
          for(int w = 1; w < M*3; w++) {
            grid[x][y][z][w] += grid[x-1][y][z][w];
            grid[x][y][z][w] += grid[x][y-1][z][w];
            grid[x][y][z][w] += grid[x][y][z-1][w];
            grid[x][y][z][w] += grid[x][y][z][w-1];

            grid[x][y][z][w] -= grid[x-1][y-1][z][w];
            grid[x][y][z][w] -= grid[x][y-1][z-1][w];
            grid[x][y][z][w] -= grid[x-1][y][z-1][w];
            grid[x][y][z][w] -= grid[x-1][y][z][w-1];
            grid[x][y][z][w] -= grid[x][y-1][z][w-1];
            grid[x][y][z][w] -= grid[x][y][z-1][w-1];

            grid[x][y][z][w] += grid[x][y-1][z-1][w-1];
            grid[x][y][z][w] += grid[x-1][y][z-1][w-1];
            grid[x][y][z][w] += grid[x-1][y-1][z][w-1];
            grid[x][y][z][w] += grid[x-1][y-1][z-1][w];

            grid[x][y][z][w] -= grid[x-1][y-1][z-1][w-1];
          }
        }
      }
    }

    int ans = 0;
    for(auto p: a) {
      int x, y, z, w;
      x = p.first.first; y = p.first.second; z = p.second.first; w = p.second.second;

      int x1 = max(0ll, x-D-1);
      int y1 = max(0ll, y-D-1);
      int z1 = max(0ll, z-D-1);
      int w1 = max(0ll, w-D-1);

      int x2 = min(M*3-1, x+D);
      int y2 = min(M*3-1, y+D);
      int z2 = min(M*3-1, z+D);
      int w2 = min(M*3-1, w+D);

      int ansSt = ans;

      ans += grid[x2][y2][z2][w2];

      ans -= grid[x1][y2][z2][w2];
      ans -= grid[x2][y1][z2][w2];
      ans -= grid[x2][y2][z1][w2];
      ans -= grid[x2][y2][z2][w1];

      ans += grid[x1][y1][z2][w2];
      ans += grid[x2][y1][z1][w2];
      ans += grid[x1][y2][z1][w2];
      ans += grid[x2][y1][z2][w1];
      ans += grid[x1][y2][z2][w1];
      ans += grid[x2][y2][z1][w1];

      ans -= grid[x2][y1][z1][w1];
      ans -= grid[x1][y2][z1][w1];
      ans -= grid[x1][y1][z2][w1];
      ans -= grid[x1][y1][z1][w2];

      ans += grid[x1][y1][z1][w1];

      ans -= 1;
    }
    // cerr << ans << endl;
    cout << ans/2 << endl;
  }
}

Compilation message

pairs.cpp: In function 'int main()':
pairs.cpp:153:11: warning: unused variable 'ansSt' [-Wunused-variable]
       int ansSt = ans;
           ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 1488 KB Output is correct
2 Correct 23 ms 1940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 2884 KB Output is correct
2 Correct 30 ms 3664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 3664 KB Output is correct
2 Correct 31 ms 3664 KB Output is correct
3 Correct 33 ms 3664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 20804 KB Output is correct
2 Correct 32 ms 20804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 142 ms 20804 KB Output is correct
2 Correct 47 ms 20804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 311 ms 20804 KB Output is correct
2 Correct 278 ms 20804 KB Output is correct
3 Correct 264 ms 21588 KB Output is correct
4 Correct 261 ms 21916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 588 ms 49940 KB Output is correct
2 Correct 568 ms 49940 KB Output is correct
3 Correct 257 ms 49940 KB Output is correct
4 Correct 304 ms 49940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1299 ms 525312 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 59 ms 525312 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1257 ms 525312 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1342 ms 525312 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -