Submission #548529

# Submission time Handle Problem Language Result Execution time Memory
548529 2022-04-13T17:49:44 Z NachoLibre Fish (IOI08_fish) C++17
88 / 100
941 ms 65536 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;

const int F = 500005, K = F;
int f, k, mo, z, c[K];
array<int, 2> a[F];
vector<int> s[K];
struct oo {
	oo() {
		l = r = 0;
		x = 1;
	}
	oo *l, *r;
	int x;

	void P() {
		int lx = (!l ? 1 : l->x);
		int rx = (!r ? 1 : r->x);
		x = lx * rx % mo;
	}
} *root;

void upd(int i, int x, int l = 1, int r = k, oo *&t = root) {
	if(!t) t = new oo();
	if(l == r) {
		t->x = x;
		return;
	}
	int m = l + r >> 1;
	if(m >= i) upd(i, x, l, m, t->l);
	else upd(i, x, m + 1, r, t->r);
	t->P();
}

void updc(int g) {
	upd(g, c[g] + 1);
}

int x(int sl, int sr, int l = 1, int r = k, oo *&t = root) {
	if(l > sr || r < sl || !t) return 1;
	if(l >= sl && r <= sr) return t->x;
	int m = l + r >> 1;
	return x(sl, sr, l, m, t->l) * x(sl, sr, m + 1, r, t->r) % mo;
}

void moz(int i) {
	while(a[z][0] * 2 > a[i][0]) {
		c[a[z][1]] -= 1;
		updc(a[z][1]);
		z -= 1;
	}
	while(a[z + 1][0] * 2 <= a[i][0]) {
		z += 1;
		c[a[z][1]] += 1;
		updc(a[z][1]);
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	#ifdef x
	freopen("x.txt", "r", stdin);
	#endif
	cin >> f >> k >> mo;
	for(int i = 1; i <= f; ++i) {
		cin >> a[i][0] >> a[i][1];
	}
	a[f + 1][0] = 2e9;
	a[f + 1][1] = k + 1;
	sort(a + 1, a + f + 1);
	vector<int> smxf(f + 2, 0);
	smxf[f + 1] = k + 1;
	{
		vector<int> fg(k + 1, 0);
		int kg = k;
		for(int i = f; i >= 1; --i) {
			if(!fg[a[i][1]]) {
				fg[a[i][1]] = kg;
				kg -= 1;
			}
		}
		for(int i = 1; i <= f; ++i) {
			a[i][1] = fg[a[i][1]];
			s[a[i][1]].push_back(i);
		}
		vector<bool> fasd(f + 1, 0);
		for(int i = f; i >= 1; --i) {
			smxf[i] = smxf[i + 1];
			if(!fasd[a[i][1]]) {
				fasd[a[i][1]] = 1;
				smxf[i] = a[i][1];
			}
		}
	}
	moz(f);
	int fp = x(1, k);
	vector<bool> xm(k + 1);
	xm[k] = 1;
	for(int i = f - 1; i >= 1; --i) {
		if(!xm[a[i][1]]) {
			xm[a[i][1]] = 1;
			moz(i);
			upd(a[i][1], c[a[i][1]]);
			(fp += x(1, a[i][1])) %= mo;
			updc(a[i][1]);
			int y = *upper_bound(all(s[a[i][1]]), z);
			int l = y + 1, r = f + 1;
			while(l < r) {
				int m = l + r >> 1;
				if(a[y][0] * 2 <= a[m][0]) r = m;
				else l = m + 1;
			}
			int ff = smxf[l];
			fp += x(1, a[i][1] - 1) * x(a[i][1] + 1, ff - 1) % mo;
			// cout << x(1, a[i][1] - 1) << endl;
			// cout << x(a[i][1] + 1, ff - 1) << endl;
			fp %= mo;
		}
	}
	cout << fp << endl;
	return 0;
}

Compilation message

fish.cpp: In function 'void upd(int, int, int, int, oo*&)':
fish.cpp:33:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   33 |  int m = l + r >> 1;
      |          ~~^~~
fish.cpp: In function 'int x(int, int, int, int, oo*&)':
fish.cpp:46:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   46 |  int m = l + r >> 1;
      |          ~~^~~
fish.cpp: In function 'int main()':
fish.cpp:114:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  114 |     int m = l + r >> 1;
      |             ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 11988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 166 ms 20524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 12160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 15788 KB Output is correct
2 Correct 107 ms 16544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12244 KB Output is correct
2 Correct 10 ms 12244 KB Output is correct
3 Correct 10 ms 12244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 133 ms 17596 KB Output is correct
2 Incorrect 242 ms 20376 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 277 ms 20800 KB Output is correct
2 Correct 205 ms 21100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 124 ms 17728 KB Output is correct
2 Correct 238 ms 20948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 222 ms 20712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 261 ms 22260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 236 ms 22412 KB Output is correct
2 Correct 310 ms 28604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 710 ms 32284 KB Output is correct
2 Correct 517 ms 30744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 528 ms 33440 KB Output is correct
2 Correct 678 ms 28908 KB Output is correct
3 Correct 606 ms 45228 KB Output is correct
4 Correct 679 ms 36676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 941 ms 45704 KB Output is correct
2 Runtime error 327 ms 65536 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -