Submission #144512

# Submission time Handle Problem Language Result Execution time Memory
144512 2019-08-17T01:36:23 Z 12tqian Global Warming (CEOI18_glo) C++14
100 / 100
1185 ms 24264 KB
#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

const double PI = 4 * atan(1);

#define sz(x) (int)(x).size()
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define vi vector<int>
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define vpi vector<pair<int, int>>
#define vpd vector<pair<double, double>>
#define pd pair<double, double>

#define f0r(i,a) for(int i=0;i<a;i++)
#define f1r(i,a,b) for(int i=a;i<b;i++)
#define trav(a, x) for (auto& a : x)

template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
  cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}

void fast_io(){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}
void io(string taskname){
    string fin = taskname + ".in";
    string fout = taskname + ".out";
    const char* FIN = fin.c_str();
    const char* FOUT = fout.c_str();
    freopen(FIN, "r", stdin);
    freopen(FOUT, "w", stdout);
    fast_io();
}

const int MAX = 2e5+5;
vi t;
int inc[MAX];
int de[MAX];
unordered_map<int, int> um;

template<class T, int SZ> struct Seg { // SZ should be power of 2
    T ID = 0; // comb(ID,b) must equal b
    T comb(T a, T b) { return max(a, b); } // easily change this to min or max

    T seg[2*SZ];
    void reset(){
        f0r(i, 2*SZ) seg[i] = 0;
    }
    void upd(int p, T value) {  // set value at position p
        seg[p += SZ] = value;
        for (p /= 2; p; p /= 2) seg[p] = comb(seg[2*p],seg[2*p+1]);
    }

    T query(int l, int r) {  // sum on interval [l, r]
        r ++; T lres = ID, rres = ID; // make sure non-commutative operations work
        for (l += SZ, r += SZ; l < r; l /= 2, r /= 2) {
            if (l&1) lres = comb(lres,seg[l++]);
            if (r&1) rres = comb(seg[--r],rres);
        }
        return comb(lres,rres);
    }
};

Seg<int, (1<<18)> s;
Tree<int> vals;

int main(){
    fast_io();
    int n, x;
    cin >> n >> x;
    f0r(i, n){
        int ti;
        cin >> ti;
        t.eb(ti);
        vals.insert(ti);
    }
    int tmp = 0;
    for(auto x: vals){
        um[x] = tmp;
        tmp++;
    }
    f0r(i, n){
        int idx = um[t[i]];
        if(idx>0) inc[i] = 1 + s.query(0, idx-1);
        else inc[i] = 1;
        s.upd(idx, max(s.query(idx, idx), inc[i]));
    }
    s.reset();
    for(int i = n-1; i>= 0; i--){
        int idx = um[t[i]];
        de[i] = 1+s.query(idx+1, MAX);
        s.upd(idx, max(s.query(idx, idx), de[i]));
    }
    s.reset();
    int ans = 0;
    f0r(i, n){
        int mx = t[i] + x;
        int it = vals.order_of_key(mx);
        if(it>0) ans = max(ans, de[i] + s.query(0, it-1));
        else ans = max(ans, de[i]);
        int idx = um[t[i]];
        s.upd(idx, max(s.query(idx, idx), inc[i]));
    }
    s.reset();
    for(int i = n-1; i>=0; i--){
        int mn = t[i] -x+1;
        int it = vals.order_of_key(mn);
        ans = max(ans, inc[i] + s.query(it, MAX));
        int idx = um[t[i]];
        s.upd(idx, max(s.query(idx, idx), de[i]));
    }
    cout << ans << endl;
    return 0;
}

Compilation message

glo.cpp:1:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(linker, "/stack:200000000")
 
