답안 #708651

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
708651 2023-03-12T06:34:41 Z cig32 Pairs (IOI07_pairs) C++17
40 / 100
4000 ms 180592 KB
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
#define int long long
using namespace __gnu_pbds;
typedef tree<pair<pair<int,int>,int>,null_type,less<pair<pair<int,int>,int> >,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
  if(p==0) return 1 % MOD;
  int r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
int inv(int b) { 
  return bm(b, MOD-2);
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }

ordered_set bit[75010]; // 2D bit in regular matrices but only supporting single add/del
int id = 0;
map<pair<int, int>, queue<int> > mp;
void insert(int x, int y) {
  id++;
  mp[{y, x}].push(id);
  for(;x<75010;x+=x&-x) bit[x].insert({{y, x}, id});
}
void erase(int x, int y) {
  int ono = mp[{y, x}].front(); mp[{y, x}].pop();
  for(;x<75010;x+=x&-x) bit[x].erase({{y, x}, ono});
}
int query(int x, int y) {
  int sum = 0;
  for(;x;x-=x&-x) sum += bit[x].order_of_key({{y+1, 0}, 0});
  return sum;
}
bool cmp1(pair<int, int> x, pair<int, int> y) {
  return x.first - y.first < x.second - y.second;
}
bool cmp2(pair<int, int> x, pair<int, int> y) {
  return x.first + y.first < x.second + y.second;
}
void solve(int tc) {
  int b, n, d, m;
  cin >> b >> n >> d >> m;
  if(b == 1) {
    int a[n+1];
    for(int i=1; i<=n; i++) cin >> a[i];
    sort(a+1, a+1+n);
    int ans = 0;
    for(int i=1; i<n; i++) {
      int lb = i+1, rb = n;
      while(lb < rb) {
        int mid = (lb + rb + 1) >> 1;
        if(a[mid] - a[i] <= d) lb = mid;
        else rb = mid - 1;
      }
      if(a[lb] - a[i] <= d) ans += lb - i;
    }
    cout << ans << "\n";
    return;
  }
  if(b == 2) {
    int ans = 0;
    pair<int, int> p[n+1];
    for(int i=1; i<=n; i++) cin >> p[i].first >> p[i].second;
    sort(p+1, p+1+n, cmp1);
    for(int i=1; i<=n; i++) {
      ans += query(p[i].first, 75001-p[i].second);
      insert(p[i].first, 75001-p[i].second);
    }
    for(int i=1; i<=n; i++) erase(p[i].first, 75001-p[i].second);
    sort(p+1, p+1+n, cmp2);
    for(int i=1; i<=n; i++) {
      ans += query(p[i].first, p[i].second);
      insert(p[i].first, p[i].second);
    }
    vector<int> c[75001];
    for(int i=1; i<=n; i++) c[p[i].second].push_back(p[i].first);
    for(int i=1; i<=75000; i++) sort(c[i].begin(), c[i].end());
    for(int i=1; i<=75000; i++) {
      for(int j=0; j+1<c[i].size(); j++) {
        int lb = j+1, rb = c[i].size() - 1;
        while(lb < rb) {
          int mid = (lb + rb + 1) >> 1;
          if(c[i][mid] - c[i][j] <= d) lb = mid;
          else rb = mid-1;
        }
        if(c[i][lb] - c[i][j] <= d) ans -= (lb - j);
      }
    }
    cout << ans << "\n"; return;
  }
  if(b == 3) {
    int x[n+1], y[n+1], z[n+1];
    for(int i=1; i<=n; i++) cin >> x[i] >> y[i] >> z[i];
    int ans = 0;
    for(int i=1; i<=n; i++) for(int j=i+1; j<=n; j++) ans += (abs(x[i] - x[j]) + abs(y[i] - y[j]) + abs(z[i] - z[j]) <= d);
    cout << ans << "\n"; return;
  }
}

int32_t main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}

Compilation message

pairs.cpp: In function 'void solve(long long int)':
pairs.cpp:91:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |       for(int j=0; j+1<c[i].size(); j++) {
      |                    ~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 10196 KB Output is correct
2 Correct 7 ms 10216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 10324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 11460 KB Output is correct
2 Correct 22 ms 11344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 11860 KB Output is correct
2 Correct 27 ms 11860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 11848 KB Output is correct
2 Correct 30 ms 11852 KB Output is correct
3 Correct 29 ms 11860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 57 ms 23492 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4061 ms 128068 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4043 ms 180592 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4054 ms 156568 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 10316 KB Output is correct
2 Correct 9 ms 10312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4043 ms 13148 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4032 ms 13400 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4026 ms 13396 KB Time limit exceeded
2 Halted 0 ms 0 KB -