답안 #891315

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
891315 2023-12-22T18:13:33 Z SorahISA Financial Report (JOI21_financial) C++17
100 / 100
740 ms 61964 KB
#ifndef SorahISA
#define SorahISA
#include SorahISA __FILE__ SorahISA

struct SegTree {
    
    vector<int> seg;
    int maxn;
    
    void init(int N) {
        maxn = (1 << (__lg(N+1) + 1));
        seg.assign(maxn<<1, 0);
    }
    
    void pull(int now) {
        int lt = now << 1, rt = lt | 1;
        seg[now] = max(seg[lt], seg[rt]);
    }
    
    void modify_point_chmax(int qX, int val) {
        // debug(qX, val);
        chmax(seg[qX += maxn], val);
        while ((qX >>= 1)) pull(qX);
    }
    
    int query_range_max(int qL, int qR) {
        // debug(qL, qR);
        int ans = max(seg[qL += maxn], seg[qR += maxn]);
        while (qL + 1 < qR) {
            if (~qL & 1) chmax(ans, seg[qL ^ 1]);
            if ( qR & 1) chmax(ans, seg[qR ^ 1]);
            qL >>= 1, qR >>= 1;
        }
        return ans;
    }
    
};

void solve() {
    int N, D; cin >> N >> D;
    
    vector<int> A(N);
    map<int, vector<int>> mp;
    for (int i = 0; i < N; ++i) cin >> A[i], mp[A[i]].eb(i);
    sort(RALL(A)), A.erase(unique(ALL(A)), end(A));
    
    set<pii> st_all, st_large;
    
    auto print_set = [&](const set<pii> &st) {
        cerr << "\u001b[36m";
        cerr << "{";
        for (const auto &p : st) cerr << p << ", ";
        cerr << "}\n";
        cerr << "\u001b[0m";
    };
    
    auto check_large = [&](int x) -> bool {
        auto it = st_large.upper_bound({x, x});
        if (it != begin(st_large)) it = prev(it);
        if (it == end(st_large)) return false;
        return (it->first <= x and x <= it->second);
    };
    
    auto insert_point = [&](int x) {
        /// x is not in st_all ///
        auto it = st_all.ee(x, x).first;
        int l = x, r = x;
        /// if x+1 is in st_all, merge them ///
        if (next(it) != end(st_all) and next(it)->first == x+1) {
            r = next(it)->second;
            /// remove x+1 from st_large if exist ///
            if (check_large(r)) st_large.erase(*next(it));
            it = st_all.erase(it);      /// remove [x, x]
            it = st_all.erase(it);      /// remove [x+1, r]
            it = st_all.ee(l, r).first; /// insert [x, r]
        }
        /// if x-1 is in st_all, merge them ///
        if (it != begin(st_all) and prev(it)->second == x-1) {
            l = prev(it)->first;
            /// remove x-1 from st_large if exist ///
            if (check_large(l)) st_large.erase(*prev(it));
            it = st_all.erase(it);       /// remove [x, r]
            it = st_all.erase(prev(it)); /// remove [l, x-1]
            it = st_all.ee(l, r).first;  /// insert [l, r]
        }
        /// insert [l, r] into st_large if large enough ///
        if (r - l + 1 >= D) st_large.ee(l, r);
        // print_set(st_all);
        // print_set(st_large);
    };
    
    auto query_stop = [&](int x) -> int {
        /// x is not in st_large ///
        auto it = st_large.upper_bound({x, x});
        return (it != end(st_large) ? it->first + D - 1 : N - 1);
    };
    
    SegTree seg; seg.init(N);
    for (int val : A) {
        // debug(val);
        vector<int> L, R;
        for (int pos : mp[val]) {
            // debug(pos);
            L.eb(pos), R.eb(query_stop(pos)), insert_point(pos);
            seg.modify_point_chmax(pos, seg.query_range_max(L.back(), R.back()) + 1);
        }
        for (int i = SZ(L)-1; i >= 0; --i) {
            seg.modify_point_chmax(L[i], seg.query_range_max(L[i], R[i]));
        }
        // for (int i = 0; i < SZ(L); ++i) {
        //     int dp = seg.query_range_max(L[i], L[i]);
        //     debug("dp", L[i], dp);
        // }
    }
    cout << seg.query_range_max(0, N-1) << "\n";
    
    // vector<int> dp(N, 1);
    // for (int i = N-1; i >= 0; --i) {
    //     for (int j = i+1, cnt = 1; cnt <= D and j < N; ++j, ++cnt) {
    //              if (A[j] >  A[i]) chmax(dp[i], dp[j] + 1);
    //         else if (A[j] == A[i]) chmax(dp[i], dp[j]), cnt = 0;
    //         else if (A[j] <  A[i]) cnt = 0;
    //     }
    //     // debug(i, A[i], dp[i]);
    // }
    // cout << *max_element(ALL(dp)) << "\n";
}

