Submission #73229

# Submission time Handle Problem Language Result Execution time Memory
73229 2018-08-28T05:30:46 Z 김세빈(#2270) Circus (Balkan15_CIRCUS) C++11
100 / 100
1026 ms 21056 KB
#include "circus.h"

#include <bits/stdc++.h>

using namespace std;

typedef pair <int, int> pii;

const int inf = 0x3f3f3f3f;

struct node{
	int v, lz, id, mv, mid;
	node() {}
	node(int v, int lz, int id, int mv, int mid) :
		v(v), lz(lz), id(id), mv(mv), mid(mid) {}
		
	void update(int c)
	{
		lz = min(lz, c);
		
		if(v > mv + lz){
			v = mv + lz;
			id = mid;
		}
	}
};

node operator + (node &nl, node &nr)
{
	int v, lz, id, mv, mid;
	
	lz = inf;
	
	if(nl.mv < nr.mv) mv = nl.mv, mid = nl.mid;
	else mv = nr.mv, mid = nr.mid;
	
	if(nl.v < mv + lz && nl.v < nr.v) v = nl.v, id = nl.id;
	else if(nr.v < mv + lz) v = nr.v, id = nr.id;
	else v = mv + lz, id = mid;
	
	return node(v, lz, id, mv, mid);
}

struct seg{
	node T[303030];
	int sgn;
	
	void init(int p, int s, int e, int *P, int v)
	{
		if(s == e){
			T[p] = node(v + P[s] * sgn, v, s, P[s] * sgn, s);
		}
		else{
			init(p << 1, s, (s + e >> 1), P, v);
			init(p << 1 | 1, (s + e >> 1) + 1, e, P, v);
			
			T[p] = T[p << 1] + T[p << 1 | 1];
		}
	}
	
	pii get_minv() { return pii(T[1].v, T[1].id); }
	
	void update(int p, int s, int e, int l, int r, int v)
	{
		if(e < l || r < s) return;
		else if(l <= s && e <= r) T[p].update(v);
		else{
			T[p << 1].update(T[p].lz);
			T[p << 1 | 1].update(T[p].lz);
			
			update(p << 1, s, (s + e >> 1), l, r, v);
			update(p << 1 | 1, (s + e >> 1) + 1, e, l, r, v);
			
			T[p] = T[p << 1] + T[p << 1 | 1];
		}
	}
	
	void erase(int p, int s, int e, int v)
	{
		if(s == e){
			T[p] = node(inf, inf, inf, inf, inf);
		}
		else{
			T[p << 1].update(T[p].lz);
			T[p << 1 | 1].update(T[p].lz);
			
			if(v <= (s + e >> 1)) erase(p << 1, s, (s + e >> 1), v);
			else erase(p << 1 | 1, (s + e >> 1) + 1, e, v);
			
			T[p] = T[p << 1] + T[p << 1 | 1];
		}
	}
};

seg TX, TY;
vector <pii> L, R;
int D[101010];
int n;

void init(int N, int m, int P[])
{
	int i, v1, v2, x1, x2, l, r;
	
	n = N;

	sort(P, P + n);
	
	TX.sgn = -1; TY.sgn = 1;
	TX.init(1, 0, n - 1, P, m);
	TY.init(1, 0, n - 1, P, m);
	
	L.emplace_back(m, m);
	R.emplace_back(0, m);
	
	for(i=1; i<=n; i++){
		tie(v1, x1) = TX.get_minv();
		tie(v2, x2) = TY.get_minv();
		if(v1 > v2) swap(v1, v2), swap(x1, x2);
		
		TX.erase(1, 0, n - 1, x1);
		TY.erase(1, 0, n - 1, x1);
		
		l = upper_bound(P, P + n, P[x1] - v1) - P - 1;
		r = lower_bound(P, P + n, P[x1] + v1) - P;
		TX.update(1, 0, n - 1, 0, l, P[x1]);
		TY.update(1, 0, n - 1, r, n - 1, -P[x1]);
		
		L.emplace_back(P[x1] - v1, P[x1]);
		R.emplace_back(P[x1] + v1, -P[x1]);
	}
	
	sort(L.begin(), L.end());
	sort(R.begin(), R.end());
	
	for(i=L.size()-2; i>=0; i--){
		L[i].second = min(L[i].second, L[i + 1].second);
	}
	
	for(i=1; i<R.size(); i++){
		R[i].second = min(R[i].second, R[i - 1].second);
	}
}

int minLength(int d)
{
	int ret = inf, k;
	
	k = lower_bound(L.begin(), L.end(), pii(d, -inf)) - L.begin();
	if(k < L.size()) ret = min(ret, L[k].second - d);
	
	k = upper_bound(R.begin(), R.end(), pii(d, inf)) - R.begin() - 1;
	if(k >= 0) ret = min(ret, R[k].second + d);
	
	return ret;
}

Compilation message

circus.cpp: In member function 'void seg::init(int, int, int, int*, int)':
circus.cpp:54:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
    init(p << 1, s, (s + e >> 1), P, v);
                     ~~^~~
circus.cpp:55:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
    init(p << 1 | 1, (s + e >> 1) + 1, e, P, v);
                      ~~^~~
circus.cpp: In member function 'void seg::update(int, int, int, int, int, int)':
circus.cpp:71:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
    update(p << 1, s, (s + e >> 1), l, r, v);
                       ~~^~~
circus.cpp:72:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
    update(p << 1 | 1, (s + e >> 1) + 1, e, l, r, v);
                        ~~^~~
circus.cpp: In member function 'void seg::erase(int, int, int, int)':
circus.cpp:87:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
    if(v <= (s + e >> 1)) erase(p << 1, s, (s + e >> 1), v);
             ~~^~~
circus.cpp:87:46: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
    if(v <= (s + e >> 1)) erase(p << 1, s, (s + e >> 1), v);
                                            ~~^~~
circus.cpp:88:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
    else erase(p << 1 | 1, (s + e >> 1) + 1, e, v);
                            ~~^~~
circus.cpp: In function 'void init(int, int, int*)':
circus.cpp:139:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=1; i<R.size(); i++){
           ~^~~~~~~~~
circus.cpp: In function 'int minLength(int)':
circus.cpp:149:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if(k < L.size()) ret = min(ret, L[k].second - d);
     ~~^~~~~~~~~~
grader.cpp: In function 'int main()':
grader.cpp:14:12: warning: unused variable 'max_code' [-Wunused-variable]
  long long max_code;
            ^~~~~~~~
grader.cpp:16:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~
grader.cpp:18:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &P[i]);
   ~~~~~^~~~~~~~~~~~~
grader.cpp:21:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &Q);
  ~~~~~^~~~~~~~~~
grader.cpp:23:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &d);
   ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 293 ms 12876 KB Output is correct
2 Correct 286 ms 12916 KB Output is correct
3 Correct 293 ms 12916 KB Output is correct
4 Correct 279 ms 12924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12924 KB Output is correct
2 Correct 3 ms 12924 KB Output is correct
3 Correct 3 ms 12924 KB Output is correct
4 Correct 3 ms 12924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12924 KB Output is correct
2 Correct 3 ms 12924 KB Output is correct
3 Correct 3 ms 12924 KB Output is correct
4 Correct 3 ms 12924 KB Output is correct
5 Correct 9 ms 12924 KB Output is correct
6 Correct 7 ms 12924 KB Output is correct
7 Correct 9 ms 12924 KB Output is correct
8 Correct 6 ms 12924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12924 KB Output is correct
2 Correct 3 ms 12924 KB Output is correct
3 Correct 3 ms 12924 KB Output is correct
4 Correct 3 ms 12924 KB Output is correct
5 Correct 9 ms 12924 KB Output is correct
6 Correct 7 ms 12924 KB Output is correct
7 Correct 9 ms 12924 KB Output is correct
8 Correct 6 ms 12924 KB Output is correct
9 Correct 547 ms 12924 KB Output is correct
10 Correct 504 ms 12924 KB Output is correct
11 Correct 508 ms 12924 KB Output is correct
12 Correct 541 ms 12924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 293 ms 12876 KB Output is correct
2 Correct 286 ms 12916 KB Output is correct
3 Correct 293 ms 12916 KB Output is correct
4 Correct 279 ms 12924 KB Output is correct
5 Correct 2 ms 12924 KB Output is correct
6 Correct 3 ms 12924 KB Output is correct
7 Correct 3 ms 12924 KB Output is correct
8 Correct 3 ms 12924 KB Output is correct
9 Correct 9 ms 12924 KB Output is correct
10 Correct 7 ms 12924 KB Output is correct
11 Correct 9 ms 12924 KB Output is correct
12 Correct 6 ms 12924 KB Output is correct
13 Correct 268 ms 13092 KB Output is correct
14 Correct 289 ms 13212 KB Output is correct
15 Correct 261 ms 13212 KB Output is correct
16 Correct 250 ms 13212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 293 ms 12876 KB Output is correct
2 Correct 286 ms 12916 KB Output is correct
3 Correct 293 ms 12916 KB Output is correct
4 Correct 279 ms 12924 KB Output is correct
5 Correct 2 ms 12924 KB Output is correct
6 Correct 3 ms 12924 KB Output is correct
7 Correct 3 ms 12924 KB Output is correct
8 Correct 3 ms 12924 KB Output is correct
9 Correct 9 ms 12924 KB Output is correct
10 Correct 7 ms 12924 KB Output is correct
11 Correct 9 ms 12924 KB Output is correct
12 Correct 6 ms 12924 KB Output is correct
13 Correct 547 ms 12924 KB Output is correct
14 Correct 504 ms 12924 KB Output is correct
15 Correct 508 ms 12924 KB Output is correct
16 Correct 541 ms 12924 KB Output is correct
17 Correct 268 ms 13092 KB Output is correct
18 Correct 289 ms 13212 KB Output is correct
19 Correct 261 ms 13212 KB Output is correct
20 Correct 250 ms 13212 KB Output is correct
21 Correct 919 ms 18212 KB Output is correct
22 Correct 966 ms 18212 KB Output is correct
23 Correct 1006 ms 19700 KB Output is correct
24 Correct 1026 ms 21056 KB Output is correct