답안 #620015

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
620015 2022-08-02T18:48:15 Z 1ne Pairs (IOI07_pairs) C++14
70 / 100
4000 ms 5744 KB
#include<bits/stdc++.h>
using namespace std;
struct dataa{
	long long v = 0;
	void add(long long left,long long right,long long val){
		v+=val;
		//sum += val *(right - left + 1);
	}
};
long long S,T;
struct Segment_Tree{
	vector<dataa>tree;
	void init(long long n){
		tree.resize(2*n - 1);
	}
	dataa combine(dataa left,dataa right){
		dataa res;
		res.v = left.v + right.v;
		return res;
	}
	long long query(long long node,long long left,long long right,long long qleft,long long qright){
		if (qright<left|| qleft > right)return 0;
		if (qleft<=left && qright>=right){
			return tree[node].v;
		}
		long long mid = (left + right)>>1;
		long long z = node + ((mid - left + 1)<<1);
		return query(node + 1,left,mid,qleft,qright) + query(z,mid + 1,right,qleft,qright);
	}
	void update(long long node,long long left,long long right,long long uleft,long long uright,long long v){
		if (left > uright || right < uleft) return;
		if (uleft <= left && right <=uright){
			tree[node].add(left,right,v);
			return;
		}
		long long mid = (left + right)>>1;
		long long z = node + ((mid - left + 1)<<1);
		if (uleft<=mid){
			update(node + 1,left,mid,uleft,uright,v);
		}
		if (uright > mid){
			update(z,mid + 1,right,uleft,uright,v);
		}
		tree[node] = combine(tree[node + 1],tree[z]);
	}
};

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int b,n,d,m;cin>>b>>n>>d>>m;
	if (b == 1){
		vector<int>arr(n);
		for (int i = 0;i<n;++i)cin>>arr[i];
		sort(arr.begin(),arr.end());
		long long ans = 0;
		int l = 0;
		for (int i = 0;i<n;++i){
			while(l<n && arr[l] - arr[i]<=d)++l;
			ans+=l - i - 1;
		}
		cout<<ans<<'\n';
	}
	else if (b == 2){
		vector<int>arr(n),brr(n);
		for (int i = 0;i<n;++i){
			int x,y;cin>>x>>y;
			arr[i] = x + y;
			brr[i] = x - y + m;
		}
		vector<int>order(n);
		iota(order.begin(),order.end(),0);
		sort(order.begin(),order.end(),[&](int i,int j){
			return arr[i] < arr[j];
		});
		long long ans = 0;
		Segment_Tree st;
		st.init(m * 2 + 5);
		int l = 0;
		for (int i = 0;i<n;++i){
			while(l<n && arr[order[l]] - arr[order[i]]<=d){st.update(0,0,m * 2 + 4,brr[order[l]],brr[order[l]],1);++l;}
			ans+=st.query(0,0,m * 2 + 4,brr[order[i]] - d,brr[order[i]] + d);
			st.update(0,0,m * 2 + 4,brr[order[i]],brr[order[i]],-1);
		}
		cout<<ans - n<<'\n';
	}
	else{
		long long ans = 0;
		vector<vector<int>>arr(n,vector<int>(3));
		for (int i = 0;i<n;++i){
			for (int j = 0;j<3;++j){
				cin>>arr[i][j];
			}
		}
		for (int i = 0;i<n;++i){
			for (int j = i + 1;j<n;++j){
				long long sum = abs(arr[i][0] - arr[j][0]) + abs(arr[i][1] - arr[j][1]) + abs(arr[i][2] - arr[j][2]);
				if (sum<=d)ans++;
			}
		}
		cout<<ans<<'\n';
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 596 KB Output is correct
2 Correct 11 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 596 KB Output is correct
2 Correct 16 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 596 KB Output is correct
2 Correct 21 ms 724 KB Output is correct
3 Correct 16 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 1500 KB Output is correct
2 Correct 32 ms 1504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 1520 KB Output is correct
2 Correct 54 ms 1504 KB Output is correct
3 Correct 53 ms 1508 KB Output is correct
4 Correct 50 ms 1500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 3680 KB Output is correct
2 Correct 84 ms 3684 KB Output is correct
3 Correct 65 ms 3696 KB Output is correct
4 Correct 63 ms 3684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4072 ms 5744 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4073 ms 5716 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4064 ms 5716 KB Time limit exceeded
2 Halted 0 ms 0 KB -