#include <bits/stdc++.h>
#ifndef LOCAL
#include "secret.h"
#else
int Secret(int x, int y) {return x+y;}
#endif
using namespace std;
#ifdef DEBUG
auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;}
auto operator<<(auto &o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";}
#define debug(X) cerr << "["#X"]: " << X << '\n';
#else
#define debug(X) ;
#endif
#define ll long long
#define all(v) (v).begin(), (v).end()
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
#define fi first
#define se second
class RMQ { //online
public:
vector<vector<int>> st;
int N, K;
vector<int> lg;
RMQ() {
N = K = 0;
}
RMQ(vector<int> v) {
N = v.size();
st.resize(N);
lg.resize(N+1);
lg[1] = 0;
FOR(i, 2, N) {
lg[i] = lg[i/2]+1;
}
K = lg[N]+1;
REP(i, N) st[i].resize(K);
REP(j, K) {
for (int i = 0; i+(1<<j)-1 < N; i++) {
if (j == 0) {st[i][j] = v[i]; continue;}
st[i][j] = Secret(st[i][j-1], st[i+(1<<(j-1))][j-1]);
}
}
}
void set(vector<int> v) {
N = v.size();
st.resize(N);
lg.resize(N+1);
lg[1] = 0;
FOR(i, 2, N) {
lg[i] = lg[i/2]+1;
}
K = lg[N]+1;
REP(i, N) st[i].resize(K);
REP(j, K) {
for (int i = 0; i+(1<<j)-1 < N; i++) {
if (j == 0) {st[i][j] = v[i]; continue;}
st[i][j] = Secret(st[i][j-1], st[i+(1<<(j-1))][j-1]);
}
}
}
int query(int l, int r) {
int d = lg[r-l+1];
int res = 0;
while (l <= r) {
if ((1<<d) <= r-l+1) {
res = Secret(res, st[l][d]);
l += (1<<d);
}
d--;
}
return res;
}
};
RMQ st;
void Init(int N, int A[]) {
vector<int> v(N);
REP(i, N) v[i] = A[i];
st.set(v);
}
int Query(int L, int R) {
return st.query(L, R);
}
#ifdef LOCAL
int main() {
int n;
cin >> n;
int a[n];
REP(i, n) cin >> a[i];
Init(n, a);
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
cout << Query(l, r) << '\n';
}
return 0;
};
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
95 ms |
2496 KB |
Wrong Answer: Query(222, 254) - expected : 34031541, actual : 34031537. |
2 |
Incorrect |
95 ms |
2408 KB |
Wrong Answer: Query(102, 157) - expected : 32612619, actual : 569483529. |
3 |
Incorrect |
95 ms |
2420 KB |
Wrong Answer: Query(67, 224) - expected : 202440844, actual : 739311628. |
4 |
Incorrect |
357 ms |
4444 KB |
Wrong Answer: Query(727, 872) - expected : 870334875, actual : 870334619. |
5 |
Incorrect |
351 ms |
4400 KB |
Wrong Answer: Query(761, 790) - expected : 112945480, actual : 112871752. |
6 |
Incorrect |
354 ms |
4404 KB |
Wrong Answer: Query(915, 915) - expected : 282904741, actual : 278446241. |
7 |
Partially correct |
394 ms |
4324 KB |
Output is partially correct - number of calls to Secret by Init = 7987, maximum number of calls to Secret by Query = 9 |
8 |
Partially correct |
389 ms |
4364 KB |
Output is partially correct - number of calls to Secret by Init = 7987, maximum number of calls to Secret by Query = 8 |
9 |
Partially correct |
398 ms |
4432 KB |
Output is partially correct - number of calls to Secret by Init = 7987, maximum number of calls to Secret by Query = 9 |
10 |
Partially correct |
390 ms |
4316 KB |
Output is partially correct - number of calls to Secret by Init = 7987, maximum number of calls to Secret by Query = 8 |