Submission #254961

# Submission time Handle Problem Language Result Execution time Memory
254961 2020-07-30T22:23:53 Z amoo_safar New Home (APIO18_new_home) C++14
0 / 100
237 ms 271868 KB
// Zende bad Shoma nasime faghat !
#include <bits/stdc++.h>
/*
#pragma GCC target ("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef pair<int, int> pii;
typedef pair<pii, int> QT;

const int N = 3e5 + 10;
const int Log = 19;

int n, q, k;
int x[N], t[N], a[N], b[N];
int y[N], c[N], ans[N], cn[N], AllT;

vector<int> V;

vector<QT> E[N << 2];
vector<pii> Ask[N];

void Add(int id, int l, int r, QT z, int L, int R){
	if(r <= L || R <= l) return ;
	if(l <= L && R <= r){
		E[id].pb(z);
		return ;
	}
	int mid = (L + R) >> 1;
	Add(id << 1, l, r, z, L, mid);
	Add(id << 1 | 1, l, r, z, mid, R);
}

map<int, int> mp[N];
set<int> rec[N];

vector<pii> seg[N << 2];

void Ins(int id, int l, int r, int val, int L, int R){
	if(r <= L || R <= l) return ;
	if(l <= L && R <= r){
		if(seg[id].empty()){
			seg[id].pb({val, val});
		} else {
			seg[id].pb({min(seg[id].back().F, val), max(seg[id].back().S, val)});
		}
		return ;
	}
	int mid = (L + R) >> 1;
	Ins(id << 1, l, r, val, L, mid);
	Ins(id << 1 | 1, l, r, val, mid, R);
}
void Undo(int id, int l, int r, int L, int R){
	if(r <= L || R <= l) return ;
	if(l <= L && R <= r){
		assert(!seg[id].empty());
		seg[id].pop_back();
		//seg[id].erase(seg[id].find(x));
		return ;
	}
	int mid = (L + R) >> 1;
	Undo(id << 1, l, r, L, mid);
	Undo(id << 1 | 1, l, r, mid, R);
}

int MN, MX, CNT, QN;
void Get(int id, int idx, int L, int R){
	
	CNT += seg[id].size();
	if(!seg[id].empty()){
		MN = min(MN, seg[id].back().F);
		MX = max(MX, seg[id].back().S);
	}
	
	if(R - L == 1) return ;
	int mid = (L + R) >> 1;
	if(idx < mid) Get(id << 1, idx, L, mid);
	else Get(id << 1 | 1, idx, mid, R);
}

void DFS(int id, int L, int R){
	for(auto &z : E[id]){
		//cerr << "# " << z.F.F << ' ' << z.F.S << ' ' << z.S << '\n';
		Ins(1, z.F.F, z.F.S, z.S, 0, V.size());
	}

	//cerr << "# " << id << ' ' << L << ' ' << R << '\n';
	if(R - L == 1){
		for(auto que : Ask[L]){
			MN = 1e8 + 1;
			CNT = 0;
			MX = -1;
			Get(1, que.F, 0, V.size());
			//debug(CNT);
			ans[que.S] = (CNT == k ? max(MX - y[que.S], y[que.S] - MN) : -1);
		}
	} else {
		int mid = (L + R) >> 1;
		DFS(id << 1, L, mid);
		DFS(id << 1 | 1, mid, R);
	}

	for(auto &z : E[id]){
		Undo(1, z.F.F, z.F.S, 0, V.size());
	}
}


int Timer = 0;
map<pair<QT, int>, int> last;

template<typename T>
T Prev(T A){return --A; };
template<typename T>
T Next(T A){return ++A; };

inline pii GetRange(pii R){
	return pii(lower_bound(all(V), R.F) - V.begin(), upper_bound(all(V), R.S) - V.begin());
}
inline pii GetRange(int tp, int pl){
	set<int>::iterator it = rec[tp].find(pl);
	int resL, resR;

	if(it == rec[tp].begin()) resL = 1;
	else resL = ( (*Prev(it)) + (*it)) / 2 + 1;

	if(Next(it) == rec[tp].end()) resR = 1e8;
	else resR = ( (*Next(it)) + (*it)) / 2;
	return pii(resL, resR);
}
/*
int SegGet(int qn){
	QN = qn;
	int pl = y[qn];
	MN = 1e8 + 1;
	MX = 0;
	CNT = 0;
	Get(1, lower_bound(all(V), pl) - V.begin(), 0, V.size());
	if(CNT != k) return -1;
	return max(MX - pl, pl - MN);
}
*/
void SegRem(int tp, int pl){
	pii ran = GetRange(GetRange(tp, pl));
	QT que = QT(ran, pl);
	assert(last.count({que, tp}) == 1);
	Add(1, last[{{ran, pl}, tp}], Timer, QT(ran, pl), 0, AllT);
	last.erase({que, tp});
	//Rem(1, ran.F, ran.S, pl, 0, V.size());
}
void SegAdd(int tp, int pl){
	pii ran = GetRange(GetRange(tp, pl));
	assert(last.count({{ran, pl}, tp}) == 0);
	last[{{ran, pl}, tp}] = Timer;
	//Ins(1, ran.F, ran.S, pl, 0, V.size());
}
void Add(int tp, int pl){
	//cerr << "+ " << tp << ' ' << pl << '\n';
	mp[tp][pl] ++;
	if(mp[tp][pl] > 1) return ;

	auto it = rec[tp].lower_bound(pl);
	int nx = -1, la = -1;
	if(it != rec[tp].end()) nx = *it;
	if(it != rec[tp].begin()) la = *Prev(it);
	if(nx != -1) SegRem(tp, nx);
	if(la != -1) SegRem(tp, la);

	rec[tp].insert(pl);
	if(nx != -1) SegAdd(tp, nx);
	if(la != -1) SegAdd(tp, la);
	SegAdd(tp, pl);
}
void Erase(int tp, int pl){
	//cerr << "- " << tp << ' ' << pl << '\n';
	mp[tp][pl] --;
	if(mp[tp][pl] > 0) return ;

	auto it = rec[tp].find(pl);
	int nx = -1, la = -1;
	
	if(Next(it) != rec[tp].end()) nx = *Next(it);
	if(it != rec[tp].begin()) la = *Prev(it);
	
	if(nx != -1) SegRem(tp, nx);
	if(la != -1) SegRem(tp, la);
	SegRem(tp, pl);

	rec[tp].erase(pl);
	
	if(nx != -1) SegAdd(tp, nx);
	if(la != -1) SegAdd(tp, la);
}

