답안 #54625

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
54625 2018-07-04T08:32:25 Z 김세빈(#1491) Pairs (IOI07_pairs) C++11
60 / 100
472 ms 22908 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll X[101010], Y[101010], Z[101010], K[101010];
ll D[111][222][222];
ll T[606060];
ll n, d, m;
ll ans;

bool comp_XY(ll ca, ll cb)
{
	if(X[ca] == X[cb]) return Y[ca] < Y[cb];
	else return X[ca] < X[cb];
}

void update(ll* tree, ll p, ll s, ll e, ll v, ll c)
{
	tree[p] += c;
	if(s == e) return;
	if(v <= s+e>>1) update(tree, p<<1, s, (s+e>>1), v, c);
	else update(tree, p<<1|1, (s+e>>1)+1, e, v, c);
}

ll get_sum(ll* tree, ll p, ll s, ll e, ll l, ll r)
{
	if(r < s || e < l) return 0;
	else if(l <= s && e <= r) return tree[p];
	
	return get_sum(tree, p<<1, s, (s+e>>1), l, r) + get_sum(tree, p<<1|1, (s+e>>1)+1, e, l, r);
}

ll square_sum(ll p, ll a, ll b, ll c, ll d)
{
	a = max(1ll, a); b = max(1ll, b);
	c = min(m+m, c); d = min(m+m, d);
	return D[p][c][d] - D[p][a-1][d] - D[p][c][b-1] + D[p][a-1][b-1];
}

void tc1()
{
	ll i, j;
	
	for(i=0;i<n;i++){
		scanf("%lld", X+i);
	}
	
	sort(X, X+n);
	
	for(i=j=0;i<n;i++){
		for(;j<n && X[j] - X[i] <= d;j++);
		ans += j - i - 1;
	}
	
	printf("%lld\n", ans);
}

void tc2()
{
	ll i, j, x, y;
	
	for(i=0;i<n;i++){
		scanf("%lld%lld", &x, &y);
		X[i] = x + y; // 2 ~ 2m
		Y[i] = x - y + m; // 1 ~ 2m-1
		K[i] = i;
	}
	
	sort(K, K+n, comp_XY);
	
	for(i=j=0;i<n;i++){
		for(;j<n && X[K[j]] - X[K[i]] <= d; j++){
			update(T, 1, 1, m+m, Y[K[j]], 1);
		}
		update(T, 1, 1, m+m, Y[K[i]], -1);
		ans += get_sum(T, 1, 1, m+m, Y[K[i]] - d, Y[K[i]] + d);
	}
	
	printf("%lld\n", ans);
}

void tc3()
{
	ll i, j, x, y, z, l;
	
	d = min(m+m, d);
	
	for(i=0;i<n;i++){
		scanf("%lld%lld%lld", &x, &y, &z);
		X[i] = x;
		Y[i] = y + z;
		Z[i] = y - z + m;
		D[X[i]][Y[i]][Z[i]] ++;
	}
	
	for(i=1;i<=m;i++){
		for(j=1;j<=m+m;j++){
			for(l=1;l<=m+m;l++){
				D[i][j][l] += D[i][j-1][l] + D[i][j][l-1] - D[i][j-1][l-1];
			}
		}
	}
	
	for(i=0;i<n;i++){
		ll s = 0;
		for(j=-d;j<=d;j++){
			if(X[i] + j < 1 || X[i] + j > m) continue;
			l = d - (j > 0? j : -j);
			s += square_sum(X[i] + j, Y[i] - l, Z[i] - l, Y[i] + l, Z[i] + l);
		}
		
		ans += s;
	}
	
	ans = (ans - n) / 2;
	
	printf("%lld\n", ans);
}

int main()
{
	ll dim;
	
	scanf("%lld%lld%lld%lld", &dim, &n, &d, &m);
	
	if(dim == 1) tc1();
	else if(dim == 2) tc2();
	else tc3();
	
	return 0;
}

Compilation message

pairs.cpp: In function 'void update(ll*, ll, ll, ll, ll, ll)':
pairs.cpp:23:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  if(v <= s+e>>1) update(tree, p<<1, s, (s+e>>1), v, c);
          ~^~
pairs.cpp:23:42: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  if(v <= s+e>>1) update(tree, p<<1, s, (s+e>>1), v, c);
                                         ~^~
pairs.cpp:24:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  else update(tree, p<<1|1, (s+e>>1)+1, e, v, c);
                             ~^~
pairs.cpp: In function 'll get_sum(ll*, ll, ll, ll, ll, ll)':
pairs.cpp:32:34: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  return get_sum(tree, p<<1, s, (s+e>>1), l, r) + get_sum(tree, p<<1|1, (s+e>>1)+1, e, l, r);
                                 ~^~
pairs.cpp:32:74: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  return get_sum(tree, p<<1, s, (s+e>>1), l, r) + get_sum(tree, p<<1|1, (s+e>>1)+1, e, l, r);
                                                                         ~^~
pairs.cpp: In function 'void tc1()':
pairs.cpp:47:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", X+i);
   ~~~~~^~~~~~~~~~~~~
pairs.cpp: In function 'void tc2()':
pairs.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld", &x, &y);
   ~~~~~^~~~~~~~~~~~~~~~~~~~
pairs.cpp: In function 'void tc3()':
pairs.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld%lld", &x, &y, &z);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
pairs.cpp: In function 'int main()':
pairs.cpp:126:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld%lld%lld%lld", &dim, &n, &d, &m);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 1372 KB Output is correct
2 Correct 20 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 1372 KB Output is correct
2 Correct 28 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 1372 KB Output is correct
2 Correct 27 ms 1372 KB Output is correct
3 Correct 27 ms 1388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 3308 KB Output is correct
2 Correct 5 ms 3308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 3308 KB Output is correct
2 Correct 47 ms 3308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 3308 KB Output is correct
2 Correct 79 ms 3308 KB Output is correct
3 Correct 94 ms 3308 KB Output is correct
4 Correct 88 ms 3308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 120 ms 7036 KB Output is correct
2 Correct 126 ms 7040 KB Output is correct
3 Correct 118 ms 7040 KB Output is correct
4 Correct 98 ms 7040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 20476 KB Output is correct
2 Incorrect 23 ms 20476 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 20476 KB Output is correct
2 Correct 44 ms 20476 KB Output is correct
3 Incorrect 39 ms 20476 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 136 ms 20476 KB Output is correct
2 Correct 208 ms 20476 KB Output is correct
3 Incorrect 123 ms 20476 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 295 ms 22812 KB Output is correct
2 Correct 472 ms 22908 KB Output is correct
3 Incorrect 172 ms 22908 KB Output isn't correct