Submission #1000359

# Submission time Handle Problem Language Result Execution time Memory
1000359 2024-06-17T10:18:24 Z jer033 Horses (IOI15_horses) C++17
100 / 100
874 ms 29088 KB
#include "horses.h"
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const ll MOD = 1'000'000'007;
const ll INF = 1'000'000'006;
const int pus = 524288;
 
ll regmul(ll a, ll b)
{
	return (a*b)%MOD;
}
 
ll boundmul(ll a, ll b)
{
	return min(INF, a*b);
}
 
ll modpow(ll x, int y)
{
	if (y==0)
		return 1;
	if (y==1)
		return x;
	ll z = modpow(x, y/2);
	z = regmul(z, z);
	if (y%2)
		z = regmul(z, x);
	return z;
}
 
ll modinv(ll a, ll b)
{
	return regmul(a, modpow(b, MOD-2));
}
 
vector<int> x;
vector<int> y;
int n;
 
int segtree[1048576];
 
void setupsegtree()
{
	for (int i=0; i<n; i++)
	{
		segtree[pus+i] = y[i];
	}
	for (int i=pus-1; i>=1; i--)
	{
		segtree[i] = max(segtree[2*i], segtree[2*i+1]);
	}
}
 
void updatesegtree(int index, int newval)
{
	index = pus+index;
	segtree[index] = newval;
	while (index!=0)
	{
		index = index/2;
		segtree[index] = max(segtree[2*index], segtree[2*index+1]);
	}
	segtree[0] = 0;
}
 
int rangequery(int indexa, int indexb)
{
	indexa = indexa+pus;
	indexb = indexb+pus;
	int ans = -1;
	while ((indexb-indexa)>=5)
	{
		if ((indexa%2)==1)
		{
			ans = max(segtree[indexa], ans);
			indexa++;
		}
		if ((indexb%2)==0)
		{
			ans = max(segtree[indexb], ans);
			indexb--;
		}
		indexa = indexa/2;
		indexb = indexb/2;
	}
	for (int i=indexa; i<=indexb; i++)
		ans = max(segtree[i], ans);
	return ans;
}
 
priority_queue<int> nonone;
vector<int> candidates;
vector<int> mybes;
ll c;
bool acand[pus];
 
void initpq(bool first)
{
	if (first)
	{
		c = 1;
		for (int i=0; i<n; i++)
		{
			if (x[i]>1)
			{
				nonone.push(i);
				c = regmul(c, x[i]);
			}
		}
	}
	else
	{
		mybes.clear();
		int m = candidates.size();
		for (int i=0; i<m; i++)
		{
			c = regmul(c, x[candidates[i]]);
			nonone.push(candidates[i]);
            acand[candidates[i]] = 0;
		}
		candidates.clear();
	}
	int j = 0;
	while ((j<30) and (not nonone.empty()))
	{
		int q = nonone.top();
		if ((x[q]>1) and (acand[q]==0))
		{
            acand[q] = 1;
			candidates.push_back(q);
			c = modinv(c, x[q]);
			j++;
		}
		nonone.pop();
	}
	//reverse the list
	vector<int> newcandidates;
	for (int i=j-1; i>=0; i--)
	{
		newcandidates.push_back(candidates[i]);
	}
	candidates = newcandidates;
 
	for (int i=0; i<j; i++)
	{
		if (i!=(j-1))
		{
			mybes.push_back(rangequery(candidates[i], candidates[i+1]-1));
		}
		else
		{
			mybes.push_back(rangequery(candidates[i], n-1));
		}
	}
}
 
ll solve()
{
	int m = candidates.size();
	if (m==0)
		return rangequery(0, n-1);
	ll safe = x[candidates[0]];
	ll bes = mybes[0];
	ll currmul = 1;
	for (int i=1; i<m; i++)
	{
        //cout << candidates[i] << '\n';
		if (boundmul(boundmul(currmul, x[candidates[i]]), mybes[i])>=bes)
		{
			safe = regmul(safe, currmul);
			safe = regmul(safe, x[candidates[i]]);
			currmul = 1;
			bes = mybes[i];
		}
		else
		{
			currmul = boundmul(currmul, x[candidates[i]]);
		}
	}
	ll doubans = regmul(c, regmul(safe, bes));
	return modinv(doubans, 2);
}
 
int init(int N, int X[], int Y[]) {
	x.push_back(2);
	y.push_back(1);
	for (int i=0; i<N; i++)
	{
		x.push_back(X[i]);
		y.push_back(Y[i]);
	}
	n=N+1;
	setupsegtree();
    initpq(1);
	return solve();
}
 
int updateX(int pos, int val) {	
	pos++;
	ll oldval = x[pos];
	x[pos] = val;
	int m = candidates.size();
	//Update the ff:
	//pq nonone
	//vector candidates
	//vector mybes
	//ll c

	//Take into account:
	//Is pos >= candidates[0] ?
	//Is val 1?
	//Is candidates full?
	if (val!=1)
	{
		nonone.push(pos);
	}
    if ((pos<candidates[0]) or ((val!=1) and (oldval==1)))
	    c = regmul(modinv(c, oldval), val);

	initpq(0);

	return solve();
}
 
int updateY(int pos, int val) {
	pos++;
	y[pos] = val;
	updatesegtree(pos, val);
	int j = candidates.size();
	for (int i=0; i<j; i++)
	{
		if (i!=(j-1))
		{
			if ((candidates[i]<=pos) and (pos<candidates[i+1]))
				mybes[i] = rangequery(candidates[i], candidates[i+1]-1);
		}
		else
		{
			if (candidates[i]<=pos)
				mybes[i] = rangequery(candidates[i], n-1);
		}
	}
 
	return solve();
}

Compilation message

horses.cpp: In function 'void initpq(bool)':
horses.cpp:115:26: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  115 |   int m = candidates.size();
      |           ~~~~~~~~~~~~~~~^~
horses.cpp: In function 'll solve()':
horses.cpp:160:25: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  160 |  int m = candidates.size();
      |          ~~~~~~~~~~~~~~~^~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:196:14: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  196 |  return solve();
      |         ~~~~~^~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:203:25: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  203 |  int m = candidates.size();
      |          ~~~~~~~~~~~~~~~^~
horses.cpp:223:14: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  223 |  return solve();
      |         ~~~~~^~
horses.cpp:203:6: warning: unused variable 'm' [-Wunused-variable]
  203 |  int m = candidates.size();
      |      ^
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:230:25: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  230 |  int j = candidates.size();
      |          ~~~~~~~~~~~~~~~^~
horses.cpp:245:14: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  245 |  return solve();
      |         ~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2472 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 2 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 2 ms 2476 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 2 ms 2396 KB Output is correct
16 Correct 2 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2500 KB Output is correct
20 Correct 2 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 2 ms 2396 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 2 ms 2396 KB Output is correct
12 Correct 2 ms 2312 KB Output is correct
13 Correct 2 ms 2396 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 1 ms 2400 KB Output is correct
16 Correct 2 ms 2396 KB Output is correct
17 Correct 2 ms 2396 KB Output is correct
18 Correct 2 ms 2396 KB Output is correct
19 Correct 2 ms 2396 KB Output is correct
20 Correct 2 ms 2396 KB Output is correct
21 Correct 2 ms 2396 KB Output is correct
22 Correct 2 ms 2396 KB Output is correct
23 Correct 5 ms 2900 KB Output is correct
24 Correct 5 ms 2568 KB Output is correct
25 Correct 5 ms 2904 KB Output is correct
26 Correct 6 ms 2532 KB Output is correct
27 Correct 6 ms 2396 KB Output is correct
28 Correct 9 ms 2548 KB Output is correct
29 Correct 3 ms 2392 KB Output is correct
30 Correct 9 ms 2908 KB Output is correct
31 Correct 2 ms 2396 KB Output is correct
32 Correct 8 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 18952 KB Output is correct
2 Correct 535 ms 29088 KB Output is correct
3 Correct 490 ms 20220 KB Output is correct
4 Correct 870 ms 23724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 2 ms 2392 KB Output is correct
4 Correct 3 ms 2396 KB Output is correct
5 Correct 2 ms 2552 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 2 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 2 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 2 ms 2396 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 2 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 2 ms 2396 KB Output is correct
23 Correct 6 ms 2396 KB Output is correct
24 Correct 5 ms 2396 KB Output is correct
25 Correct 5 ms 2540 KB Output is correct
26 Correct 6 ms 2396 KB Output is correct
27 Correct 6 ms 2652 KB Output is correct
28 Correct 9 ms 2548 KB Output is correct
29 Correct 3 ms 2396 KB Output is correct
30 Correct 9 ms 2452 KB Output is correct
31 Correct 2 ms 2460 KB Output is correct
32 Correct 5 ms 2396 KB Output is correct
33 Correct 67 ms 17084 KB Output is correct
34 Correct 66 ms 16824 KB Output is correct
35 Correct 103 ms 26496 KB Output is correct
36 Correct 104 ms 26344 KB Output is correct
37 Correct 56 ms 14964 KB Output is correct
38 Correct 106 ms 17588 KB Output is correct
39 Correct 26 ms 14776 KB Output is correct
40 Correct 123 ms 21432 KB Output is correct
41 Correct 18 ms 14772 KB Output is correct
42 Correct 52 ms 14784 KB Output is correct
43 Correct 55 ms 21944 KB Output is correct
44 Correct 51 ms 21844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 2 ms 2392 KB Output is correct
3 Correct 2 ms 2396 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 2 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 2 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 2 ms 2396 KB Output is correct
23 Correct 6 ms 2396 KB Output is correct
24 Correct 5 ms 2396 KB Output is correct
25 Correct 6 ms 2768 KB Output is correct
26 Correct 6 ms 2572 KB Output is correct
27 Correct 5 ms 2392 KB Output is correct
28 Correct 9 ms 2560 KB Output is correct
29 Correct 3 ms 2392 KB Output is correct
30 Correct 9 ms 2396 KB Output is correct
31 Correct 2 ms 2396 KB Output is correct
32 Correct 5 ms 2552 KB Output is correct
33 Correct 73 ms 18980 KB Output is correct
34 Correct 513 ms 28776 KB Output is correct
35 Correct 477 ms 19896 KB Output is correct
36 Correct 871 ms 23468 KB Output is correct
37 Correct 66 ms 16568 KB Output is correct
38 Correct 66 ms 16288 KB Output is correct
39 Correct 100 ms 26056 KB Output is correct
40 Correct 109 ms 26044 KB Output is correct
41 Correct 55 ms 15032 KB Output is correct
42 Correct 119 ms 17656 KB Output is correct
43 Correct 27 ms 14724 KB Output is correct
44 Correct 123 ms 21604 KB Output is correct
45 Correct 18 ms 14780 KB Output is correct
46 Correct 58 ms 14776 KB Output is correct
47 Correct 51 ms 21860 KB Output is correct
48 Correct 51 ms 21888 KB Output is correct
49 Correct 457 ms 19124 KB Output is correct
50 Correct 450 ms 19032 KB Output is correct
51 Correct 497 ms 27444 KB Output is correct
52 Correct 498 ms 28980 KB Output is correct
53 Correct 458 ms 17104 KB Output is correct
54 Correct 867 ms 19868 KB Output is correct
55 Correct 136 ms 15800 KB Output is correct
56 Correct 874 ms 22428 KB Output is correct
57 Correct 62 ms 16568 KB Output is correct
58 Correct 383 ms 17104 KB Output is correct
59 Correct 52 ms 21916 KB Output is correct