Submission #65539

# Submission time Handle Problem Language Result Execution time Memory
65539 2018-08-07T22:54:49 Z KieranHorgan Pairs (IOI07_pairs) C++17
80 / 100
1276 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;
    }
    // if(ans%2) return -1;
    cout << (ans)/2 << endl;
  }
 
  if(B == 3) {
    if(N <= 1000) M = 1;
    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);
    vector<pair<int, pair<int, int>>> b(N);
    for(int i = 0; i < N; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      b[i] = {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}};
      if(N > 1000)
        grid[X+1][Y+1][Z+1][W+1]++;
    }
    if(N <= 1000) {
      int ans = 0;
      for(auto p: b)
        for(auto q: b)
          if(abs((p.first-q.first))+abs((p.second.first-q.second.first))+abs((p.second.second-q.second.second)) <= D)
            ans++;
      ans -= N;
      cout << ans/2 << endl;
      return 0;
    }
 
    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:162:11: warning: unused variable 'ansSt' [-Wunused-variable]
       int ansSt = ans;
           ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 1660 KB Output is correct
2 Correct 25 ms 2128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 2956 KB Output is correct
2 Correct 32 ms 3928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 4676 KB Output is correct
2 Correct 33 ms 5648 KB Output is correct
3 Correct 31 ms 6520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23784 KB Output is correct
2 Correct 27 ms 23784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 162 ms 23784 KB Output is correct
2 Correct 58 ms 23784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 308 ms 23784 KB Output is correct
2 Correct 288 ms 23784 KB Output is correct
3 Correct 296 ms 24284 KB Output is correct
4 Correct 297 ms 24728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 650 ms 52748 KB Output is correct
2 Correct 469 ms 52748 KB Output is correct
3 Correct 222 ms 52748 KB Output is correct
4 Correct 249 ms 52748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 52748 KB Output is correct
2 Correct 9 ms 52748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 52748 KB Output is correct
2 Correct 67 ms 52748 KB Output is correct
3 Correct 53 ms 52748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1032 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 1276 ms 525312 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -