Submission #620863

# Submission time Handle Problem Language Result Execution time Memory
620863 2022-08-03T09:52:18 Z Hazem Horses (IOI15_horses) C++14
100 / 100
871 ms 65484 KB
#include <bits/stdc++.h>
#define LL long long
#define F first
#define S second

using namespace std;

const int M = 5e5+10;
const int MOD = 1e9+7;

LL add(LL a,LL b){
	return (a+b)%MOD;
}

LL mult(LL a,LL b){
	return (a*b)%MOD;
}

LL fastpow(LL n,LL k){

	LL ret = 1;

	while(k){

		if(k&1)ret = mult(ret,n);
		k /= 2;
		n = mult(n,n);
	}
	return ret;
}

LL divide(LL a,LL b){
	return mult(a,fastpow(b,MOD-2));
}

LL a[M],b[M],tree[M*4][2],n;
LL pr = 1;
set<int>st;

void update(int v,int tl,int tr,int pos,int val,int t){

	if(tl==tr){
		tree[v][t] = val;
		return ;
	}

	int mid = (tl+tr)/2;
	if(pos<=mid)
		update(v*2,tl,mid,pos,val,t);
	else 
		update(v*2+1,mid+1,tr,pos,val,t);

	if(!t)
		tree[v][t] = max(tree[v*2][t],tree[v*2+1][t]);
	else 
		tree[v][t] = mult(tree[v*2][t],tree[v*2+1][t]);
}

LL get(int v,int tl,int tr,int l,int r,int t){

	if(tl>r||tr<l)
		return 1;

	if(tl>=l&&tr<=r)
		return tree[v][t];

	int mid = (tl+tr)/2;
	if(!t)
		return max(get(v*2,tl,mid,l,r,t),get(v*2+1,mid+1,tr,l,r,t));
	else 
		return mult(get(v*2,tl,mid,l,r,t),get(v*2+1,mid+1,tr,l,r,t));
}

LL calc(){

	vector<pair<LL,LL>>vec;
	int cnt = 0,last = n+1;
	for(auto it:st){
		if(cnt==30)
			break;
		
		cnt++;
		int idx = -(it);
		if(last-idx>1)
			vec.push_back({1,get(1,1,n,idx+1,last-1,0)});
		
		last = idx;
		vec.push_back({a[idx],b[idx]});
	}
	pr = (last==1)?1:get(1,1,n,1,last-1,1);

	if(vec.empty())
		vec.push_back({1,get(1,1,n,1,n,0)});
	
	reverse(vec.begin(),vec.end());
	LL idx = 0,cur = 1;
	for(int i = 1;i<vec.size();i++){
		
		if((cur*vec[i].F>vec[idx].S)||(cur*vec[i].F*vec[i].S>vec[idx].S))
			idx = i,cur = 1;
		else 
			cur *= vec[i].F;
	}

	cur = 1;
	for(int i=0;i<=idx;i++)
		cur = mult(cur,vec[i].F);
	
	return mult(pr,mult(cur,vec[idx].S));
}

int init(int N, int X[], int Y[]) {
	
	n = N;
	a[0] = 1;
	st.insert(0);
	for(int i=1;i<=n;i++){
		a[i] = X[i-1];
		b[i] = Y[i-1];
		update(1,1,n,i,b[i],0);
		update(1,1,n,i,a[i],1);

		if(a[i]>1)
			st.insert(-i);
	}

	return calc();
}

int updateX(int pos, int val) {	

	if(a[pos+1]>1)
		st.erase(-pos-1);

	a[pos+1] = val;
	update(1,1,n,pos+1,val,1);

	if(a[pos+1]>1)
		st.insert(-pos-1);

	return calc();
}

int updateY(int pos, int val) {
	
	b[pos+1] = val;
	update(1,1,n,pos+1,b[pos+1],0);

	return calc();
}

Compilation message

horses.cpp: In function 'long long int calc()':
horses.cpp:77:22: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   77 |  int cnt = 0,last = n+1;
      |                     ~^~
horses.cpp:85:29: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   85 |    vec.push_back({1,get(1,1,n,idx+1,last-1,0)});
      |                             ^
horses.cpp:90:27: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   90 |  pr = (last==1)?1:get(1,1,n,1,last-1,1);
      |                           ^
horses.cpp:93:28: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   93 |   vec.push_back({1,get(1,1,n,1,n,0)});
      |                            ^
