# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
838160 | mat_jur | Secret (JOI14_secret) | C++14 | 402 ms | 4364 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
};
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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |