답안 #54317

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
54317 2018-07-03T06:52:52 Z 윤교준(#1474) Sails (IOI07_sails) C++11
0 / 100
59 ms 1140 KB
#include <bits/stdc++.h>
#define rf(x) (x)=0;while(*p<48)p++;while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);
#define pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define INF (0x3f3f3f3f)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
static unsigned char str[55000055], *p=str;

const int MAXN = 100005;
const int MX = 131072;

struct SEG {
	int d[MAXN*3], dmx[MAXN*3], dmn[MAXN*3];

	void prop(int i, int s, int e) {
		if(s == e) {
			dmx[i] += d[i];
			dmn[i] += d[i];
			d[i] = 0;
		} else {
			dmx[i] = max(dmx[i*2], dmx[i*2+1]) + d[i];
			dmn[i] = min(dmn[i*2], dmn[i*2+1]) + d[i];
			d[i*2] += d[i];
			d[i*2+1] += d[i];
			d[i] = 0;
		}
	}
	void upd(int i, int s, int e, int p, int q, int r) {
	    prop(i, s, e); if(q < p || e < p || q < s) return;
		if(p <= s && e <= q) {
			d[i] += r;
			prop(i, s, e);
			return;
		}
		int m = (s+e)/2;
		upd(i*2, s, m, p, q, r);
		upd(i*2+1, m+1, e, p, q, r);
		prop(i, s, e);
	}
	void upd(int s, int e, int x) { upd(1, 1, 100000, s, e, x); }
	pii get(int i, int s, int e, int p, int q) {
	    prop(i, s, e); if(q < p || e < p || q < s) return pii(-INF, INF);
		if(p <= s && e <= q) return pii(dmx[i], dmn[i]);
		int m = (s+e)/2;
		pii l = get(i*2, s, m, p, q), r = get(i*2+1, m+1, e, p, q);
		prop(i, s, e);
		return pii(max(l.first, r.first) + d[i], min(l.second, r.second) + d[i]);
	}
	pii get(int s, int e) { return get(1, 1, 100000, s, e); }

	void getV(vector<pair<int, pii>> &V, int i, int s, int e, int p, int q) {
		prop(i, s, e); if(q < p || e < p || q < s) return;
		if(p <= s && e <= q) { V.eb(i, pii(s, e)); return; }
		int m = (s+e)/2;
		getV(V, i*2, s, m, p, q);
		getV(V, i*2+1, m+1, e, p, q);
		prop(i, s, e);
	}
	int _getMN(int i, int s, int e, int h) {
		prop(i, s, e); if(s == e) return s;
		int m = (s+e)/2;
        prop(i*2, s, m); prop(i*2+1, m+1, e);
		if(dmn[i*2] != h) {
			return _getMN(i*2, s, m, h);
		} else {
			return _getMN(i*2+1, m+1, e, h);
		}
	}
	int getMN(int s, int e, int h) {
		vector<pair<int, pii>> V;
		getV(V, 1, 1, 100000, s, e);
		if(dmn[V.back().first] == h) return e;
		int i, S, E;
		for(auto &v : V) {
			if(dmn[v.first] == h) continue;
			i = v.first;
			tie(S, E) = v.second;
			break;
		}
		return _getMN(i, S, E, h) - 1;
	}
	int _getMX(int i, int s, int e, int h) {
		prop(i, s, e); if(s == e) return s;
		int m = (s+e)/2;
		prop(i*2, s, m); prop(i*2+1, m+1, e);
		if(dmx[i*2+1] != h) {
			return _getMX(i*2+1, m+1, e, h);
		} else {
			return _getMX(i*2, s, m, h);
		}
	}
	int getMX(int s, int e, int h) {
		vector<pair<int, pii>> V;
		getV(V, 1, 1, 100000, s, e);
		if(dmx[V[0].first] == h) return s;
		revv(V);
		int i, S, E;
		for(auto &v : V) {
			if(dmx[v.first] == h) continue;
			i = v.first;
			tie(S, E) = v.second;
			break;
		}
		return _getMX(i, S, E, h) + 1;
	}
} seg;

int C[MAXN];
int A[MAXN], B[MAXN];

ll Ans;
int N;

int main() {
    freopen("input.txt", "r", stdin);
	fread(str, 1, 55000055, stdin);

	rf(N)

	{
	    vector<pii> V;
	    for(int i = 1, a, b; i <= N; i++) {
            rf(a) rf(b)
            V.eb(a, b);
	    }
	    sorv(V);
        for(int i = 0; i < N; i++)
            tie(A[i+1], B[i+1]) = V[i];
	}

	for(int i = 1; i <= N; i++) {
		int ex = A[i], sx = ex-B[i]+1;
		int sl = sx, su = sx;
		int h = seg.get(sx, sx).first;

		//printf("i=%d, h=%d : %d %d\n", i, h, sx, ex);

		/*
		{
			int s = sx, e = ex; for(int m; s < e;) {
				m = (s+e+1)/2;
				if(seg.get(sx, m).second < h) e = m-1;
				else s = m;
			}
			su = s;
		}
		{
			int s = 1, e = sx; for(int m; s < e;) {
				m = (s+e)/2;
				if(seg.get(m, sx).first > h) s = m+1;
				else e = m;
			}
			sl = s;
		}
		*/

		su = seg.getMN(sx, ex, h);
		sl = seg.getMX(1, sx, h);

		//printf("%d : (%d %d) :: %d %d\n", i, sx, ex, sl, su);

		{
			int l = su-sx+1;
			seg.upd(sl, sl+l-1, 1);
			//printf("SEG %d %d\n", sl, sl+l-1);
		}
		seg.upd(su+1, ex, 1);
		//printf("SEG %d %d\n", su+1, ex);
	}

	for(int i = 1; i <= 100000; i++) {
		int c = seg.get(i, i).first;
		//if(i <= 10) printf("%d : %d\n", i, c);
		Ans += ll(c) * (c-1) / 2;
	}

	cout << Ans << endl;
	return 0;
}

Compilation message

sails.cpp: In function 'int main()':
sails.cpp:121:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("input.txt", "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sails.cpp:122:7: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  fread(str, 1, 55000055, stdin);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sails.cpp:22:3: warning: 'e' may be used uninitialized in this function [-Wmaybe-uninitialized]
   if(s == e) {
   ^~
sails.cpp:120:5: note: 'e' was declared here
 int main() {
     ^~~~
sails.cpp:110:31: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
   return _getMX(i, S, E, h) + 1;
                               ^
sails.cpp:120:5: note: 's' was declared here
 int main() {
     ^~~~
sails.cpp:103:7: warning: 'i' may be used uninitialized in this function [-Wmaybe-uninitialized]
   int i, S, E;
       ^
sails.cpp:86:31: warning: 'e' may be used uninitialized in this function [-Wmaybe-uninitialized]
   return _getMN(i, S, E, h) - 1;
                               ^
sails.cpp:120:5: note: 'e' was declared here
 int main() {
     ^~~~
sails.cpp:22:3: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
   if(s == e) {
   ^~
sails.cpp:120:5: note: 's' was declared here
 int main() {
     ^~~~
sails.cpp:79:7: warning: 'i' may be used uninitialized in this function [-Wmaybe-uninitialized]
   int i, S, E;
       ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 41 ms 596 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 59 ms 628 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 40 ms 664 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 40 ms 800 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 43 ms 924 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 40 ms 924 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 41 ms 1020 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# 결과 실행 시간 메모리 Grader output
1 Runtime error 46 ms 1140 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# 결과 실행 시간 메모리 Grader output
1 Runtime error 41 ms 1140 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 52 ms 1140 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 40 ms 1140 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -