이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
int n, q, d;
struct MaxTree{
vector<int> tree;
vector<int> d;
vector<bool> act;
MaxTree(vector<int>&v){
int m =1;
while(m<v.size()){
m*=2;
}
m*=2;
tree.resize(m);
d.resize(m);
act.resize(m, false);
build(v, 0, v.size()-1, 1);
}
void build(vector<int>& v, int lt, int rt, int t){
if(rt ==lt){
tree[t] = v[lt];
d[t] = v[lt];
}
else{
int mid = (lt+rt)/2;
build(v, lt, mid, t*2);
build(v, mid+1, rt, t*2+1);
tree[t] = max(tree[t*2], tree[t*2+1]);
}
}
void push(int t, int lt, int rt){
if(act[t] && lt!=rt){
d[t*2] =d[t];
d[t*2+1] =d[t];
tree[t*2] = d[t];
tree[t*2+1] = d[t];
act[t*2] = true;
act[t*2+1] = true;
act[t] = false;
}
}
void update(int l, int r, int v, int lt, int rt, int t){
if(l<=lt && rt<=r){
d[t] = v;
tree[t] = v;
act[t] =true;
}
else if(r<lt || rt<l){
return;
}
else{
int mid = (lt+rt)/2;
push(t, lt, rt);
update(l, r, v, lt, mid, t*2);
update(l, r, v, mid+1, rt, t*2+1);
tree[t] = max(tree[t*2], tree[t*2+1]);
}
}
int get(int l, int r, int lt, int rt, int t){
////cout<<lt<<" "<<rt<<" "<<tree[t]<<endl;
if(l<=lt && rt<=r){
return tree[t];
}
else if(rt<l|| r<lt){
return 0;
}
else{
int mid = (lt+rt)/2;
push(t, lt, rt);
return max(get(l, r, lt, mid, t*2), get(l, r, mid+1, rt, t*2+1));
}
}
};
vector<pii> oc;
signed main(){
cin>>n>>d;
vector<int> v(n);
for(int i = 0; i<n; i++){
cin>>v[i];
}
vector<int> big(n);
map<int, int> small;
for(int i = 0; i<n; i++){
cin>>v[i];
big[i] = v[i];
}
set<int> big2(big.begin(), big.end());
big.assign(big2.begin(), big2.end());
for(int i = 0; i<big.size(); i++){
small.insert(pii(big[i], i));
////cout<<big[i]<<endl;
}
auto ref = vector<int>(big.size(), 0);
MaxTree max_tr = MaxTree(ref);
deque<pii> rem;
int bs =big.size();
for(int i = 0; i<n; i++){
oc.clear();
if(rem.size()>0 && rem.front().first<i-d){
////cout<<"removing date "<<set_tr.tree[1]<<endl;
////cout<<"i "<<i<<" removing "<<rem[0].second<<endl;
if(rem.size()==1){
max_tr.update(0, bs-1, 0, 0, bs-1, 1);
//cout<<"all"<<endl;
}
else{
max_tr.update(0, rem[1].second-1, 0, 0, bs-1, 1);
//cout<<"i "<<i<<" erasing "<<big[0]<<" "<<big[rem[1].second-1]<<endl;
}
//cout<<"er"<<endl;
rem.pop_front();
}
//cout<<"i: "<<i+1<<" "<<max(1LL, max_tr.get(0, small[v[i]]-1, 0, bs-1, 1)+1)<<endl;
max_tr.update(small[v[i]], small[v[i]], max(max_tr.get(0, small[v[i]], 0, bs-1, 1),max(1LL, max_tr.get(0, small[v[i]]-1, 0, bs-1, 1)+1LL)), 0, bs-1, 1);
while(rem.size()>0 && rem.back().second>=small[v[i]]){
//cout<<rem.back().first<<" "<<big[rem.back().second]<<" useless "<<endl;
rem.pop_back();
}
rem.push_back(pii(i, small[v[i]]));
}
cout<<max_tr.get(0, bs-1, 0, bs-1, 1)<<endl;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In constructor 'MaxTree::MaxTree(std::vector<long long int>&)':
Main.cpp:16:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | while(m<v.size()){
| ~^~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:104:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int i = 0; i<big.size(); i++){
| ~^~~~~~~~~~~
# | 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... |