Submission #1013647

# Submission time Handle Problem Language Result Execution time Memory
1013647 2024-07-03T18:24:02 Z Ivo_12 Fish (IOI08_fish) C++
100 / 100
608 ms 33076 KB
#include <bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define pii pair < int, int >

using namespace std;

const int N = 5e5+10, K = 5e5+10;
const int logN = 20, maxN = (1<<20), startpos = maxN-1;
int n, k, m;
pii ribe[N];
int najvecidrag[K];
int draginterval[K];
int cur;
int lowout[K];
int st[maxN<<1];
vector < int > dragulji;
int sol;
bool rip[K];
//bool ripp[7010][7010];
int temp;
int newcol[K];
int granice[K];

bool dragsort1( int a, int b ) {
	if(najvecidrag[a] > najvecidrag[b]) return 1;
	return 0;
}

bool dragsort2( int a, int b ) {
	if(lowout[a] > lowout[b]) return 1;
	return 0;
}

int add( int a, int b ) {
	return (a + b >= m) ? a+b-m : a+b;
}

int mul( int a, int b ) {
	return (ll) a * b % m;
}

inline void merge( int i ) {
	st[i] = mul(st[i*2+1], st[i*2+2]);
	return;
}

inline void update( int i, int v ) {
	
	i += startpos;
	st[i] += v;
	while(i != 0) {
		i = (i - 1) / 2;
		merge(i);
	}
	return;
	
}

int product( int i, int l, int r, int lo, int hi ) {
	if(l>=lo && r<=hi) return st[i];
	if(r<=lo || l>=hi) return 1;
	return mul(product(i*2+1, l, l + (r - l) / 2, lo, hi), product(i*2+2, l + (r - l) / 2, r, lo, hi));
}

int main( void ) {
	
	FIO
	cin >> n >> k >> m;
	for(int i = 0; i < n; i++) {
		cin >> ribe[i].F >> ribe[i].S;
		ribe[i].S--;
	}
	sort(ribe, ribe + n);
	cur = 0;
	for(int i = 0; i < n; i++) {
		najvecidrag[ribe[i].S] = i;
	}
	for(int i = 0; i < k; i++) dragulji.pb(i);
	sort(dragulji.begin(), dragulji.end(), dragsort1);
	reverse(dragulji.begin(), dragulji.end());
	for(int i = 0; i < k; i++) {
		newcol[dragulji[i]] = i;
	}
	dragulji.clear();
	for(int i = 0; i < n; i++) {
		ribe[i].S = newcol[ribe[i].S];
	}
	for(int i = 0; i < n; i++) {
		najvecidrag[ribe[i].S] = i;
		while(ribe[cur].F * 2 <= ribe[i].F) {
			cur++;
		}
		draginterval[ribe[i].S] = cur;
		lowout[ribe[i].S] = n;
	}
	for(int i = 0; i < n; i++) {
		if(i >= draginterval[ribe[i].S]) {
			lowout[ribe[i].S] = min(lowout[ribe[i].S], i);
		}
	}
	for(int i = 0; i < maxN<<1; i++) st[i] = 1;
	for(int i = 0; i < n; i++) {
		update(ribe[i].S, 1);
	}
	for(int i = 0; i < k; i++) {
		dragulji.pb(i);
	}
	sort(dragulji.begin(), dragulji.end(), dragsort1);
	cur = n-1;
	for(int i = 0; i < k; i++) {
		while(cur >= 0 && draginterval[dragulji[i]] <= cur) {
			if(!rip[ribe[cur].S])update(ribe[cur].S, -1);
			cur--;
		}
//		cout << st[startpos+0] << " " << st[startpos+1] << " " << st[startpos+2] << " " << st[startpos+3] << "\n";
		update(dragulji[i], -1);
		sol = add(sol, st[0]);
		update(dragulji[i], 1-st[dragulji[i] + startpos]);
		rip[dragulji[i]] = 1;
//		cout << st[startpos+0] << " " << st[startpos+1] << " " << st[startpos+2] << " " << st[startpos+3] << "\n\n";
	}
	for(int i = 0; i < k; i++) rip[i] = 0;
//	cout << sol << "\n";
	cur = 0;
	for(int i = 0; i < k; i++) granice[i] = k;
	sort(dragulji.begin(), dragulji.end(), dragsort2);
	reverse(dragulji.begin(), dragulji.end());
	for(int i = 0; i < k; i++) {
		while(lowout[dragulji[cur]] < draginterval[i]) {
			granice[dragulji[cur]] = i;
			cur++;
		}
	}
	cur = n-1;
	sort(dragulji.begin(), dragulji.end(), dragsort1);
	for(int i = 0; i < maxN<<1; i++) st[i] = 1;
	for(int i = 0; i < n; i++) {
		update(ribe[i].S, 1);
	}
//
	for(int i = 0; i < k; i++) {
		while(cur >= 0 && draginterval[dragulji[i]] <= cur) {
			update(ribe[cur].S, -1);
			cur--;
		}
		temp = 1 - st[dragulji[i] + startpos];
		update(dragulji[i], temp);
		sol = add(sol, product(0, 0, maxN, 0, granice[dragulji[i]]));
//		cout << product(0, 0, maxN, 0, granice[i]) << "\n";
		update(dragulji[i], -temp);
	}
//	cout << "\n";
//	for(int i = 0; i < n; i++) cout << ribe[i].F << " " << ribe[i].S << "\n";
//
	
	cout << sol;
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16856 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16728 KB Output is correct
2 Correct 285 ms 26560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 16744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 21328 KB Output is correct
2 Correct 148 ms 21844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 16728 KB Output is correct
2 Correct 7 ms 16988 KB Output is correct
3 Correct 6 ms 16732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 133 ms 23124 KB Output is correct
2 Correct 261 ms 27216 KB Output is correct
3 Correct 237 ms 27216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 197 ms 26708 KB Output is correct
2 Correct 230 ms 27476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 151 ms 23376 KB Output is correct
2 Correct 226 ms 27480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 215 ms 26712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 256 ms 28100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 222 ms 26544 KB Output is correct
2 Correct 371 ms 30932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 380 ms 30056 KB Output is correct
2 Correct 323 ms 26576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 326 ms 28108 KB Output is correct
2 Correct 375 ms 30928 KB Output is correct
3 Correct 380 ms 30276 KB Output is correct
4 Correct 349 ms 31048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 438 ms 31696 KB Output is correct
2 Correct 595 ms 31940 KB Output is correct
3 Correct 585 ms 33076 KB Output is correct
4 Correct 608 ms 32712 KB Output is correct