Submission #73228

# Submission time Handle Problem Language Result Execution time Memory
73228 2018-08-28T05:29:29 Z 김세빈(#2270) Circus (Balkan15_CIRCUS) C++11
100 / 100
1268 ms 20824 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];
		}
	}
	
	void print()
	{
		int i;
		
		for(i=1; i<=7; i++){
			printf("%d %d %d %d %d\n", T[i].v, T[i].lz, T[i].id, T[i].mv, T[i].mid);
		}
			printf("\n");
	}
};

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);
	
//	TY.print();
	
	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);
		
//		printf("%d : %d\n", x1, v1);
		
		TX.erase(1, 0, n - 1, x1);
		TY.erase(1, 0, n - 1, x1);
		
//		TY.print();
		
		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]);
		
//		TY.print();
	}
	
	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);
	}
	
//	for(pii &p: L) printf("%d %d\n", p.first, p.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:157: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:169: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 292 ms 12828 KB Output is correct
2 Correct 310 ms 13032 KB Output is correct
3 Correct 299 ms 13032 KB Output is correct
4 Correct 301 ms 13032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13032 KB Output is correct
2 Correct 3 ms 13032 KB Output is correct
3 Correct 3 ms 13032 KB Output is correct
4 Correct 2 ms 13032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13032 KB Output is correct
2 Correct 3 ms 13032 KB Output is correct
3 Correct 3 ms 13032 KB Output is correct
4 Correct 2 ms 13032 KB Output is correct
5 Correct 6 ms 13032 KB Output is correct
6 Correct 7 ms 13032 KB Output is correct
7 Correct 7 ms 13032 KB Output is correct
8 Correct 8 ms 13032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13032 KB Output is correct
2 Correct 3 ms 13032 KB Output is correct
3 Correct 3 ms 13032 KB Output is correct
4 Correct 2 ms 13032 KB Output is correct
5 Correct 6 ms 13032 KB Output is correct
6 Correct 7 ms 13032 KB Output is correct
7 Correct 7 ms 13032 KB Output is correct
8 Correct 8 ms 13032 KB Output is correct
9 Correct 568 ms 13032 KB Output is correct
10 Correct 540 ms 13032 KB Output is correct
11 Correct 505 ms 13032 KB Output is correct
12 Correct 604 ms 13032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 292 ms 12828 KB Output is correct
2 Correct 310 ms 13032 KB Output is correct
3 Correct 299 ms 13032 KB Output is correct
4 Correct 301 ms 13032 KB Output is correct
5 Correct 3 ms 13032 KB Output is correct
6 Correct 3 ms 13032 KB Output is correct
7 Correct 3 ms 13032 KB Output is correct
8 Correct 2 ms 13032 KB Output is correct
9 Correct 6 ms 13032 KB Output is correct
10 Correct 7 ms 13032 KB Output is correct
11 Correct 7 ms 13032 KB Output is correct
12 Correct 8 ms 13032 KB Output is correct
13 Correct 327 ms 13148 KB Output is correct
14 Correct 340 ms 13196 KB Output is correct
15 Correct 309 ms 13196 KB Output is correct
16 Correct 285 ms 13212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 292 ms 12828 KB Output is correct
2 Correct 310 ms 13032 KB Output is correct
3 Correct 299 ms 13032 KB Output is correct
4 Correct 301 ms 13032 KB Output is correct
5 Correct 3 ms 13032 KB Output is correct
6 Correct 3 ms 13032 KB Output is correct
7 Correct 3 ms 13032 KB Output is correct
8 Correct 2 ms 13032 KB Output is correct
9 Correct 6 ms 13032 KB Output is correct
10 Correct 7 ms 13032 KB Output is correct
11 Correct 7 ms 13032 KB Output is correct
12 Correct 8 ms 13032 KB Output is correct
13 Correct 568 ms 13032 KB Output is correct
14 Correct 540 ms 13032 KB Output is correct
15 Correct 505 ms 13032 KB Output is correct
16 Correct 604 ms 13032 KB Output is correct
17 Correct 327 ms 13148 KB Output is correct
18 Correct 340 ms 13196 KB Output is correct
19 Correct 309 ms 13196 KB Output is correct
20 Correct 285 ms 13212 KB Output is correct
21 Correct 1135 ms 18124 KB Output is correct
22 Correct 1176 ms 18124 KB Output is correct
23 Correct 1216 ms 19768 KB Output is correct
24 Correct 1268 ms 20824 KB Output is correct