#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 res = st[l][0];
l++;
if (l > r) return res;
int d = lg[r-l+1];
while (l <= r) {
if ((1<<d) <= r-l+1) {
res = Secret(res, st[l][d]);
l += (1<<d);
}
d--;
}
return res;
*/
int d = lg[r-l+1];
return Secret(st[l][d], st[r-(1<<d)+1][d]);
}
};
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 |
2332 KB |
Wrong Answer: Query(222, 254) - expected : 34031541, actual : 839935006. |
2 |
Incorrect |
95 ms |
2336 KB |
Wrong Answer: Query(60, 375) - expected : 669221184, actual : 73044041. |
3 |
Incorrect |
102 ms |
2344 KB |
Wrong Answer: Query(211, 401) - expected : 674373968, actual : 951413062. |
4 |
Incorrect |
353 ms |
4400 KB |
Wrong Answer: Query(90, 497) - expected : 397934825, actual : 889372840. |
5 |
Incorrect |
350 ms |
4392 KB |
Wrong Answer: Query(587, 915) - expected : 752404486, actual : 389074008. |
6 |
Incorrect |
356 ms |
4288 KB |
Wrong Answer: Query(915, 915) - expected : 282904741, actual : 31281100. |
7 |
Incorrect |
351 ms |
4500 KB |
Wrong Answer: Query(84, 976) - expected : 742463504, actual : 709920818. |
8 |
Incorrect |
356 ms |
4432 KB |
Wrong Answer: Query(58, 987) - expected : 20022464, actual : 225603928. |
9 |
Incorrect |
354 ms |
4300 KB |
Wrong Answer: Query(33, 967) - expected : 676869696, actual : 819205962. |
10 |
Incorrect |
354 ms |
4400 KB |
Wrong Answer: Query(116, 961) - expected : 68487362, actual : 538241930. |