제출 #48857

#제출 시각아이디문제언어결과실행 시간메모리
48857kriii새 집 (APIO18_new_home)C++17
57 / 100
5113 ms277912 KiB
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <map>
#include <set>
using namespace std;

int N,K,Q;
int C,XP[1001001];
map<int, pair<vector<pair<int, int> >, vector<pair<int, int> > > > query;
map<int, int> make[300300];
int ans[300300];

const int non = 500000000;
const int Z = 1<<20;
multiset<int> upc[Z];
multiset<int, greater<int> > downc[Z];
int up[Z*2],down[Z*2];

void push(int v, int u, int d, bool in)
{
	int p = lower_bound(XP, XP+C, v) - XP;
	{
		int x = p;
		if (in) upc[x].insert(u);
		else upc[x].erase(upc[x].find(u));

		if (upc[x].empty()) up[x+Z] = non;
		else up[x+Z] = *upc[x].begin();

		x = (x + Z) / 2;
		while (x){
			up[x] = min(up[x*2], up[x*2+1]);
			x /= 2;
		}
	}

	{
		int x = p;
		if (in) downc[x].insert(d);
		else downc[x].erase(downc[x].find(d));
		if (downc[x].empty()) down[x+Z] = -non;
		else down[x+Z] = *downc[x].begin();

		x = (x + Z) / 2;
		while (x){
			down[x] = max(down[x*2], down[x*2+1]);
			x /= 2;
		}
	}
}

int out(int v)
{
	int u = non, d = -non;
	{
		int x = lower_bound(XP,XP+C,v) - XP + Z, y = C-1 + Z;
		while (x < y){
			if (x & 1) u = min(u, up[x++]);
			if (~y & 1) u = min(u, up[y--]);
			x /= 2; y /= 2;
		}
		if (x == y) u = min(u, up[x]);
	}
	{
		int x = Z, y = upper_bound(XP,XP+C,v) - XP + Z - 1;
		while (x < y){
			if (x & 1) d = max(d, down[x++]);
			if (~y & 1) d = max(d, down[y--]);
			x /= 2; y /= 2;
		}
		if (x == y) d = max(d, down[x]);
	}

	return max(v-u,-v+d);
}

void mt(int s, int e, bool in)
{
	push((s+e)/2,s,e,in);
}

int main()
{
	scanf ("%d %d %d",&N,&K,&Q);
	for (int i=0;i<N;i++){
		int x,t,a,b; scanf ("%d %d %d %d",&x,&t,&a,&b);
		x *= 2;
		query[a].first.push_back({x,t});
		query[b+1].first.push_back({x,-t});
	}
	for (int i=0;i<Q;i++){
		int l,y; scanf ("%d %d",&l,&y);
		l *= 2;
		query[y].second.push_back({l,i});
	}

	for (int i=1;i<=K;i++){
		make[i][-non] = make[i][non] = 1;
	}
	XP[C++] = -non;
	XP[C++] = non;
	XP[C++] = 0;

	for (auto &p : query){
		auto &v = p.second.first;
		for (auto &q : v){
			int x = q.first;
			if (q.second > 0){
				int t = q.second;
				int &c = make[t][x];
				if (c == 0){
					auto I = make[t].find(x);
					auto J = I, K = I; J--; K++;
					XP[C++] = (J->first + I->first) / 2;
					XP[C++] = (I->first + K->first) / 2;
				}
				c++;
			}
			else{
				int t = -q.second;
				int &c = make[t][x];
				if (c == 1){
					auto I = make[t].find(x);
					auto J = I, K = I; J--; K++;
					XP[C++] = (J->first + K->first) / 2;
					make[t].erase(I);
				}
				else c--;
			}
		}
	}

	sort(XP,XP+C);
	C = unique(XP,XP+C) - XP;

	int v = lower_bound(XP,XP+C,0) - XP;
	for (int i=1;i<K;i++){
		upc[v].insert(-non);
		downc[v].insert(non);
	}
	mt(-non,non,true);

	for (int i=1;i<Z*2;i++) up[i] = non, down[i] = -non;

	int have = 0;
	for (auto &p : query){
		auto &v = p.second.first;
		for (auto &q : v){
			int x = q.first;
			if (q.second > 0){
				int t = q.second;
				if (make[t].size() == 2) have++;
				int &c = make[t][x];
				if (c == 0){
					auto I = make[t].find(x);
					auto J = I, K = I; J--; K++;
					mt(J->first,K->first,false);
					mt(J->first,I->first,true);
					mt(I->first,K->first,true);
				}
				c++;
			}
			else{
				int t = -q.second;
				int &c = make[t][x];
				if (c == 1){
					auto I = make[t].find(x);
					auto J = I, K = I; J--; K++;
					mt(J->first,I->first,false);
					mt(I->first,K->first,false);
					mt(J->first,K->first,true);
					make[t].erase(I);
					if (make[t].size() == 2) have--;
				}
				else c--;
			}
		}
		auto &w = p.second.second;
		for (auto &q : w){
			ans[q.second] = have == K ? out(q.first) / 2 : -1;
		}
	}

	for (int i=0;i<Q;i++){
		printf ("%d\n",ans[i]);
	}

	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

new_home.cpp: In function 'int main()':
new_home.cpp:86:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf ("%d %d %d",&N,&K,&Q);
  ~~~~~~^~~~~~~~~~~~~~~~~~~~~
new_home.cpp:88:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x,t,a,b; scanf ("%d %d %d %d",&x,&t,&a,&b);
                ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:94:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int l,y; scanf ("%d %d",&l,&y);
            ~~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...