Submission #54304

# Submission time Handle Problem Language Result Execution time Memory
54304 2018-07-03T06:34:27 Z 윤교준(#1474) Sails (IOI07_sails) C++11
0 / 100
285 ms 5796 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 _upd(int i, int s, int e) {
		if(s == e) {
			dmx[i] = dmn[i] = d[i];
		} 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];
		}
	}
	void upd(int i, int s, int e, int p, int q, int r) {
		if(q < p || e < p || q < s) return;
		if(p <= s && e <= q) {
			d[i] += r;
			_upd(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);
		_upd(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) {
	    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);
		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) {
		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);
	}
	int _getMN(int i, int s, int e, int h) {
		if(s == e) return s;
		int m = (s+e)/2;
		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) {
		if(s == e) return s;
		int m = (s+e)/2;
		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);
		}
		seg.upd(su+1, ex, 1);
	}

	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:113: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:82:13: warning: 'E' may be used uninitialized in this function [-Wmaybe-uninitialized]
   int m = (s+e)/2;
           ~~^~~
sails.cpp:94:13: note: 'E' was declared here
   int i, S, E;
             ^
sails.cpp:82:13: warning: 'S' may be used uninitialized in this function [-Wmaybe-uninitialized]
   int m = (s+e)/2;
           ~~^~~
sails.cpp:94:10: note: 'S' was declared here
   int i, S, E;
          ^
sails.cpp:83:11: warning: 'i' may be used uninitialized in this function [-Wmaybe-uninitialized]
   if(dmx[i*2+1] != h) {
          ~^~
sails.cpp:94:7: note: 'i' was declared here
   int i, S, E;
       ^
sails.cpp:60:13: warning: 'e' may be used uninitialized in this function [-Wmaybe-uninitialized]
   int m = (s+e)/2;
           ~~^~~
sails.cpp:67:6: note: 'e' was declared here
  int getMN(int s, int e, int h) {
      ^~~~~
sails.cpp:60:13: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
   int m = (s+e)/2;
           ~~^~~
sails.cpp:67:6: note: 's' was declared here
  int getMN(int s, int e, int h) {
      ^~~~~
sails.cpp:61:11: warning: 'i' may be used uninitialized in this function [-Wmaybe-uninitialized]
   if(dmn[i*2] != h) {
          ~^~
sails.cpp:67:6: note: 'i' was declared here
  int getMN(int s, int e, int h) {
      ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 30 ms 376 KB Output is correct
2 Incorrect 19 ms 480 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 18 ms 560 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 596 KB Output is correct
2 Incorrect 19 ms 644 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 19 ms 720 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 976 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 36 ms 1872 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 81 ms 2356 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 184 ms 3280 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 230 ms 5352 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 233 ms 5520 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 285 ms 5796 KB Output isn't correct
2 Halted 0 ms 0 KB -