Submission #289989

# Submission time Handle Problem Language Result Execution time Memory
289989 2020-09-03T09:37:54 Z Berted Horses (IOI15_horses) C++14
100 / 100
1318 ms 46200 KB
#include "horses.h"
#include <iostream>
#include <algorithm>
#include <utility>
#define ll long long
#define pii pair<ll, ll>
#define fst first
#define snd second

const int SZ = (1 << 20) + 5;
const ll MD = 1e9 + 7;

using namespace std;

int n;
ll x[500001], y[500001];

namespace segX
{
	pii seg[SZ];
	
	inline pii merge(const pii &L, const pii &R)
	{
		pii ret;
		ret.fst = min(MD, L.fst * R.fst);
		ret.snd = (L.snd * R.snd) % MD;
		return ret;
	}

	void build(int idx, int L, int R)
	{
		seg[idx] = {1, 1};
		if (L <= R)
		{
			if (L == R) {seg[idx] = {x[L], x[R]};}
			else
			{
				int M = L + R >> 1;
				build(idx << 1, L, M);
				build(idx << 1 | 1, M + 1, R);
				seg[idx] = merge(seg[idx << 1], seg[idx << 1 | 1]);
			} 
		}
	}

	void update(int idx, int L, int R, int x, int v)
	{
		if (L <= R)
		{
			if (L == x && x == R) {seg[idx] = {v, v};}
			else
			{
				int M = L + R >> 1;
				if (x <= M) {update(idx << 1, L, M, x, v);}
				else {update(idx << 1 | 1, M + 1, R, x, v);}
				seg[idx] = merge(seg[idx << 1], seg[idx << 1 | 1]);
			}
		}
	}

	pii query(int idx, int L, int R, int l, int r)
	{
		l = max(L, l); r = min(R, r);
		if (l <= r)
		{
			if (L == l && R == r) {return seg[idx];}
			else
			{
				int M = L + R >> 1;
				return merge(query(idx << 1, L, M, l, r), query(idx << 1 | 1, M + 1, R, l, r));
			}
		}
		return {1, 1};
	}
}

namespace segY
{
	int seg[SZ];

	inline int merge(const int &L, const int &R)
	{
		if (L == -1) {return R;}
		if (R == -1) {return L;}
		if (y[R] * segX::query(1, 0, n - 1, L + 1, R).fst >= y[L]) {return R;}
		else {return L;}
	}

	void build(int idx, int L, int R)
	{
		seg[idx] = -1;
		if (L <= R)
		{
			if (L == R) {seg[idx] = L;}
			else
			{
				int M = L + R >> 1;
				build(2 * idx, L, M);
				build(2 * idx + 1, M + 1, R);
				seg[idx] = merge(seg[2 * idx], seg[2 * idx + 1]);
			}
		}
	}

	void update(int idx, int L, int R, int x)
	{
		if (L < R)
		{
			int M = L + R >> 1;
			if (x <= M) {update(2 * idx, L, M, x);}
			else {update(2 * idx + 1, M + 1, R, x);}
			seg[idx] = merge(seg[2 * idx], seg[2 * idx + 1]);
		}
	}
}

inline ll eval(int idx) {return y[idx] * segX::query(1, 0, n - 1, 0, idx).snd % MD;}


int init(int N, int X[], int Y[]) 
{
	n = N;
	for (int i = 0; i < n; i++) {x[i] = X[i]; y[i] = Y[i];}
	segX::build(1, 0, n - 1);
	segY::build(1, 0, n - 1);
	return eval(segY::seg[1]);
}

int updateX(int pos, int val) 
{
	segX::update(1, 0, n - 1, pos, val);
	segY::update(1, 0, n - 1, pos);	
	return eval(segY::seg[1]);
}

int updateY(int pos, int val) 
{
	y[pos] = val;
	segY::update(1, 0, n - 1, pos);
	return eval(segY::seg[1]);
}

Compilation message

