Submission #9836

# Submission time Handle Problem Language Result Execution time Memory
9836 2014-09-30T10:21:29 Z Qwaz 허수아비 (JOI14_scarecrows) C++
100 / 100
204 ms 7336 KB
#include <cstdio>
#include <algorithm>

using namespace std;
typedef long long ll;
const int MAX = 200020;

struct point {
	int x, y;

	bool operator < (const point &t) const {
		return x < t.x;
	}
};

int n;
point data[MAX], tmp[MAX];

void init(){
	scanf("%d", &n);

	int i;
	for(i = 0; i<n; i++)
		scanf("%d%d", &data[i].x, &data[i].y);
	sort(data, data+n);
}

point lStack[MAX], rStack[MAX];
int lTop, rTop;

ll solve(int s, int e){
	if(s >= e-1)
		return 0;

	int m = (s+e)>>1;

	ll ans = 0;
	ans += solve(s, m);
	ans += solve(m, e);

	lTop = rTop = 0;

	int r, l = s;
	for(r = m; r<e; r++){
		while(l < m && data[l].y < data[r].y){
			while(lTop && lStack[lTop-1].x < data[l].x)
				lTop--;
			lStack[lTop++] = data[l++];
		}

		int minY;
		while(rTop && rStack[rTop-1].x > data[r].x)
			rTop--;
		minY = rTop ? rStack[rTop-1].y : -1;
		rStack[rTop++] = data[r];

		int p = 0, q = lTop, r;
		while(p < q){
			r = (p+q) >> 1;
			if(lStack[r].y > minY)
				q = r;
			else
				p = r+1;
		}

		ans += lTop-p;
	}

	int tFull = 0;
	l = s;
	r = m;
	while(1){
		if(l == m && r == e)
			break;

		if((l < m && data[l].y < data[r].y) || r == e)
			tmp[tFull++] = data[l++];
		else
			tmp[tFull++] = data[r++];
	}

	int i;
	for(i = 0; i<tFull; i++)
		data[s+i] = tmp[i];

	return ans;
}

int main(){
	init();

	printf("%lld\n", solve(0, n));

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 7336 KB Output is correct
2 Correct 0 ms 7336 KB Output is correct
3 Correct 0 ms 7336 KB Output is correct
4 Correct 0 ms 7336 KB Output is correct
5 Correct 0 ms 7336 KB Output is correct
6 Correct 0 ms 7336 KB Output is correct
7 Correct 0 ms 7336 KB Output is correct
8 Correct 0 ms 7336 KB Output is correct
9 Correct 0 ms 7336 KB Output is correct
10 Correct 0 ms 7336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 7336 KB Output is correct
2 Correct 4 ms 7336 KB Output is correct
3 Correct 0 ms 7336 KB Output is correct
4 Correct 0 ms 7336 KB Output is correct
5 Correct 4 ms 7336 KB Output is correct
6 Correct 4 ms 7336 KB Output is correct
7 Correct 4 ms 7336 KB Output is correct
8 Correct 0 ms 7336 KB Output is correct
9 Correct 4 ms 7336 KB Output is correct
10 Correct 4 ms 7336 KB Output is correct
11 Correct 4 ms 7336 KB Output is correct
12 Correct 0 ms 7336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 7336 KB Output is correct
2 Correct 204 ms 7336 KB Output is correct
3 Correct 128 ms 7336 KB Output is correct
4 Correct 116 ms 7336 KB Output is correct
5 Correct 136 ms 7336 KB Output is correct
6 Correct 152 ms 7336 KB Output is correct
7 Correct 176 ms 7336 KB Output is correct
8 Correct 196 ms 7336 KB Output is correct
9 Correct 112 ms 7336 KB Output is correct
10 Correct 144 ms 7336 KB Output is correct
11 Correct 160 ms 7336 KB Output is correct
12 Correct 196 ms 7336 KB Output is correct
13 Correct 196 ms 7336 KB Output is correct
14 Correct 96 ms 7336 KB Output is correct
15 Correct 164 ms 7336 KB Output is correct
16 Correct 180 ms 7336 KB Output is correct
17 Correct 100 ms 7336 KB Output is correct
18 Correct 180 ms 7336 KB Output is correct
19 Correct 196 ms 7336 KB Output is correct
20 Correct 184 ms 7336 KB Output is correct