답안 #641876

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
641876 2022-09-17T17:58:55 Z QwertyPi 말 (IOI15_horses) C++14
100 / 100
899 ms 64368 KB
#include "horses.h"


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const ll MOD = 1e9 + 7;

const ll MAXN = 5e5 + 11;

ll x[MAXN], y[MAXN];

struct BIT{
	ll bit[MAXN] = {0}, id;
	void init(ll id, ll (*f)(ll, ll)) { 
		this->id = id; 
		fill(bit, bit + MAXN, id);
		combine = f;
	}
	void add(ll pos, ll val){
		pos++;
		for(ll i = pos; i < MAXN; i += i & -i){
			bit[i] = combine(bit[i], val);
		}
	}
	ll qry(ll pos){
		pos++; ll ret = id;
		for(ll i = pos; i; i -= i & -i){
			ret = combine(ret, bit[i]);
		}
		return ret;
	}
	ll (*combine)(ll, ll);
} bit_add, bit_mul;


ll N;

namespace RMQ{
	ll t[MAXN * 4] = {0};
	void upd(int pos, int val, int v = 0, int tl = 0, int tr = N - 1){
		if(tl == tr){
			t[v] = val;
			return;
		}
		int tm = (tl + tr) / 2;
		if(pos <= tm){
			upd(pos, val, v * 2 + 1, tl, tm);
		}else{
			upd(pos, val, v * 2 + 2, tm + 1, tr);
		}
		t[v] = std::max(t[v * 2 + 1], t[v * 2 + 2]);
	}
	ll max(int l, int r, int v = 0, int tl = 0, int tr = N - 1){
		if(r < tl || tr < l) return 0;
		if(l <= tl && tr <= r) return t[v];
		int tm = (tl + tr) / 2;
		return std::max(max(l, r, v * 2 + 1, tl, tm),
				max(l, r, v * 2 + 2, tm + 1, tr));
	}
};

set<ll> n1;

ll add(ll x, ll y) { return x + y; }
ll mul(ll x, ll y) { return x * y % MOD; }
ll bp(ll a, ll b) { if(b == 0) return 1; return bp(a * a % MOD, b / 2) * (b % 2 ? a : 1) % MOD; }
ll mi(ll a) { return bp(a, MOD - 2); }

ll query() {
	ll ans = 0, rans = 1;
	ll c = 1;
	auto ptr = --n1.end();
	while(ptr != n1.begin() && c <= MOD){
		ptr--; c *= x[*ptr];
	}
	c = 1;
	for(; ptr != --n1.end(); ptr++){
		ll v = RMQ::max(*ptr, *next(ptr) - 1);
		if(c * v > ans){
			ans = c * v;
			rans = bit_add.qry(*ptr) > 0 ? 0 : mul(bit_mul.qry(*ptr), v);
		}
		c *= x[*next(ptr)];
	}
	return rans;
}

int init(int N, int X[], int Y[]) {
	::N = N;
	bit_add.init(0, add);
	bit_mul.init(1, mul);
	
	for(int i = 0; i < N; i++) x[i] = X[i];
	for(int i = 0; i < N; i++) y[i] = Y[i];

	for(int i = 0; i < N; i++){
		if(x[i] == MOD) bit_add.add(i, 1);
		else bit_mul.add(i, x[i]);
	}
	
	n1.insert(0); n1.insert(N);
	for(int i = 0; i < N; i++){
		if(x[i] != 1) n1.insert(i);
	}
	
	for(int i = 0; i < N; i++){
		RMQ::upd(i, y[i]);
	}
	return query();
}

int updateX(int pos, int val) {	
	if(x[pos] == MOD) bit_add.add(pos, -1);
	else bit_mul.add(pos, mi(x[pos]));
	if(x[pos] != 1 && pos != 0) n1.erase(pos);
	x[pos] = val;
	if(x[pos] == MOD) bit_add.add(pos, 1);
	else bit_mul.add(pos, x[pos]);
	if(x[pos] != 1) n1.insert(pos);
	return query();
}

int updateY(int pos, int val) {
	y[pos] = val;
	RMQ::upd(pos, val);
	return query();
}

Compilation message

horses.cpp: In member function 'void BIT::init(long long int, long long int (*)(long long int, long long int))':
horses.cpp:16:15: warning: declaration of 'id' shadows a member of 'BIT' [-Wshadow]
   16 |  void init(ll id, ll (*f)(ll, ll)) {
      |               ^
horses.cpp:15:22: note: shadowed declaration is here
   15 |  ll bit[MAXN] = {0}, id;
      |                      ^~
horses.cpp: At global scope:
horses.cpp:42:63: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   42 |  void upd(int pos, int val, int v = 0, int tl = 0, int tr = N - 1){
      |                                                             ~~^~~
horses.cpp:55:57: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   55 |  ll max(int l, int r, int v = 0, int tl = 0, int tr = N - 1){
      |                                                       ~~^~~
horses.cpp: In function 'long long int add(long long int, long long int)':
horses.cpp:66:17: warning: declaration of 'y' shadows a global declaration [-Wshadow]
   66 | ll add(ll x, ll y) { return x + y; }
      |                 ^
horses.cpp:12:13: note: shadowed declaration is here
   12 | ll x[MAXN], y[MAXN];
      |             ^
horses.cpp:66:11: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   66 | ll add(ll x, ll y) { return x + y; }
      |           ^
horses.cpp:12:4: note: shadowed declaration is here
   12 | ll x[MAXN], y[MAXN];
      |    ^
horses.cpp: In function 'long long int mul(long long int, long long int)':
horses.cpp:67:17: warning: declaration of 'y' shadows a global declaration [-Wshadow]
   67 | ll mul(ll x, ll y) { return x * y % MOD; }
      |                 ^
horses.cpp:12:13: note: shadowed declaration is here
   12 | ll x[MAXN], y[MAXN];
      |             ^
horses.cpp:67:11: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   67 | ll mul(ll x, ll y) { return x * y % MOD; }
      |           ^
horses.cpp:12:4: note: shadowed declaration is here
   12 | ll x[MAXN], y[MAXN];
      |    ^
horses.cpp: In function 'long long int query()':
horses.cpp:80:19: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   80 |   ll v = RMQ::max(*ptr, *next(ptr) - 1);
      |                   ^~~~
horses.cpp:80:36: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   80 |   ll v = RMQ::max(*ptr, *next(ptr) - 1);
      |                         ~~~~~~~~~~~^~~
horses.cpp:80:39: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   80 |   ll v = RMQ::max(*ptr, *next(ptr) - 1);
      |                                       ^
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:90:14: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   90 | int init(int N, int X[], int Y[]) {
      |          ~~~~^
horses.cpp:38:4: note: shadowed declaration is here
   38 | ll N;
      |    ^
horses.cpp:109:18: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  109 |   RMQ::upd(i, y[i]);
      |               ~~~^
horses.cpp:109:19: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  109 |   RMQ::upd(i, y[i]);
      |                   ^
horses.cpp:111:14: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  111 |  return query();
      |         ~~~~~^~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:122:14: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  122 |  return query();
      |         ~~~~~^~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:127:19: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  127 |  RMQ::upd(pos, val);
      |                   ^
horses.cpp:128:14: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  128 |  return query();
      |         ~~~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 5 ms 8020 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 5 ms 8152 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Correct 4 ms 8148 KB Output is correct
7 Correct 4 ms 8148 KB Output is correct
8 Correct 4 ms 8148 KB Output is correct
9 Correct 4 ms 8148 KB Output is correct
10 Correct 4 ms 8148 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 4 ms 8148 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 4 ms 8148 KB Output is correct
15 Correct 4 ms 8060 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 4 ms 8148 KB Output is correct
18 Correct 5 ms 8148 KB Output is correct
19 Correct 4 ms 8148 KB Output is correct
20 Correct 4 ms 8044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Correct 4 ms 8148 KB Output is correct
7 Correct 4 ms 8148 KB Output is correct
8 Correct 4 ms 8148 KB Output is correct
9 Correct 4 ms 8148 KB Output is correct
10 Correct 4 ms 8148 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 5 ms 8148 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 4 ms 8140 KB Output is correct
15 Correct 4 ms 8148 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 4 ms 8148 KB Output is correct
18 Correct 4 ms 8148 KB Output is correct
19 Correct 4 ms 8148 KB Output is correct
20 Correct 4 ms 8148 KB Output is correct
21 Correct 4 ms 8148 KB Output is correct
22 Correct 4 ms 8148 KB Output is correct
23 Correct 5 ms 8148 KB Output is correct
24 Correct 5 ms 8148 KB Output is correct
25 Correct 5 ms 8148 KB Output is correct
26 Correct 5 ms 8148 KB Output is correct
27 Correct 9 ms 8204 KB Output is correct
28 Correct 6 ms 8212 KB Output is correct
29 Correct 5 ms 8148 KB Output is correct
30 Correct 5 ms 8148 KB Output is correct
31 Correct 6 ms 8144 KB Output is correct
32 Correct 10 ms 8316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 716 ms 52588 KB Output is correct
2 Correct 391 ms 52476 KB Output is correct
3 Correct 423 ms 52556 KB Output is correct
4 Correct 504 ms 52528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Correct 4 ms 8084 KB Output is correct
7 Correct 4 ms 8148 KB Output is correct
8 Correct 4 ms 8148 KB Output is correct
9 Correct 4 ms 8148 KB Output is correct
10 Correct 5 ms 8096 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 4 ms 8148 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 4 ms 8148 KB Output is correct
15 Correct 4 ms 8148 KB Output is correct
16 Correct 4 ms 8156 KB Output is correct
17 Correct 4 ms 8148 KB Output is correct
18 Correct 4 ms 8020 KB Output is correct
19 Correct 5 ms 8148 KB Output is correct
20 Correct 4 ms 8148 KB Output is correct
21 Correct 4 ms 8148 KB Output is correct
22 Correct 4 ms 8020 KB Output is correct
23 Correct 5 ms 8148 KB Output is correct
24 Correct 5 ms 8148 KB Output is correct
25 Correct 5 ms 8148 KB Output is correct
26 Correct 5 ms 8148 KB Output is correct
27 Correct 10 ms 8148 KB Output is correct
28 Correct 6 ms 8212 KB Output is correct
29 Correct 5 ms 8148 KB Output is correct
30 Correct 5 ms 8160 KB Output is correct
31 Correct 7 ms 8148 KB Output is correct
32 Correct 12 ms 8148 KB Output is correct
33 Correct 115 ms 28236 KB Output is correct
34 Correct 111 ms 28292 KB Output is correct
35 Correct 253 ms 51856 KB Output is correct
36 Correct 242 ms 51660 KB Output is correct
37 Correct 181 ms 28276 KB Output is correct
38 Correct 182 ms 40140 KB Output is correct
39 Correct 106 ms 28080 KB Output is correct
40 Correct 232 ms 57556 KB Output is correct
41 Correct 129 ms 30156 KB Output is correct
42 Correct 161 ms 30172 KB Output is correct
43 Correct 225 ms 57932 KB Output is correct
44 Correct 227 ms 57952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 5 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8136 KB Output is correct
5 Correct 5 ms 8148 KB Output is correct
6 Correct 4 ms 8140 KB Output is correct
7 Correct 4 ms 8148 KB Output is correct
8 Correct 4 ms 8148 KB Output is correct
9 Correct 4 ms 8148 KB Output is correct
10 Correct 4 ms 8148 KB Output is correct
11 Correct 4 ms 8148 KB Output is correct
12 Correct 4 ms 8148 KB Output is correct
13 Correct 4 ms 8148 KB Output is correct
14 Correct 4 ms 8148 KB Output is correct
15 Correct 4 ms 8148 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 5 ms 8148 KB Output is correct
18 Correct 4 ms 8148 KB Output is correct
19 Correct 4 ms 8152 KB Output is correct
20 Correct 4 ms 8148 KB Output is correct
21 Correct 4 ms 8148 KB Output is correct
22 Correct 4 ms 8148 KB Output is correct
23 Correct 5 ms 8148 KB Output is correct
24 Correct 5 ms 8148 KB Output is correct
25 Correct 5 ms 8148 KB Output is correct
26 Correct 5 ms 8212 KB Output is correct
27 Correct 9 ms 8192 KB Output is correct
28 Correct 8 ms 8148 KB Output is correct
29 Correct 5 ms 8148 KB Output is correct
30 Correct 5 ms 8148 KB Output is correct
31 Correct 6 ms 8148 KB Output is correct
32 Correct 8 ms 8148 KB Output is correct
33 Correct 730 ms 52516 KB Output is correct
34 Correct 396 ms 52548 KB Output is correct
35 Correct 414 ms 52436 KB Output is correct
36 Correct 527 ms 52532 KB Output is correct
37 Correct 120 ms 28260 KB Output is correct
38 Correct 134 ms 28196 KB Output is correct
39 Correct 253 ms 51736 KB Output is correct
40 Correct 250 ms 51704 KB Output is correct
41 Correct 182 ms 28212 KB Output is correct
42 Correct 176 ms 40108 KB Output is correct
43 Correct 106 ms 28012 KB Output is correct
44 Correct 235 ms 57468 KB Output is correct
45 Correct 131 ms 30156 KB Output is correct
46 Correct 158 ms 30240 KB Output is correct
47 Correct 227 ms 57948 KB Output is correct
48 Correct 224 ms 57828 KB Output is correct
49 Correct 236 ms 35224 KB Output is correct
50 Correct 207 ms 35200 KB Output is correct
51 Correct 403 ms 64368 KB Output is correct
52 Correct 325 ms 63824 KB Output is correct
53 Correct 899 ms 33488 KB Output is correct
54 Correct 395 ms 46976 KB Output is correct
55 Correct 212 ms 31144 KB Output is correct
56 Correct 328 ms 59384 KB Output is correct
57 Correct 445 ms 31768 KB Output is correct
58 Correct 654 ms 32408 KB Output is correct
59 Correct 227 ms 57968 KB Output is correct