이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 7;
const int M = 8e5 + 7;
const int inf = 1e18 + 7;
// bool debug_mode = 0;
struct MergeSortTree{
vector < int > tree[M];
void build(vector < int > &v , int ind=1 , int l=1 , int r=N){
for(int i = l-1;i<r;i++){
tree[ind].push_back(v[i]);
}
sort(tree[ind].begin() , tree[ind].end());
if(l != r){
int mid = (l+r) >> 1;
build(v , ind*2 , l , mid);
build(v , ind*2+1 , mid+1 , r);
}
}
inline int query(int ind , int val){
if(ind < 0){
cout << "0" << endl;
exit(0);
}
auto it = lower_bound(tree[ind].begin() , tree[ind].end() , val);
int res = -1;
if(it != tree[ind].begin()){
res = *(--it);
}
// if(debug_mode){
// cout << "v : ";for(auto itr : tree[ind])cout << itr << " ";cout << endl;
// cout << "val : " << val << endl;
// cout << "res : " << res << endl;
// }
return res;
}
} mst;
struct SegmentTree{
struct Node{
int mx , indx;
long long ans;
Node():mx(0) , ans(-inf) , indx(0){}
} tree[M];
Node merge(Node &a , Node &b){
Node c;
c.mx = max(a.mx , b.mx);
int tmp = mst.query(b.indx , a.mx);
if(tmp != -1){
c.ans = max({a.ans , b.ans , (long long)a.mx + (long long)tmp});
}
else {
c.ans = max(a.ans , b.ans);
}
return c;
}
void build(vector < int > &v , int ind=1 , int l=1 , int r=N){
for(int i = l-1;i<r;i++){
tree[ind].mx = max(tree[ind].mx , v[i]);
}
if(l != r){
int mid = (l+r) >> 1;
build(v , ind*2 , l , mid);
build(v , ind*2+1 , mid+1 , r);
tree[ind] = merge(tree[ind*2] , tree[ind*2+1]);
}
tree[ind].indx = ind;
// cout << l << " " << r << " => " << tree[ind].mx << " " << tree[ind].ans << endl;
}
vector < Node > vec;
void dfs(int ql , int qr , int ind=1 , int l=1 , int r=N){
if(l >= ql and r <= qr){
// cout << l << " " << r << " : " << tree[ind].mx << " " << tree[ind].ans << endl;
vec.push_back(tree[ind]);
}
else if(l > qr or r < ql){
return;
}
else{
int mid = (l+r) >> 1;
dfs(ql , qr , ind*2 , l , mid);
dfs(ql , qr , ind*2+1 , mid+1 , r);
}
}
int query(int l , int r){
Node ret;
// debug_mode = 1;
for(auto itr : vec){
ret = merge(ret , itr);
// cout << ret.mx << " " << ret.ans << endl;
}
// debug_mode = 0;
vec.clear();
// cout << "query : " << ret.ans << endl;
return ret.ans;
}
} segt;
void solve(){
int n,m;
cin >> n >> m;
vector < int > arr(n);
for(int i = 0;i<n;i++){
cin >> arr[i];
}
mst.build(arr,1,1,arr.size());
segt.build(arr,1,1,arr.size());
// cout << "---" << endl;
for(int qt = 0;qt<m;qt++){
int l,r,k;
cin >> l >> r >> k;
segt.dfs(l,r,1,1,arr.size());
cout << (segt.query(l,r) <= k) << '\n';
// cout << "---" << endl;
}
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase = 1;//cin >> testcase;
while(testcase--)solve();
}
컴파일 시 표준 에러 (stderr) 메시지
sortbooks.cpp: In constructor 'SegmentTree::Node::Node()':
sortbooks.cpp:42:19: warning: 'SegmentTree::Node::ans' will be initialized after [-Wreorder]
42 | long long ans;
| ^~~
sortbooks.cpp:41:18: warning: 'long long int SegmentTree::Node::indx' [-Wreorder]
41 | int mx , indx;
| ^~~~
sortbooks.cpp:43:9: warning: when initialized here [-Wreorder]
43 | Node():mx(0) , ans(-inf) , indx(0){}
| ^~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |