Submission #197662

# Submission time Handle Problem Language Result Execution time Memory
197662 2020-01-22T05:55:10 Z arnold518 Pairs (IOI07_pairs) C++14
100 / 100
1752 ms 133272 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;

struct Point
{
	int x, y, z;
	Point() {}
	Point(int x) : x(x), y(0), z(0) {}
	Point(int x, int y) : x(x), y(y), z(0) {}
	Point(int x, int y, int z) : x(x), y(y), z(z) {}
	bool operator < (const Point &p) const { return x<p.x; }
};

struct Query
{
	int x, y1, y2, d;
	bool operator < (const Query &p) const { return x<p.x; }
};

int B, N, D, M;
Point A[MAXN+10];
ll ans;

struct BIT
{
	vector<int> tree;
	BIT() : tree(2*M) {}
	void update(int i) { for(; i<2*M; i+=(i&-i)) tree[i]++; }
	int query(int i) { int ret=0; for(; i>0; i-=(i&-i)) ret+=tree[i]; return ret; }
	int query(int l, int r) { return query(r)-query(l-1); }
};

ll f(vector<Point> &V, vector<Point> &S, vector<int> &R)
{
	int i, j;

	vector<Point> V2;
	for(i=0; i<V.size(); i++) V2.push_back(Point(V[i].x+V[i].y-1, V[i].x-V[i].y+M));

	vector<Query> S2;
	for(i=0; i<S.size(); i++)
	{
		int x1=S[i].x+S[i].y-1-R[i], x2=S[i].x+S[i].y-1+R[i];
		int y1=S[i].x-S[i].y+M-R[i], y2=S[i].x-S[i].y+M+R[i];
		x1=max(x1, 1); x2=min(x2, 2*M-1);
		y1=max(y1, 1); y2=min(y2, 2*M-1);

		S2.push_back({x1-1, y1, y2, -1});
		S2.push_back({x2, y1, y2, 1});
	}
	sort(S2.begin(), S2.end());
	sort(V2.begin(), V2.end());

	BIT bit=BIT();
	ll ret=0;
	for(i=0, j=0; i<S2.size(); i++)
	{
		for(; j<V2.size() && V2[j].x<=S2[i].x; j++) bit.update(V2[j].y);
		ret+=S2[i].d*bit.query(S2[i].y1, S2[i].y2);
	}
	return ret;
}

vector<Point> V[76], S[76];
vector<int> R[76];

int main()
{
	int i, j;

	scanf("%d%d%d%d", &B, &N, &D, &M);
	for(i=1; i<=N; i++)
	{
		if(B==1) scanf("%d", &A[i].x);
		if(B==2) scanf("%d%d", &A[i].x, &A[i].y);
		if(B==3) scanf("%d%d%d", &A[i].x, &A[i].y, &A[i].z);
	}

	if(B==1)
	{
		sort(A+1, A+N+1);
		for(i=1; i<=N; i++) ans+=upper_bound(A+1, A+N+1, Point(A[i].x+D))-lower_bound(A+1, A+N+1, Point(A[i].x-D));
		ans-=N; ans/=2;
		printf("%lld", ans);
		return 0;
	}

	if(B==2)
	{
		vector<Point> V, S;
		vector<int> R;
		for(i=1; i<=N; i++) V.push_back(A[i]), S.push_back(A[i]), R.push_back(D);
		ans=f(V, S, R);
		ans-=N; ans/=2;
		printf("%lld", ans);
		return 0;
	}

	if(B==3)
	{
		for(i=1; i<=N; i++)
		{
			V[A[i].z].push_back(A[i]);
			for(j=1; j<=M; j++)
			{
				if(D-abs(j-A[i].z)<0) continue;
				S[j].push_back(A[i]);
				R[j].push_back(D-abs(j-A[i].z));
			}
		}
		for(i=1; i<=M; i++) ans+=f(V[i], S[i], R[i]);

		ans-=N; ans/=2;
		printf("%lld", ans);
		return 0;
	}
}

Compilation message

pairs.cpp: In function 'll f(std::vector<Point>&, std::vector<Point>&, std::vector<int>&)':
pairs.cpp:44:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<V.size(); i++) V2.push_back(Point(V[i].x+V[i].y-1, V[i].x-V[i].y+M));
           ~^~~~~~~~~
pairs.cpp:47:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<S.size(); i++)
           ~^~~~~~~~~
pairs.cpp:62:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0, j=0; i<S2.size(); i++)
                ~^~~~~~~~~~
pairs.cpp:64:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(; j<V2.size() && V2[j].x<=S2[i].x; j++) bit.update(V2[j].y);
         ~^~~~~~~~~~
pairs.cpp: In function 'int main()':
pairs.cpp:77:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d", &B, &N, &D, &M);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
pairs.cpp:80:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   if(B==1) scanf("%d", &A[i].x);
            ~~~~~^~~~~~~~~~~~~~~
pairs.cpp:81:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   if(B==2) scanf("%d%d", &A[i].x, &A[i].y);
            ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
pairs.cpp:82:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   if(B==3) scanf("%d%d%d", &A[i].x, &A[i].y, &A[i].z);
            ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 1524 KB Output is correct
2 Correct 26 ms 1444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 1528 KB Output is correct
2 Correct 33 ms 1528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 1528 KB Output is correct
2 Correct 36 ms 1656 KB Output is correct
3 Correct 33 ms 1536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1016 KB Output is correct
2 Correct 3 ms 1016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 10652 KB Output is correct
2 Correct 53 ms 10652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 10700 KB Output is correct
2 Correct 63 ms 10656 KB Output is correct
3 Correct 63 ms 10652 KB Output is correct
4 Correct 63 ms 10652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 10652 KB Output is correct
2 Correct 76 ms 10652 KB Output is correct
3 Correct 73 ms 10780 KB Output is correct
4 Correct 71 ms 10656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 1404 KB Output is correct
2 Correct 10 ms 1656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 10056 KB Output is correct
2 Correct 262 ms 26956 KB Output is correct
3 Correct 208 ms 26984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 463 ms 41620 KB Output is correct
2 Correct 1373 ms 106728 KB Output is correct
3 Correct 918 ms 108600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1135 ms 92632 KB Output is correct
2 Correct 1752 ms 132456 KB Output is correct
3 Correct 1562 ms 133272 KB Output is correct