Submission #144021

# Submission time Handle Problem Language Result Execution time Memory
144021 2019-08-15T17:06:45 Z dolphingarlic Horses (IOI15_horses) C++14
100 / 100
423 ms 118776 KB
#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll md = 1000000007;
vector<ll> x;
vector<ll> y;
ll n;
//overall maximum: prefix * maxy
struct Data{
	ll prefix=1; //prefix inclusive of max
	bool prefixgmd=false;
	ll suffix=1;
	bool suffixgmd=false;
	ll maxy=1;
};
 
Data combine(Data l, Data r){
	//first determine which has maximum
	Data ans;
	//cout<<"combine"<<endl;
	//cout<<l.prefix<<" "<<l.prefixgmd<<" "<<l.suffix<<" "<<l.suffixgmd<<" "<<l.maxy<<endl;
	//cout<<r.prefix<<" "<<r.prefixgmd<<" "<<r.suffix<<" "<<r.suffixgmd<<" "<<r.maxy<<endl;
	if (l.suffixgmd||r.prefixgmd||l.suffix*r.prefix>=md){
		//r has maximum
		ans.prefix=(((l.prefix*l.suffix)%md)*r.prefix)%md;
		ans.prefixgmd=true;
		ans.suffix=r.suffix;
		ans.suffixgmd=r.suffixgmd;
		ans.maxy=r.maxy;
	} else {
		ll rval = l.suffix*r.prefix*r.maxy;
		ll lval = l.maxy;
		if (rval>lval){
			//r has maximum
			ans.maxy=r.maxy;
			if (l.prefixgmd){
				ans.prefixgmd=true;
				ans.prefix=(((l.prefix*l.suffix)%md)*r.prefix)%md;
			} else {
				ans.prefix=(l.prefix*l.suffix);
				if (ans.prefix>=md){
					ans.prefix%=md;
					ans.prefix*=r.prefix;
					ans.prefix%=md;
					ans.prefixgmd=true;
				} else {
					ans.prefix*=r.prefix;
					if (ans.prefix>=md){
						ans.prefix%=md;
						ans.prefixgmd=true;
					} else {
						ans.prefixgmd=false;
					}
				}
				ans.prefix%=md;
			}
			ans.suffix=r.suffix;
			ans.suffixgmd=r.suffixgmd;
		} else {
			//l has maximum
			ans.maxy=l.maxy;
			ans.prefixgmd=l.prefixgmd;
			ans.prefix=l.prefix;
			if (r.suffixgmd){
				ans.suffixgmd=true;
				ans.suffix=(((l.suffix*r.prefix)%md)*r.suffix)%md;
			} else {
				ans.suffix=(l.suffix*r.prefix);
				if (ans.suffix>=md){
					ans.suffix%=md;
					ans.suffix*=r.suffix;
					ans.suffix%=md;
					ans.suffixgmd=true;
				} else {
					ans.suffix*=r.suffix;
					if (ans.suffix>=md){
						ans.suffix%=md;
						ans.suffixgmd=true;
					} else {
						ans.suffixgmd=false;
					}
				}
				ans.suffix%=md;
			}
		}
	}
	//cout<<ans.prefix<<" "<<ans.prefixgmd<<" "<<ans.suffix<<" "<<ans.suffixgmd<<" "<<ans.maxy<<endl;
	return ans;
}
 
struct Node{
	ll s,e,m;
	Data d;
	Node *l, *r;
	Node(ll _s, ll _e){
		s=_s;e=_e;
		m=(s+e)/2;
		if (s==e){
			d.prefix=x[s];
			d.maxy=y[s];
			return;
		}
		l=new Node(s,m);
		r=new Node(m+1,e);
		d=combine(l->d,r->d);
	}
 
	void update(ll pos){
		if (s==e){
			d.prefix=x[pos];
			d.maxy=y[pos];
			d.suffix=1;
			d.prefixgmd=false;
			d.suffixgmd=false;
			return;
		}
		if (pos<=m){
			l->update(pos);
		} else {
			r->update(pos);
		}
		d=combine(l->d,r->d);
	}
 
	Data query(ll a, ll b){
		if (a==s&&b==e){
			return d;
		}
		if (b<=m){
			return l->query(a,b);
		} else if (m<a){
			return r->query(a,b);
		} else {
			return combine(l->query(a,m),r->query(m+1,b));
		}
	}
};
Node *root;
int init(int N, int X[], int Y[]) {
	n=N;
	x=vector<ll>(X,X+n);
	y=vector<ll>(Y,Y+n);
	root = new Node(0,n-1);
	Data d = root->query(0,n-1);
	ll ans = (d.prefix*d.maxy)%md;
	return ans;
}
 
int updateX(int pos, int val) {
	//cout<<"x"<<endl;
	x[pos]=val;
	root->update(pos);
	Data d = root->query(0,n-1);
	ll ans = (d.prefix*d.maxy)%md;
	return ans;
}
 
int updateY(int pos, int val) {
	//cout<<"y"<<endl;
	y[pos]=val;
	root->update(pos);
	Data d = root->query(0,n-1);
	ll ans = (d.prefix*d.maxy)%md;
	return ans;
}

Compilation message

horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:147:9: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
  return ans;
         ^~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:156:9: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
  return ans;
         ^~~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:165:9: warning: conversion to 'int' from 'll {aka long long int}' may alter its value [-Wconversion]
  return ans;
         ^~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 252 KB Output is correct
9 Correct 3 ms 256 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 128 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 380 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 256 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 380 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 380 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 3 ms 504 KB Output is correct
24 Correct 3 ms 504 KB Output is correct
25 Correct 3 ms 504 KB Output is correct
26 Correct 3 ms 504 KB Output is correct
27 Correct 3 ms 504 KB Output is correct
28 Correct 3 ms 504 KB Output is correct
29 Correct 3 ms 504 KB Output is correct
30 Correct 3 ms 504 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 3 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 228 ms 106984 KB Output is correct
2 Correct 420 ms 107000 KB Output is correct
3 Correct 402 ms 107056 KB Output is correct
4 Correct 416 ms 107020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 292 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 256 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 256 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 256 KB Output is correct
20 Correct 2 ms 256 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 256 KB Output is correct
23 Correct 3 ms 504 KB Output is correct
24 Correct 3 ms 532 KB Output is correct
25 Correct 3 ms 504 KB Output is correct
26 Correct 3 ms 504 KB Output is correct
27 Correct 3 ms 504 KB Output is correct
28 Correct 3 ms 504 KB Output is correct
29 Correct 3 ms 508 KB Output is correct
30 Correct 3 ms 504 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 3 ms 504 KB Output is correct
33 Correct 169 ms 106104 KB Output is correct
34 Correct 170 ms 106104 KB Output is correct
35 Correct 181 ms 106128 KB Output is correct
36 Correct 182 ms 106084 KB Output is correct
37 Correct 159 ms 106048 KB Output is correct
38 Correct 150 ms 106060 KB Output is correct
39 Correct 142 ms 106024 KB Output is correct
40 Correct 163 ms 106128 KB Output is correct
41 Correct 142 ms 106104 KB Output is correct
42 Correct 141 ms 106072 KB Output is correct
43 Correct 154 ms 105976 KB Output is correct
44 Correct 153 ms 105980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 256 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 2 ms 256 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 256 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 2 ms 256 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 256 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 252 KB Output is correct
20 Correct 2 ms 256 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 3 ms 504 KB Output is correct
24 Correct 3 ms 504 KB Output is correct
25 Correct 3 ms 504 KB Output is correct
26 Correct 3 ms 504 KB Output is correct
27 Correct 3 ms 504 KB Output is correct
28 Correct 3 ms 504 KB Output is correct
29 Correct 3 ms 504 KB Output is correct
30 Correct 3 ms 504 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 3 ms 508 KB Output is correct
33 Correct 235 ms 107000 KB Output is correct
34 Correct 423 ms 107128 KB Output is correct
35 Correct 398 ms 106964 KB Output is correct
36 Correct 397 ms 107060 KB Output is correct
37 Correct 167 ms 106104 KB Output is correct
38 Correct 169 ms 106076 KB Output is correct
39 Correct 179 ms 106112 KB Output is correct
40 Correct 181 ms 106104 KB Output is correct
41 Correct 159 ms 106104 KB Output is correct
42 Correct 151 ms 106044 KB Output is correct
43 Correct 142 ms 105964 KB Output is correct
44 Correct 162 ms 106140 KB Output is correct
45 Correct 141 ms 106096 KB Output is correct
46 Correct 140 ms 106104 KB Output is correct
47 Correct 152 ms 105976 KB Output is correct
48 Correct 153 ms 106104 KB Output is correct
49 Correct 382 ms 112004 KB Output is correct
50 Correct 411 ms 112004 KB Output is correct
51 Correct 269 ms 118776 KB Output is correct
52 Correct 290 ms 118284 KB Output is correct
53 Correct 376 ms 110392 KB Output is correct
54 Correct 262 ms 110852 KB Output is correct
55 Correct 220 ms 109240 KB Output is correct
56 Correct 265 ms 113788 KB Output is correct
57 Correct 216 ms 109816 KB Output is correct
58 Correct 221 ms 110404 KB Output is correct
59 Correct 155 ms 112324 KB Output is correct