#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
#include <time.h>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const ll inf = 1e18;
const ll mod = 1000000007;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 5e5 + 5;
#include "secret.h"
int n;
int a[1001];
/*
Segment tree solution
* MAX calls to Secret by Init = 999
* MAX calls to Secret by Query = 16
int seg[10000];
void build(int k, int l, int r){
if(l == r){
seg[k] = a[l];
return;
}
int mid = (l + r) / 2;
build(k * 2, l, mid);
build(k * 2 + 1, mid + 1, r);
seg[k] = Secret(seg[k * 2], seg[k * 2 + 1]);
}
vector<int> v;
void query(int k, int l, int r, int x, int y){
if(y < l || r < x) return;
if(x <= l && r <= y){
v.pb(seg[k]);
return;
}
int mid = (l + r) / 2;
query(k * 2, l, mid, x, y);
query(k * 2 + 1, mid + 1, r, x, y);
}
int Query(int L, int R){
v.clear();
query(1, 0, n - 1, L, R);
int ans = v[0];
fr(i, 1, (int)v.size()){
ans = Secret(ans, v[i]);
}
return ans;
}
*/
/**
Sparse table solution
* MAX calls to Secret by Init = 7987
* MAX calls to Secret by Query = 8
int sparse[10001][10];
void Init(int N, int A[]) {
n = N;
fr(i, 0, N){
a[i] = A[i];
sparse[i][0] = a[i];
}
fr(i, 1, 10){
for(int j = 0; j + (1 << i) - 1 < n; j ++){
sparse[j][i] = Secret(sparse[j][i - 1], sparse[j + (1<<(i-1))][i - 1]);
}
}
}
int Query(int L, int R){
int len = (R - L + 1);
int ans = -1;
for(int i = 9; i >= 0; i --){
if(len & (1 << i)){
if(ans == -1){
ans = sparse[L][i];
}
else{
ans = Secret(ans, sparse[L][i]);
}
L += (1 << i);
}
}
return ans;
}
**/
//Meet in the middle technique:
int v[1000][1000];
void f(int l, int r){
if(l == r){
v[l][r] = a[l];
return;
}
int mid = (l + r) / 2;
v[mid][mid] = a[mid];
v[mid + 1][mid + 1] = a[mid + 1];
for(int i = mid - 1; i >= l; i --){
v[i][mid] = Secret(a[i], v[i + 1][mid]);
}
for(int i = mid + 2; i <= r; i ++){
v[mid + 1][i] = Secret(v[mid + 1][i - 1], a[i]);
}
f(l, mid);
f(mid + 1, r);
}
void Init(int N, int A[]) {
n = N;
fr(i, 0, N){
a[i] = A[i];
}
memset(v, -1, sizeof(v));
f(0, n - 1);
}
int Query(int L, int R){
if(v[L][R] != -1) return v[L][R];
if(L + 1 == R){
return Secret(a[L], a[R]);
}
fr(i, L, R){
if(v[L][i] != -1 && v[i + 1][R] != -1) return Secret(v[L][i], v[i + 1][R]);
}
}
/*
int main(){
int N;
cin >> N;
int A[N];
fr(i, 0, N){
cin >> A[i];
}
Init(N, A);
int Q;
cin >> Q;
while(Q --){
int L, R;
cin >> L >> R;
Query(L, R);
}
}
*/
Compilation message
secret.cpp: In function 'int Query(int, int)':
secret.cpp:147:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
6264 KB |
Output is correct - number of calls to Secret by Init = 3578, maximum number of calls to Secret by Query = 1 |
2 |
Correct |
149 ms |
6264 KB |
Output is correct - number of calls to Secret by Init = 3586, maximum number of calls to Secret by Query = 1 |
3 |
Correct |
149 ms |
6264 KB |
Output is correct - number of calls to Secret by Init = 3595, maximum number of calls to Secret by Query = 1 |
4 |
Correct |
513 ms |
8208 KB |
Output is correct - number of calls to Secret by Init = 7969, maximum number of calls to Secret by Query = 1 |
5 |
Correct |
508 ms |
8312 KB |
Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1 |
6 |
Correct |
505 ms |
8284 KB |
Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1 |
7 |
Correct |
515 ms |
8288 KB |
Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1 |
8 |
Correct |
511 ms |
8312 KB |
Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1 |
9 |
Correct |
515 ms |
8312 KB |
Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1 |
10 |
Correct |
514 ms |
8248 KB |
Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1 |