horses.cpp: In function 'void segX::build(int, int, int)':
horses.cpp:38:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   38 |     int M = L + R >> 1;
      |             ~~^~~
horses.cpp: In function 'void segX::update(int, int, int, int, int)':
horses.cpp:53:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   53 |     int M = L + R >> 1;
      |             ~~^~~
horses.cpp: In function 'std::pair<long long int, long long int> segX::query(int, int, int, int, int)':
horses.cpp:69:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   69 |     int M = L + R >> 1;
      |             ~~^~~
horses.cpp: In function 'void segY::build(int, int, int)':
horses.cpp:97:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   97 |     int M = L + R >> 1;
      |             ~~^~~
horses.cpp: In function 'void segY::update(int, int, int, int)':
horses.cpp:109:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  109 |    int M = L + R >> 1;
      |            ~~^~~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:126:13: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  126 |  return eval(segY::seg[1]);
      |         ~~~~^~~~~~~~~~~~~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:133:13: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  133 |  return eval(segY::seg[1]);
      |         ~~~~^~~~~~~~~~~~~~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:140:13: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  140 |  return eval(segY::seg[1]);
      |         ~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 288 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 3 ms 384 KB Output is correct
28 Correct 3 ms 512 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 710 ms 37336 KB Output is correct
2 Correct 709 ms 46200 KB Output is correct
3 Correct 893 ms 37496 KB Output is correct
4 Correct 844 ms 41336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 3 ms 512 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Correct 2 ms 512 KB Output is correct
33 Correct 328 ms 36612 KB Output is correct
34 Correct 312 ms 36604 KB Output is correct
35 Correct 275 ms 43512 KB Output is correct
36 Correct 257 ms 43640 KB Output is correct
37 Correct 258 ms 34852 KB Output is correct
38 Correct 249 ms 35832 KB Output is correct
39 Correct 218 ms 34656 KB Output is correct
40 Correct 234 ms 38648 KB Output is correct
41 Correct 246 ms 34684 KB Output is correct
42 Correct 227 ms 34856 KB Output is correct
43 Correct 202 ms 39032 KB Output is correct
44 Correct 203 ms 39032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 0 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 3 ms 512 KB Output is correct
24 Correct 3 ms 512 KB Output is correct
25 Correct 2 ms 512 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 3 ms 512 KB Output is correct
28 Correct 3 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 3 ms 512 KB Output is correct
32 Correct 2 ms 512 KB Output is correct
33 Correct 700 ms 37416 KB Output is correct
34 Correct 701 ms 46200 KB Output is correct
35 Correct 898 ms 37564 KB Output is correct
36 Correct 837 ms 41260 KB Output is correct
37 Correct 319 ms 36780 KB Output is correct
38 Correct 311 ms 36600 KB Output is correct
39 Correct 271 ms 43640 KB Output is correct
40 Correct 251 ms 43464 KB Output is correct
41 Correct 257 ms 34808 KB Output is correct
42 Correct 244 ms 35704 KB Output is correct
43 Correct 218 ms 34680 KB Output is correct
44 Correct 231 ms 38612 KB Output is correct
45 Correct 241 ms 34680 KB Output is correct
46 Correct 231 ms 34808 KB Output is correct
47 Correct 209 ms 39164 KB Output is correct
48 Correct 204 ms 39160 KB Output is correct
49 Correct 1318 ms 38648 KB Output is correct
50 Correct 1206 ms 38904 KB Output is correct
51 Correct 748 ms 45432 KB Output is correct
52 Correct 558 ms 44920 KB Output is correct
53 Correct 958 ms 37004 KB Output is correct
54 Correct 714 ms 37496 KB Output is correct
55 Correct 527 ms 35992 KB Output is correct
56 Correct 547 ms 40436 KB Output is correct
57 Correct 769 ms 36472 KB Output is correct
58 Correct 651 ms 36948 KB Output is correct
59 Correct 208 ms 39032 KB Output is correct