#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
vector<vector<int>> lef, rig;
vector<int> v;
int n;
void Init(int N, int A[]) {
v.resize(N);
n = N;
REP(i, N) v[i] = A[i];
function<void(int, int, int)> solve = [&](int l, int r, int d) {
cerr << d << " " << l << ' ' << r << '\n';
if (r == l) {
return;
}
int mid = (l+r)/2;
if (lef.size() < d+1) {lef.resize(d+1); rig.resize(d+1); lef[d].resize(N); rig[d].resize(N);}
lef[d][mid] = v[mid];
for (int i = mid-1; i >= l; i--) {
lef[d][i] = Secret(v[i], lef[d][i+1]);
}
rig[d][mid+1] = v[mid+1];
FOR(i, mid+2, r) {
rig[d][i] = Secret(rig[d][i-1], v[i]);
}
solve(l, mid, d+1); solve(mid+1, r, d+1);
};
solve(0, N-1, 0);
debug(lef);
debug(rig);
}
int Query(int L, int R) {
int l = 0, r = n-1, d = 0;
if (L == R) return v[L];
while (l < r) {
int mid = (l+r)/2;
if (L <= mid && mid < R) return Secret(lef[d][L], rig[d][R]);
if (R <= mid) r = mid;
else l = mid+1;
d++;
}
return v[L];
}
#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
Compilation message
secret.cpp: In lambda function:
secret.cpp:37:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
37 | if (lef.size() < d+1) {lef.resize(d+1); rig.resize(d+1); lef[d].resize(N); rig[d].resize(N);}
| ~~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
2412 KB |
Output is correct - number of calls to Secret by Init = 3578, maximum number of calls to Secret by Query = 1 |
2 |
Correct |
116 ms |
2344 KB |
Output is correct - number of calls to Secret by Init = 3586, maximum number of calls to Secret by Query = 1 |
3 |
Correct |
116 ms |
2388 KB |
Output is correct - number of calls to Secret by Init = 3595, maximum number of calls to Secret by Query = 1 |
4 |
Correct |
397 ms |
4260 KB |
Output is correct - number of calls to Secret by Init = 7969, maximum number of calls to Secret by Query = 1 |
5 |
Correct |
391 ms |
4404 KB |
Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1 |
6 |
Correct |
388 ms |
4336 KB |
Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1 |
7 |
Correct |
388 ms |
4300 KB |
Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1 |
8 |
Correct |
404 ms |
4440 KB |
Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1 |
9 |
Correct |
394 ms |
4352 KB |
Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1 |
10 |
Correct |
393 ms |
4300 KB |
Output is correct - number of calls to Secret by Init = 7978, maximum number of calls to Secret by Query = 1 |