# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
261039 | onjo0127 | 역사적 조사 (JOI14_historical) | C++11 | 316 ms | 8048 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>
using namespace std;
using pii = pair<int, int>;
const int bck = 320;
struct query {
int s, e, id;
};
int N, X[100009], I[100009];
long long ans[100009], sum[100009], mx;
vector<query> L[333];
void add(int x) {
sum[I[x]] += X[x];
mx = max(mx, sum[I[x]]);
}
void del(int x) {
sum[I[x]] -= X[x];
}
int main() {
vector<int> P;
int Q; scanf("%d%d", &N, &Q);
for(int i=1; i<=N; i++) {
scanf("%d", &X[i]);
P.push_back(X[i]);
}
sort(P.begin(), P.end());
P.resize(unique(P.begin(), P.end()) - P.begin());
for(int i=1; i<=N; i++) I[i] = lower_bound(P.begin(), P.end(), X[i]) - P.begin() + 1;
for(int i=1; i<=Q; i++) {
int s, e; scanf("%d%d", &s, &e);
L[s / bck].push_back({s, e, i});
}
for(int i=0; i<=N/bck; i++) sort(L[i].begin(), L[i].end(), [&](query PP, query QQ) { return PP.e < QQ.e; });
for(int i=0; i<=N/bck; i++) {
mx = 0;
int ql = i*bck, qr = min((i+1)*bck - 1, N), rm = qr;
for(auto& it: L[i]) {
int s = it.s, e = it.e;
if(s/bck == e/bck) {
mx = 0;
for(int i=s; i<=e; i++) add(i);
ans[it.id] = mx;
for(int i=s; i<=e; i++) del(i);
mx = 0;
}
else {
while(rm < e) add(++rm);
long long tmp = mx;
for(int i=s; i<=qr; i++) add(i);
ans[it.id] = mx;
for(int i=s; i<=qr; i++) del(i);
mx = tmp;
}
}
for(int i=rm; i>qr; i--) del(i);
}
for(int i=1; i<=Q; i++) printf("%lld\n", ans[i]);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |