답안 #522009

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
522009 2022-02-03T15:17:43 Z emanuelsilva Pairs (IOI07_pairs) C++17
100 / 100
307 ms 36012 KB
#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'

typedef long long ll;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T> 
	using ord_set = tree<T, null_type, less<T>, rb_tree_tag,
	tree_order_statistics_node_update>;

const int MAX = 4*76;
const int SHIFT = 76;

int bit[MAX][MAX][MAX];

void fix(int& val) {
	val = max(val, 0);
	val = min(val, MAX-1);
}

int pref(int x, int y, int z) {
	int ret = 0;
	x++, y++, z++;
	fix(x), fix(y), fix(z);
	for (; x; x -= x & -x) for (int y2 = y; y2; y2 -= y2 & -y2)
		for (int z2 = z; z2; z2 -= z2 & -z2) ret += bit[x][y2][z2];
	return ret;
}

void update(int x, int y, int z, int val) {
	x++, y++, z++;
	for (; x < MAX; x += x & -x) for (int y2 = y; y2 < MAX; y2 += y2 & -y2)
		for (int z2 = z; z2 < MAX; z2 += z2 & -z2) bit[x][y2][z2] += val;
}

int query(int x0, int y0, int z0, int x1, int y1, int z1) { 
	return pref(x1, y1, z1) 
	- pref(x1, y1, z0-1) - pref(x1, y0-1, z1)  - pref(x0-1, y1, z1) 
	+ pref(x1, y0-1, z0-1) + pref(x0-1, y1, z0-1) + pref(x0-1, y0-1, z1)
	- pref(x0-1, y0-1, z0-1); 
}

void solve1(int n, int d, int m) {
	vector<pair<int, int>> ev;
	for (int i = 0; i<n; i++) {
		int x; cin >> x;
		ev.emplace_back(x, 0);
		ev.emplace_back(x+d, 1);
	}
	sort(ev.begin(), ev.end());

	int cnt = 0;
	ll ans = 0;
	for (auto [x, ty] : ev) {
		if (ty == 0) ans += cnt, cnt++;
		else cnt--;
	}
	cout << ans << endl;
}

void solve2(int n, int d, int m) {
	vector<tuple<int, int, int, int>> ev;
	for (int i = 0; i<n; i++) {
		int x, y; cin >> x >> y;
		int nx = x + y;
		y = x - y;
		x = nx;

		ev.emplace_back(x, 0, y, i);
		ev.emplace_back(x+d, 1, y, i);
	}
	sort(ev.begin(), ev.end());
	ord_set<pair<int, int>> st;
	ll ans = 0;
	for (auto [x, ty, y, i] : ev) {
		if (ty == 0) {
			ans += st.order_of_key({y+d, INF}) - st.order_of_key({y-d, -INF});
			st.insert({y, i});
		}
		else st.erase({y, i});
	}
	cout << ans << endl;
}

void solve3(int n, int d, int m) {
	vector<tuple<int, int, int, int, int>> ev; // w ty x y z
	for (int i = 0; i<n; i++) {
		int x, y, z; cin >> x >> y >> z;
		int nx = x + y + z + SHIFT;
		int ny = x + y - z + SHIFT;
		int nz = x - y + z + SHIFT;
		int nw = x - y - z;

		ev.emplace_back(nw, 0, nx, ny, nz);
		ev.emplace_back(nw+d, 1, nx, ny, nz);
	}
	sort(ev.begin(), ev.end());
	ll ans = 0;
	for (auto [w, ty, x, y, z] : ev) {
		if (ty == 0) {
			ans += query(x-d, y-d, z-d, x+d, y+d, z+d);
			update(x, y, z, +1);
		}
		else update(x, y, z, -1);
	}
	cout << ans << endl;

}

int main() { _
	int b, n, d, m; cin >> b >> n >> d >> m;
	if (b == 1) solve1(n, d, m);
	if (b == 2) solve2(n, d, m);
	if (b == 3) solve3(n, d, m);


	exit(0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 2832 KB Output is correct
2 Correct 27 ms 2888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 3192 KB Output is correct
2 Correct 27 ms 3256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 3192 KB Output is correct
2 Correct 27 ms 3272 KB Output is correct
3 Correct 31 ms 3268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 5084 KB Output is correct
2 Correct 91 ms 9968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 5252 KB Output is correct
2 Correct 119 ms 7540 KB Output is correct
3 Correct 112 ms 7164 KB Output is correct
4 Correct 112 ms 6760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 5568 KB Output is correct
2 Correct 153 ms 7936 KB Output is correct
3 Correct 101 ms 9152 KB Output is correct
4 Correct 94 ms 7324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 15280 KB Output is correct
2 Correct 8 ms 15308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 160 ms 6176 KB Output is correct
2 Correct 126 ms 6172 KB Output is correct
3 Correct 151 ms 6192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 240 ms 25808 KB Output is correct
2 Correct 274 ms 25868 KB Output is correct
3 Correct 159 ms 25860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 307 ms 35836 KB Output is correct
2 Correct 262 ms 35892 KB Output is correct
3 Correct 179 ms 36012 KB Output is correct