# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
20584 | kdh9949 | 역사적 조사 (JOI14_historical) | C++14 | 2966 ms | 6804 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;
typedef long long ll;
const int sz = 131072, b = 320;
int n, q, a[100010], xx[100010];
ll ans[100010];
struct Query{
int s, e, idx;
bool operator<(const Query &oth) const {
return (s / b == oth.s / b) ? e < oth.e : s < oth.s;
}
} qa[100010];
struct Seg{
ll dat[2 * sz];
void init(){
for(int i = 1; i < 2 * sz; i++) dat[i] = 0;
}
void upd(int x, int v){
x += sz - 1; dat[x] += v * xx[x - sz + 1];
for(x /= 2; x; x /= 2) dat[x] = max(dat[2 * x], dat[2 * x + 1]);
}
ll get(){ return dat[1]; }
} S;
int main(){
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; i++){ scanf("%d", a + i); xx[i] = a[i]; }
sort(xx + 1, xx + n + 1);
for(int i = 1; i <= n; i++) a[i] = (int)(lower_bound(xx + 1, xx + n + 1, a[i]) - xx);
for(int i = 1; i <= q; i++){ scanf("%d%d", &qa[i].s, &qa[i].e); qa[i].idx = i; }
sort(qa + 1, qa + q + 1);
qa[0].s = 1; qa[0].e = 0;
S.init();
for(int i = 1; i <= q; i++){
int ls = qa[i - 1].s, le = qa[i - 1].e;
int cs = qa[i].s, ce = qa[i].e;
for(; ls < cs; ) S.upd(a[ls++], -1);
for(; ls > cs; ) S.upd(a[--ls], 1);
for(; le < ce; ) S.upd(a[++le], 1);
for(; le > ce; ) S.upd(a[le--], -1);
ans[qa[i].idx] = S.get();
}
for(int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
}
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... |