이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define int long long int
#define No_TOI_I_am_Garbage ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int n, a[500050], q;
vector<pair<int, int> > seg;
struct thing {
int left, right, v1, v2, lz;
} arr[500050 << 2];
void build(int left, int right, int m)
{
arr[m].left = left; arr[m].right = right;
arr[m].lz = 0;
if(left == right)
{
arr[m].v1 = arr[m].v2 = a[left];
return;
}
int mid = (left + right) >> 1;
build(left, mid, m * 2);
build(mid + 1, right, m * 2 + 1);
arr[m].v2 = max(arr[m * 2].v2, arr[m * 2 + 1].v2);
arr[m].v1 = arr[m].v2;
}
void down(int m)
{
arr[m * 2].v1 = max(arr[m * 2].v1, arr[m * 2].v2 + arr[m].lz);
arr[m * 2 + 1].v1 = max(arr[m * 2 + 1].v1, arr[m * 2 + 1].v2 + arr[m].lz);
arr[m * 2].lz = max(arr[m].lz, arr[m * 2].lz);
arr[m * 2 + 1].lz = max(arr[m].lz, arr[m * 2 + 1].lz);
}
void modify(int left, int right, int x, int m)
{
if(arr[m].left > right || arr[m].right < left) return;
if(left <= arr[m].left && arr[m].right <= right)
{
arr[m].lz = max(arr[m].lz, x);
arr[m].v1 = max(arr[m].v1, x + arr[m].v2);
return;
}
down(m);
modify(left, right, x, m * 2);
modify(left, right, x, m * 2 + 1);
arr[m].v1 = max(arr[m * 2].v1, arr[m * 2 + 1].v1);
arr[m].v2 = max(arr[m * 2].v2, arr[m * 2 + 1].v2);
return;
}
int search(int left, int right, int m)
{
if(arr[m].left > right || arr[m].right < left) return 0;
if(left <= arr[m].left && arr[m].right <= right)
return arr[m].v1;
down(m);
return max(search(left, right, m * 2), search(left, right, m * 2 + 1));
}
struct he {
int first, second, seq, ans;
} que[500050];
int32_t main()
{
No_TOI_I_am_Garbage
cin >> n;
for(int i=1; i <= n; i ++)
cin >> a[i];
deque<int> Q; Q.push_back(1);
for(int i=2; i <= n; i ++)
{
while(!Q.empty() && a[Q.back()] <= a[i])
{
int top = Q.back(); Q.pop_back();
seg.push_back(make_pair(top, i));
}
if(!Q.empty()) seg.push_back(make_pair(Q.back(), i));
Q.push_back(i);
}
sort(seg.begin(), seg.end(), [](pair<int, int> x, pair<int, int> y) {
return x.first > y.first;
});
cin >> q;
for(int i=0; i < q; i ++)
{
cin >> que[i].first >> que[i].second;
que[i].seq = i;
}
sort(que, que + q, [](he x, he y) {
return x.first > y.first;
});
build(1, n, 1);
int ind = 0;
for(int i=0; i < q; i ++)
{
while(ind < seg.size() && seg[ind].first >= que[i].first)
{
modify(seg[ind].second * 2 - seg[ind].first, n, a[seg[ind].first] + a[seg[ind].second], 1);
// cout << seg[ind].second * 2 - seg[ind].first << ' ' << a[seg[ind].first] + a[seg[ind].second] << ' ' << seg[ind].first << ' ' << seg[ind].second << '\n';
ind ++;
}
// cout << '\n';
que[i].ans = search(que[i].first, que[i].second, 1);
}
sort(que, que + q, [](he x, he y) {
return x.seq < y.seq;
});
for(int i=0; i < q; i ++)
cout << que[i].ans << '\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
jumps.cpp: In function 'int32_t main()':
jumps.cpp:92:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | while(ind < seg.size() && seg[ind].first >= que[i].first)
| ~~~~^~~~~~~~~~~~
# | 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... |