vector<int> Ea[N*2], Er[N*2], Eq[N*2];

/*
int fen[N];
void AddFen(int idx, int d){
	for(; idx < N; idx += (idx & -idx))
		fen[idx] += d;
}
int GetFen(int idx){
	int res = 0;
	for(; idx; idx -= (idx & -idx))
		res += fen[idx];
	return res;
}
*/


int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	//freopen("Big.txt", "r", stdin);
	//freopen("Out.txt", "w", stdout);
	
	cin >> n >> k >> q;
	assert(n == 20);
	for(int i = 1; i <= n; i++){
		cin >> x[i] >> t[i] >> a[i] >> b[i];
		b[i] ++;
		//V.pb(a[i]); V.pb(b[i]);
	}
	for(int i = 1; i <= q; i++){
		cin >> y[i] >> c[i];
		V.pb(c[i]);
	}

	sort(all(V)); V.resize(unique(all(V)) - V.begin());
	V.pb(1e8 + 3);
	AllT = V.size();
	assert(V.size() < N);
	for(int i = 1; i <= n; i++){
		a[i] = lower_bound(all(V), a[i]) - V.begin();
		b[i] = lower_bound(all(V), b[i]) - V.begin();
		Ea[a[i]].pb(i);
		Er[b[i]].pb(i);
		//cerr << "!!! " << a[i] << ' ' << b[i] << '\n';
	}
	for(int i = 1; i <= q; i++){
		c[i] = lower_bound(all(V), c[i]) - V.begin();
		Eq[c[i]].pb(i);
		//cerr << "? " << c[i] << '\n';
	}
	//cerr << "WTF! \n";
	V.clear();
	//for(int i = 1; i <= n; i++) V.pb(x[i]);
	for(int i = 1; i <= q; i++) V.pb(y[i]);
	sort(all(V)); V.resize(unique(all(V)) - V.begin());
	
	//if(n > 20) assert(false);
	for(int tm = 0; tm < AllT; tm++){
		//if(tm % 1000 == 0)cerr << tm << '\n';
		Timer = tm;
		
		for(auto i : Ea[tm])
			Add(t[i], x[i]);
		for(auto i : Er[tm])
			Erase(t[i], x[i]);
		//}
		for(auto i : Eq[tm])
			Ask[tm].pb({lower_bound(all(V), y[i]) - V.begin(), i});
		//cerr << '\n';

	}
	//cerr << "Done !\n";
	assert(q == 20);
	DFS(1, 0, AllT);
	/*
	V.clear();
	V.pb(-1);
	for(int i = 1; i <= n; i++) V.pb(x[i]);
	//for(int i = 1; i <= q; i++) V.pb(y[i]);
	sort(all(V)); V.resize(unique(all(V)) - V.begin());
	*/
	for(int i = 1; i <= q; i++) cout << ans[i] << '\n';
	return 0;
}
/*
4 2 4
3 1 1 10
9 2 2 4
7 2 5 7
4 1 8 10
5 3
5 6
5 9
1 10

2 1 3
1 1 1 4
1 1 2 6
1 3
1 5
1 7

1 1 1
100000000 1 1 1
1 1

*/
# Verdict Execution time Memory Grader output
1 Correct 77 ms 134264 KB Output is correct
2 Runtime error 217 ms 271868 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 77 ms 134264 KB Output is correct
2 Runtime error 217 ms 271868 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 225 ms 271868 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 237 ms 271736 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 77 ms 134264 KB Output is correct
2 Runtime error 217 ms 271868 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 77 ms 134264 KB Output is correct
2 Runtime error 217 ms 271868 KB Execution killed with signal 11 (could be triggered by violating memory limits)
3 Halted 0 ms 0 KB -