glo.cpp: In function 'void io(std::__cxx11::string)':
glo.cpp:52:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen(FIN, "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~
glo.cpp:53:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen(FOUT, "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2424 KB Output is correct
2 Correct 4 ms 2424 KB Output is correct
3 Correct 4 ms 2424 KB Output is correct
4 Correct 4 ms 2424 KB Output is correct
5 Correct 4 ms 2552 KB Output is correct
6 Correct 4 ms 2424 KB Output is correct
7 Correct 4 ms 2424 KB Output is correct
8 Correct 4 ms 2424 KB Output is correct
9 Correct 4 ms 2424 KB Output is correct
10 Correct 4 ms 2424 KB Output is correct
11 Correct 4 ms 2424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2424 KB Output is correct
2 Correct 4 ms 2424 KB Output is correct
3 Correct 4 ms 2424 KB Output is correct
4 Correct 4 ms 2424 KB Output is correct
5 Correct 4 ms 2552 KB Output is correct
6 Correct 4 ms 2424 KB Output is correct
7 Correct 4 ms 2424 KB Output is correct
8 Correct 4 ms 2424 KB Output is correct
9 Correct 4 ms 2424 KB Output is correct
10 Correct 4 ms 2424 KB Output is correct
11 Correct 4 ms 2424 KB Output is correct
12 Correct 4 ms 2424 KB Output is correct
13 Correct 4 ms 2424 KB Output is correct
14 Correct 5 ms 2424 KB Output is correct
15 Correct 5 ms 2472 KB Output is correct
16 Correct 4 ms 2380 KB Output is correct
17 Correct 4 ms 2424 KB Output is correct
18 Correct 4 ms 2424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2424 KB Output is correct
2 Correct 4 ms 2424 KB Output is correct
3 Correct 4 ms 2424 KB Output is correct
4 Correct 4 ms 2424 KB Output is correct
5 Correct 4 ms 2552 KB Output is correct
6 Correct 4 ms 2424 KB Output is correct
7 Correct 4 ms 2424 KB Output is correct
8 Correct 4 ms 2424 KB Output is correct
9 Correct 4 ms 2424 KB Output is correct
10 Correct 4 ms 2424 KB Output is correct
11 Correct 4 ms 2424 KB Output is correct
12 Correct 4 ms 2424 KB Output is correct
13 Correct 4 ms 2424 KB Output is correct
14 Correct 5 ms 2424 KB Output is correct
15 Correct 5 ms 2472 KB Output is correct
16 Correct 4 ms 2380 KB Output is correct
17 Correct 4 ms 2424 KB Output is correct
18 Correct 4 ms 2424 KB Output is correct
19 Correct 5 ms 2552 KB Output is correct
20 Correct 5 ms 2552 KB Output is correct
21 Correct 5 ms 2552 KB Output is correct
22 Correct 5 ms 2552 KB Output is correct
23 Correct 5 ms 2424 KB Output is correct
24 Correct 5 ms 2424 KB Output is correct
25 Correct 5 ms 2424 KB Output is correct
26 Correct 5 ms 2552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1006 ms 22176 KB Output is correct
2 Correct 1068 ms 22296 KB Output is correct
3 Correct 1185 ms 22176 KB Output is correct
4 Correct 1002 ms 22176 KB Output is correct
5 Correct 353 ms 13416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 7312 KB Output is correct
2 Correct 160 ms 7268 KB Output is correct
3 Correct 149 ms 7368 KB Output is correct
4 Correct 71 ms 5236 KB Output is correct
5 Correct 4 ms 2424 KB Output is correct
6 Correct 68 ms 5360 KB Output is correct
7 Correct 123 ms 7844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 287 ms 12400 KB Output is correct
2 Correct 414 ms 12340 KB Output is correct
3 Correct 713 ms 22292 KB Output is correct
4 Correct 364 ms 13532 KB Output is correct
5 Correct 177 ms 12340 KB Output is correct
6 Correct 368 ms 21280 KB Output is correct
7 Correct 329 ms 21280 KB Output is correct
8 Correct 231 ms 13388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2424 KB Output is correct
2 Correct 4 ms 2424 KB Output is correct
3 Correct 4 ms 2424 KB Output is correct
4 Correct 4 ms 2424 KB Output is correct
5 Correct 4 ms 2552 KB Output is correct
6 Correct 4 ms 2424 KB Output is correct
7 Correct 4 ms 2424 KB Output is correct
8 Correct 4 ms 2424 KB Output is correct
9 Correct 4 ms 2424 KB Output is correct
10 Correct 4 ms 2424 KB Output is correct
11 Correct 4 ms 2424 KB Output is correct
12 Correct 4 ms 2424 KB Output is correct
13 Correct 4 ms 2424 KB Output is correct
14 Correct 5 ms 2424 KB Output is correct
15 Correct 5 ms 2472 KB Output is correct
16 Correct 4 ms 2380 KB Output is correct
17 Correct 4 ms 2424 KB Output is correct
18 Correct 4 ms 2424 KB Output is correct
19 Correct 5 ms 2552 KB Output is correct
20 Correct 5 ms 2552 KB Output is correct
21 Correct 5 ms 2552 KB Output is correct
22 Correct 5 ms 2552 KB Output is correct
23 Correct 5 ms 2424 KB Output is correct
24 Correct 5 ms 2424 KB Output is correct
25 Correct 5 ms 2424 KB Output is correct
26 Correct 5 ms 2552 KB Output is correct
27 Correct 1006 ms 22176 KB Output is correct
28 Correct 1068 ms 22296 KB Output is correct
29 Correct 1185 ms 22176 KB Output is correct
30 Correct 1002 ms 22176 KB Output is correct
31 Correct 353 ms 13416 KB Output is correct
32 Correct 145 ms 7312 KB Output is correct
33 Correct 160 ms 7268 KB Output is correct
34 Correct 149 ms 7368 KB Output is correct
35 Correct 71 ms 5236 KB Output is correct
36 Correct 4 ms 2424 KB Output is correct
37 Correct 68 ms 5360 KB Output is correct
38 Correct 123 ms 7844 KB Output is correct
39 Correct 287 ms 12400 KB Output is correct
40 Correct 414 ms 12340 KB Output is correct
41 Correct 713 ms 22292 KB Output is correct
42 Correct 364 ms 13532 KB Output is correct
43 Correct 177 ms 12340 KB Output is correct
44 Correct 368 ms 21280 KB Output is correct
45 Correct 329 ms 21280 KB Output is correct
46 Correct 231 ms 13388 KB Output is correct
47 Correct 411 ms 13300 KB Output is correct
48 Correct 414 ms 13264 KB Output is correct
49 Correct 1052 ms 24260 KB Output is correct
50 Correct 362 ms 14676 KB Output is correct
51 Correct 227 ms 13780 KB Output is correct
52 Correct 395 ms 23560 KB Output is correct
53 Correct 395 ms 23436 KB Output is correct
54 Correct 355 ms 24264 KB Output is correct
55 Correct 726 ms 24192 KB Output is correct