Submission #391517

#TimeUsernameProblemLanguageResultExecution timeMemory
391517patrikpavic2Event Hopping 2 (JOI21_event2)C++17
100 / 100
377 ms30392 KiB
#include <cstdio>
#include <cstring>
#include <set>
#include <vector>
#include <algorithm>

#define PB push_back
#define X first
#define Y second

using namespace std;

const int N = 2e5 + 500;
const int LOG = 18; 
const int OFF = (1 << 18);

vector < int > saz;

int n, L[N], R[N], k;
int par[N][LOG];
vector < int > sol;
int T[2 * OFF], prop[2 * OFF];
set < int > S;

void refresh(int x){
	if(!prop[x]) return;
	T[x] += prop[x];
	if(x < OFF){
		prop[2 * x] += prop[x];
		prop[2 * x + 1] += prop[x];
	}
	prop[x] = 0;
}

void update(int i, int a, int b, int lo, int hi, int x){
	refresh(i);
	if(lo <= a && b <= hi){
		prop[i] += x; refresh(i);
		return;
	}
	if(a > hi || b < lo) return;
	update(2 * i, a, (a + b) / 2, lo, hi, x);
	update(2 * i + 1, (a + b) / 2 + 1, b, lo, hi, x);
	T[i] = max(T[2 * i], T[2 * i + 1]);
}

int query(int i, int a, int b, int lo, int hi){
	refresh(i);
	if(lo <= a && b <= hi) return T[i];
	if(a > hi || b < lo) return 0;
	return max(query(2 * i, a, (a + b) / 2, lo, hi), query(2 * i + 1, (a + b) / 2 + 1, b, lo, hi));
}

int query(int l, int r){
	int ans = 0;
	for(int k = LOG - 1;k >= 0;k--)
		if(par[l][k] <= r)
			l = par[l][k], ans += (1 << k);
	return ans;
}

int main(){
	scanf("%d%d", &n, &k);
	for(int i = 0;i < n;i++){
		scanf("%d%d", L + i, R + i);
		saz.PB(L[i]); saz.PB(R[i]);
	}
	for(int i = 0;i < N;i++) par[i][0] = N - 1;
	sort(saz.begin(), saz.end());
	saz.erase(unique(saz.begin(), saz.end()), saz.end());
	for(int i = 0;i < n;i++){
		L[i] = lower_bound(saz.begin(), saz.end(), L[i]) - saz.begin();
		R[i] = lower_bound(saz.begin(), saz.end(), R[i]) - saz.begin();
		par[L[i]][0] = min(par[L[i]][0], R[i]);
	}
	for(int i = N - 2;i >= 0;i--)
		par[i][0] = min(par[i][0], par[i + 1][0]);
	for(int j = 1;j < LOG;j++)
		for(int i = 0;i < N;i++)
			par[i][j] = par[par[i][j - 1]][j - 1];
	int sve = query(0, N - 2);
	//printf("sve = %d\n", sve);
	S.insert(0), S.insert(N - 2);
	for(int i = 0;i < n;i++){
		if((int)sol.size() == k) break;
		if(query(1, 0, OFF - 1, L[i], R[i] - 1)) continue;
		int LL = *(--S.upper_bound(L[i]));
		int RR = *S.lower_bound(R[i]);
	//	printf("i = %d\n", i);
		//printf("sve = %d (LL, RR) = %d (LL, L) = %d (R, RR) = %d\n", sve, query(LL, RR), query(LL, L[i]), query(R[i], RR));
		if(sve - query(LL, RR) + query(LL, L[i]) + query(R[i], RR) + 1 >= k){
			sol.PB(i); S.insert(L[i]); S.insert(R[i]);
			sve += query(LL, L[i]) + query(R[i], RR) - query(LL, RR) + 1;
			update(1, 0, OFF - 1, L[i], R[i] - 1, 1);
		}
	}
	//printf("%d\n", (int)sol.size());
	if((int)sol.size() < k)
		printf("-1\n");
	else{
		for(int x : sol)
			printf("%d\n", x + 1);
	}
	return 0;
}


Compilation message (stderr)

event2.cpp: In function 'int main()':
event2.cpp:63:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   63 |  scanf("%d%d", &n, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~
event2.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   65 |   scanf("%d%d", L + i, R + i);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...