답안 #355631

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
355631 2021-01-22T22:05:04 Z ogibogi2004 Pairs (IOI07_pairs) C++14
80 / 100
323 ms 33232 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int b,n,d,m;
void solve1()
{
	vector<int>v;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		v.push_back(x);
	}
	sort(v.begin(),v.end());
	queue<int>q;
	long long ans=0;
	for(int i=0;i<v.size();i++)
	{
		q.push(v[i]);
		while(v[i]-q.front()>d)
		{
			q.pop();
		}
		ans+=q.size()-1;
	}
	cout<<ans<<endl;
}
vector<int>animals[500000];
int bit[500000];
void update(int idx,int val)
{
	idx++;
	for(;idx<500000;idx+=idx&(-idx))
	{
		bit[idx]+=val;
	}
}
int query(int idx)
{
	//cout<<idx<<endl;
	idx++;
	int ret=0;
	for(;idx>0;idx-=idx&(-idx))
	{
		ret+=bit[idx];
	}
	return ret;
}
ll query(int l,int r)
{
	return query(r)-query(l-1);
}
void solve2()
{
	vector<pair<int,int> >v;
	int min1=(int)1e9,min2=(int)1e9;
	for(int i=1;i<=n;i++)
	{
		int x,y;
		cin>>x>>y;
		v.push_back({x-y,x+y});
		min1=min(min1,v.back().first);
		min2=min(min2,v.back().second);
	}
	for(int i=0;i<v.size();i++)
	{
		v[i].first-=min1;
		v[i].second-=min2;
	}
	for(auto xd:v)
	{
		animals[xd.first].push_back(xd.second);
	}
	ll ans=0;
	for(int i=0;i<500000;i++)
	{
		//cout<<i<<endl;
		sort(animals[i].begin(),animals[i].end());
		if(i-d-1>=0)
		{
			for(auto a:animals[i-d-1])
			{
				update(a,-1);
			}
		}
		for(auto a:animals[i])
		{
			ans+=query(a-d,a+d);
			update(a,+1);
		}
	}
	cout<<ans<<endl;
}
int bit3[200][200][200];
void update3(int idx1,int idx2,int idx3,int val)
{
	idx1++;
	idx2++;
	idx3++;
	for(int i1=idx1;i1<200;i1+=i1&(-i1))
	for(int i2=idx2;i2<200;i2+=i2&(-i2))
	for(int i3=idx3;i3<200;i3+=i3&(-i3))
	{
		bit3[i1][i2][i3]+=val;
	}
}
int query3(int idx1,int idx2,int idx3)
{
	idx1++;
	idx2++;
	idx3++;
	if(idx1>=200)idx1=199;
	if(idx2>=200)idx2=199;
	if(idx3>=200)idx3=199;
	int ans=0;
	for(int i1=idx1;i1>0;i1-=i1&(-i1))
	for(int i2=idx2;i2>0;i2-=i2&(-i2))
	for(int i3=idx3;i3>0;i3-=i3&(-i3))
	{
		ans+=bit3[i1][i2][i3];
	}
	return ans;
}
int query3(int x1,int y1,int z1,int x2,int y2,int z2)
{
	return query3(x2,y2,z2)-query3(x1-1,y2,z2)-query3(x2,y1-1,z2)-query3(x2,y2,z1-1)+query3(x2,y1-1,z1-1)+query3(x1-1,y2,z1-1)+query3(x1-1,y1-1,z2)-query3(x1-1,y1-1,z1-1);
}
struct point
{
	int x,y,z;
	bool operator<(point const& other)const
	{
		return make_pair(x,make_pair(y,z))<make_pair(other.x,make_pair(other.y,other.z));
	}
};
vector<point>ivan[300];
void solve3()
{
	int min1,min2,min3=(int)1e9;
	min1=min3;min2=min3;
	for(int i=1;i<=n;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		ivan[a+b+c].push_back({a+b-c,a-b+c,a-b-c});
		min1=min(min1,ivan[a+b+c].back().x);
		min2=min(min2,ivan[a+b+c].back().y);
		min3=min(min3,ivan[a+b+c].back().z);
	}
	for(int i=0;i<300;i++)
	{
		for(int j=0;j<ivan[i].size();j++)
		{
			ivan[i][j].x-=min1;
			ivan[i][j].y-=min2;
			ivan[i][j].z-=min3;
		}
	}
	ll ans=0;
	for(int i=0;i<300;i++)
	{
		sort(ivan[i].begin(),ivan[i].end());
		if(i-d-1>=0)
		{
			for(auto a:ivan[i-d-1])
			{
				update3(a.x,a.y,a.z,-1);
			}
		}
		for(auto a:ivan[i])
		{
			//cout<<i<<" "<<a.x<<" "<<a.z<<" "<<a.y<<endl;
			ans+=query3(a.x-d,a.y-d,a.z-d,a.x+d,a.y+d,a.z+d);
			update3(a.x,a.y,a.z,+1);
		}
	}
	cout<<ans<<endl;
}
int main()
{
	cin>>b>>n>>d>>m;
	if(b==1)solve1();
	if(b==2)solve2();
	if(b==3)solve3();
return 0;
}

Compilation message

pairs.cpp: In function 'void solve1()':
pairs.cpp:17:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |  for(int i=0;i<v.size();i++)
      |              ~^~~~~~~~~
pairs.cpp: In function 'void solve2()':
pairs.cpp:65:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |  for(int i=0;i<v.size();i++)
      |              ~^~~~~~~~~
pairs.cpp: In function 'void solve3()':
pairs.cpp:152:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |   for(int j=0;j<ivan[i].size();j++)
      |               ~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12012 KB Output is correct
2 Correct 8 ms 12012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 12648 KB Output is correct
2 Correct 47 ms 12784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 12576 KB Output is correct
2 Correct 56 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 12648 KB Output is correct
2 Correct 58 ms 12648 KB Output is correct
3 Correct 55 ms 12648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 12652 KB Output is correct
2 Correct 13 ms 12652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 13536 KB Output is correct
2 Correct 63 ms 13792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 13696 KB Output is correct
2 Correct 79 ms 13920 KB Output is correct
3 Correct 83 ms 13792 KB Output is correct
4 Correct 72 ms 14048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 15456 KB Output is correct
2 Correct 104 ms 15840 KB Output is correct
3 Correct 84 ms 14432 KB Output is correct
4 Correct 83 ms 14432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 22380 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 152 ms 15212 KB Output is correct
2 Correct 160 ms 15340 KB Output is correct
3 Correct 139 ms 15212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 264 ms 28396 KB Output is correct
2 Correct 244 ms 28524 KB Output is correct
3 Correct 155 ms 28396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 323 ms 33232 KB Output isn't correct
2 Halted 0 ms 0 KB -