int32_t main() {
    fastIO();
    
    int t = 1; // cin >> t;
    for (int _ = 1; _ <= t; ++_) {
        solve();
    }
    
    return 0;
}

#else

#ifdef local
#define _GLIBCXX_DEBUG 1
#endif
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;

using int64 = long long;
#define int int64
using float80 = long double;
#define double float80
using pii = pair<int, int>;
template <typename T> using Prior = std::priority_queue<T>;
template <typename T> using prior = std::priority_queue<T, vector<T>, greater<T>>;

#define eb emplace_back
#define ef emplace_front
#define ee emplace
#define pb pop_back
#define pf pop_front
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
#define SZ(x) ((int)(x).size())

ostream & operator << (ostream &os, const pii &p) {
    return os << "(" << p.first << ", " << p.second << ")";
}

#ifdef local
#define fastIO() void()
#define debug(...) \
    fprintf(stderr, "\u001b[33m"), \
    fprintf(stderr, "At [%s], line %d: (%s) = ", __FUNCTION__, __LINE__, #__VA_ARGS__), \
    _do(__VA_ARGS__), \
    fprintf(stderr, "\u001b[0m")
template <typename T> void _do(T &&_t) { cerr << _t << "\n"; }
template <typename T, typename ...U> void _do(T &&_t, U &&..._u) { cerr << _t << ", ", _do(_u...); }
#else
#define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)
#define debug(...) void()
#endif

template <typename T, typename U> bool chmin(T &lhs, U rhs) { return lhs > rhs ? lhs = rhs, 1 : 0; }
template <typename T, typename U> bool chmax(T &lhs, U rhs) { return lhs < rhs ? lhs = rhs, 1 : 0; }

#endif

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:49:10: warning: variable 'print_set' set but not used [-Wunused-but-set-variable]
   49 |     auto print_set = [&](const set<pii> &st) {
      |          ^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 8 ms 1036 KB Output is correct
42 Correct 8 ms 1496 KB Output is correct
43 Correct 4 ms 860 KB Output is correct
44 Correct 6 ms 860 KB Output is correct
45 Correct 7 ms 856 KB Output is correct
46 Correct 7 ms 1568 KB Output is correct
47 Correct 7 ms 1372 KB Output is correct
48 Correct 8 ms 1372 KB Output is correct
49 Correct 7 ms 1372 KB Output is correct
50 Correct 7 ms 1492 KB Output is correct
51 Correct 8 ms 1372 KB Output is correct
52 Correct 8 ms 1496 KB Output is correct
53 Correct 5 ms 1368 KB Output is correct
54 Correct 6 ms 1372 KB Output is correct
55 Correct 8 ms 1276 KB Output is correct
56 Correct 6 ms 1624 KB Output is correct
57 Correct 7 ms 1372 KB Output is correct
58 Correct 4 ms 1372 KB Output is correct
59 Correct 4 ms 1372 KB Output is correct
60 Correct 5 ms 1372 KB Output is correct
61 Correct 5 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 26504 KB Output is correct
2 Correct 225 ms 25808 KB Output is correct
3 Correct 341 ms 24860 KB Output is correct
4 Correct 740 ms 49728 KB Output is correct
5 Correct 499 ms 51284 KB Output is correct
6 Correct 740 ms 54100 KB Output is correct
7 Correct 233 ms 43644 KB Output is correct
8 Correct 218 ms 43600 KB Output is correct
9 Correct 252 ms 56400 KB Output is correct
10 Correct 227 ms 44564 KB Output is correct
11 Correct 513 ms 52304 KB Output is correct
12 Correct 558 ms 52304 KB Output is correct
13 Correct 455 ms 43924 KB Output is correct
14 Correct 722 ms 61964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 22976 KB Output is correct
2 Correct 212 ms 18260 KB Output is correct
3 Correct 333 ms 19724 KB Output is correct
4 Correct 533 ms 48464 KB Output is correct
5 Correct 470 ms 47852 KB Output is correct
6 Correct 542 ms 48252 KB Output is correct
7 Correct 256 ms 43856 KB Output is correct
8 Correct 206 ms 43860 KB Output is correct
9 Correct 259 ms 43604 KB Output is correct
10 Correct 385 ms 43976 KB Output is correct
11 Correct 489 ms 44184 KB Output is correct
12 Correct 344 ms 43860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 8 ms 1036 KB Output is correct
42 Correct 8 ms 1496 KB Output is correct
43 Correct 4 ms 860 KB Output is correct
44 Correct 6 ms 860 KB Output is correct
45 Correct 7 ms 856 KB Output is correct
46 Correct 7 ms 1568 KB Output is correct
47 Correct 7 ms 1372 KB Output is correct
48 Correct 8 ms 1372 KB Output is correct
49 Correct 7 ms 1372 KB Output is correct
50 Correct 7 ms 1492 KB Output is correct
51 Correct 8 ms 1372 KB Output is correct
52 Correct 8 ms 1496 KB Output is correct
53 Correct 5 ms 1368 KB Output is correct
54 Correct 6 ms 1372 KB Output is correct
55 Correct 8 ms 1276 KB Output is correct
56 Correct 6 ms 1624 KB Output is correct
57 Correct 7 ms 1372 KB Output is correct
58 Correct 4 ms 1372 KB Output is correct
59 Correct 4 ms 1372 KB Output is correct
60 Correct 5 ms 1372 KB Output is correct
61 Correct 5 ms 1372 KB Output is correct
62 Correct 117 ms 26504 KB Output is correct
63 Correct 225 ms 25808 KB Output is correct
64 Correct 341 ms 24860 KB Output is correct
65 Correct 740 ms 49728 KB Output is correct
66 Correct 499 ms 51284 KB Output is correct
67 Correct 740 ms 54100 KB Output is correct
68 Correct 233 ms 43644 KB Output is correct
69 Correct 218 ms 43600 KB Output is correct
70 Correct 252 ms 56400 KB Output is correct
71 Correct 227 ms 44564 KB Output is correct
72 Correct 513 ms 52304 KB Output is correct
73 Correct 558 ms 52304 KB Output is correct
74 Correct 455 ms 43924 KB Output is correct
75 Correct 722 ms 61964 KB Output is correct
76 Correct 105 ms 22976 KB Output is correct
77 Correct 212 ms 18260 KB Output is correct
78 Correct 333 ms 19724 KB Output is correct
79 Correct 533 ms 48464 KB Output is correct
80 Correct 470 ms 47852 KB Output is correct
81 Correct 542 ms 48252 KB Output is correct
82 Correct 256 ms 43856 KB Output is correct
83 Correct 206 ms 43860 KB Output is correct
84 Correct 259 ms 43604 KB Output is correct
85 Correct 385 ms 43976 KB Output is correct
86 Correct 489 ms 44184 KB Output is correct
87 Correct 344 ms 43860 KB Output is correct
88 Correct 443 ms 23376 KB Output is correct
89 Correct 677 ms 49748 KB Output is correct
90 Correct 438 ms 24120 KB Output is correct
91 Correct 550 ms 48464 KB Output is correct
92 Correct 197 ms 19340 KB Output is correct
93 Correct 317 ms 19600 KB Output is correct
94 Correct 514 ms 38768 KB Output is correct
95 Correct 580 ms 51944 KB Output is correct
96 Correct 570 ms 48572 KB Output is correct
97 Correct 578 ms 48536 KB Output is correct
98 Correct 544 ms 48420 KB Output is correct
99 Correct 553 ms 48724 KB Output is correct
100 Correct 559 ms 48724 KB Output is correct
101 Correct 255 ms 52052 KB Output is correct
102 Correct 269 ms 45392 KB Output is correct
103 Correct 288 ms 43672 KB Output is correct
104 Correct 343 ms 44172 KB Output is correct
105 Correct 422 ms 44372 KB Output is correct
106 Correct 362 ms 44284 KB Output is correct
107 Correct 412 ms 43996 KB Output is correct
108 Correct 500 ms 45900 KB Output is correct
109 Correct 370 ms 43772 KB Output is correct
110 Correct 512 ms 44452 KB Output is correct
111 Correct 458 ms 44112 KB Output is correct
112 Correct 371 ms 44116 KB Output is correct
113 Correct 473 ms 44112 KB Output is correct
114 Correct 464 ms 44252 KB Output is correct
115 Correct 229 ms 43880 KB Output is correct
116 Correct 235 ms 43648 KB Output is correct
117 Correct 228 ms 43756 KB Output is correct
118 Correct 235 ms 43788 KB Output is correct
119 Correct 274 ms 43860 KB Output is correct
120 Correct 274 ms 43876 KB Output is correct