Submission #1268699

#TimeUsernameProblemLanguageResultExecution timeMemory
1268699herominhsteveGlobal Warming (CEOI18_glo)C++20
100 / 100
113 ms8416 KiB
#include <bits/stdc++.h>
#define el '\n'
#define FNAME "stellar"
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,n) memset(x,(n),sizeof(x))
using namespace std;
const long long MOD = (long long) 1e9+7;
template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;}
template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;}

void setup(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
	if (fopen(FNAME".inp","r")){
		freopen(FNAME".inp","r",stdin);
		freopen(FNAME".out","w",stdout);
	}
}

int n,X;
vector<long long> a;

void init(){
	cin>>n>>X;
    a.assign(n+1,0);
    for (int i=1;i<=n;i++) cin>>a[i];
}
template<typename T>
struct FenwickTree{
    int n;
    vector<T> bit;
    FenwickTree(int N=0){
        n=N;
        if (n>0){
            bit.resize(n+1,0);
        }
    }
    void update(int node,T val){
        while (node<=n){
            maximize(bit[node],val);
            node += (node & -node);
        }
    }
    T getVal(int node){
        T res=0;
        while (node>0){
            maximize(res,bit[node]);
            node -= (node & -node);
        }
        return res;
    }
};

void sol(){
    vector<long long> compress(allof1(a));
    for (int i=1;i<=n;i++) compress.push_back(a[i]+X);
    sort(allof(compress));
    compress.resize(unique(allof(compress))-compress.begin());
    FenwickTree<int> bitNor(compress.size()+1);
    FenwickTree<int> bitUp(compress.size()+1);

    function<int(long long)> getID = [&] (long long x) -> int {
        return lower_bound(allof(compress),x) - compress.begin() + 1;
    };

    int res = 1;
    for (int i=1;i<=n;i++){
        int curID = getID(a[i]);
        int curNor = bitNor.getVal(curID-1) + 1;
        int curUp = bitUp.getVal(curID-1) + 1; // ! Not changed
        // ! start changing here
        int addx = getID(a[i] + X);
        if (addx>=1) maximize(curUp,bitNor.getVal(addx-1)+1);
        bitNor.update(curID,curNor);
        bitUp.update(curID,curUp);
        maximize(res,max(curNor,curUp));
    }
    cout<<res;
}

int main(){
    setup();
    init();
    sol();
}

Compilation message (stderr)

glo.cpp: In function 'void setup()':
glo.cpp:16:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |                 freopen(FNAME".inp","r",stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:17:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |                 freopen(FNAME".out","w",stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...