horses.cpp:93:32: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   93 |   vec.push_back({1,get(1,1,n,1,n,0)});
      |                                ^
horses.cpp:97:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |  for(int i = 1;i<vec.size();i++){
      |                ~^~~~~~~~~~~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:120:14: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  120 |   update(1,1,n,i,b[i],0);
      |              ^
horses.cpp:120:21: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  120 |   update(1,1,n,i,b[i],0);
      |                  ~~~^
horses.cpp:121:14: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  121 |   update(1,1,n,i,a[i],1);
      |              ^
horses.cpp:121:21: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  121 |   update(1,1,n,i,a[i],1);
      |                  ~~~^
horses.cpp:127:13: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  127 |  return calc();
      |         ~~~~^~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:136:13: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  136 |  update(1,1,n,pos+1,val,1);
      |             ^
horses.cpp:141:13: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  141 |  return calc();
      |         ~~~~^~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:147:13: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  147 |  update(1,1,n,pos+1,b[pos+1],0);
      |             ^
horses.cpp:147:28: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  147 |  update(1,1,n,pos+1,b[pos+1],0);
      |                     ~~~~~~~^
horses.cpp:149:13: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  149 |  return calc();
      |         ~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 260 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 308 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 304 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 304 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 264 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 304 KB Output is correct
17 Correct 1 ms 304 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 312 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 4 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 448 KB Output is correct
26 Correct 2 ms 468 KB Output is correct
27 Correct 4 ms 340 KB Output is correct
28 Correct 3 ms 444 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 436 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 4 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 374 ms 54352 KB Output is correct
2 Correct 486 ms 53208 KB Output is correct
3 Correct 467 ms 53504 KB Output is correct
4 Correct 532 ms 53740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 292 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 6 ms 340 KB Output is correct
24 Correct 2 ms 320 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 444 KB Output is correct
27 Correct 4 ms 340 KB Output is correct
28 Correct 3 ms 444 KB Output is correct
29 Correct 2 ms 320 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 4 ms 340 KB Output is correct
33 Correct 248 ms 29876 KB Output is correct
34 Correct 187 ms 32560 KB Output is correct
35 Correct 327 ms 62908 KB Output is correct
36 Correct 322 ms 62768 KB Output is correct
37 Correct 256 ms 30656 KB Output is correct
38 Correct 270 ms 43432 KB Output is correct
39 Correct 210 ms 30524 KB Output is correct
40 Correct 305 ms 57864 KB Output is correct
41 Correct 183 ms 30652 KB Output is correct
42 Correct 226 ms 30596 KB Output is correct
43 Correct 313 ms 58312 KB Output is correct
44 Correct 300 ms 58220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 308 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 312 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 304 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 304 KB Output is correct
22 Correct 1 ms 312 KB Output is correct
23 Correct 6 ms 340 KB Output is correct
24 Correct 2 ms 320 KB Output is correct
25 Correct 2 ms 344 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 6 ms 316 KB Output is correct
28 Correct 4 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 4 ms 340 KB Output is correct
33 Correct 354 ms 53708 KB Output is correct
34 Correct 494 ms 65484 KB Output is correct
35 Correct 488 ms 56656 KB Output is correct
36 Correct 555 ms 60728 KB Output is correct
37 Correct 264 ms 32560 KB Output is correct
38 Correct 215 ms 32512 KB Output is correct
39 Correct 330 ms 62772 KB Output is correct
40 Correct 318 ms 62960 KB Output is correct
41 Correct 269 ms 30720 KB Output is correct
42 Correct 255 ms 43468 KB Output is correct
43 Correct 176 ms 30440 KB Output is correct
44 Correct 313 ms 57960 KB Output is correct
45 Correct 174 ms 30568 KB Output is correct
46 Correct 228 ms 30580 KB Output is correct
47 Correct 294 ms 58204 KB Output is correct
48 Correct 317 ms 58320 KB Output is correct
49 Correct 871 ms 35812 KB Output is correct
50 Correct 358 ms 35604 KB Output is correct
51 Correct 468 ms 64728 KB Output is correct
52 Correct 449 ms 64316 KB Output is correct
53 Correct 850 ms 33932 KB Output is correct
54 Correct 579 ms 47440 KB Output is correct
55 Correct 287 ms 31564 KB Output is correct
56 Correct 440 ms 59816 KB Output is correct
57 Correct 270 ms 32140 KB Output is correct
58 Correct 712 ms 32796 KB Output is correct
59 Correct 289 ms 58304 KB Output is correct