Submission #519524

# Submission time Handle Problem Language Result Execution time Memory
519524 2022-01-26T14:16:42 Z Dasha_Gnedko Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++17
100 / 100
2777 ms 224880 KB
#include <bits/stdc++.h>

//#include <ext/pb_ds/detail/standard_policies.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

using namespace std;

//using namespace __gnu_pbds;
//template <typename T> using ordered_set = tree <T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update>;

mt19937 gen(time(0));

#define ll long long
#define ld long double
#define pb push_back
#define F first
#define S second
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define sz(a) int32_t(a.size())
#define endl '\n'
//#define int long long

const int N = 1e6 + 100;
const int M = 31;
const int mod = 998244353;
const int inf = 2e9 + 100;

int a[N];

struct DO
{
    vector < vector < int > > t;
    vector < pair < int, int > > li;
    vector < int > ans;
    int pr = 2;

    void build(int n)
    {
        while (pr < n) pr *= 2;
        t.resize(2 * pr);
        ans.resize(2 * pr, 0);
        for(int i = pr; i < pr + n; i++)
            t[i].pb(a[i - pr]);
        for(int v = pr - 1; v > 0; v--)
        {
            ans[v] = max(ans[v * 2], ans[v * 2 + 1]);
            for(auto to: t[v * 2 + 1])
            {
                if (t[v * 2].back() <= to) break;
                ans[v] = max(ans[v], t[v * 2].back() + to);
            }
            int i = 0, j = 0;
            while (i < sz(t[v * 2]) || j < sz(t[v * 2 + 1]))
            {
                if (j == sz(t[v * 2 + 1]) || (i < sz(t[v * 2]) && t[v * 2][i] <= t[v * 2 + 1][j])) t[v].pb(t[v * 2][i++]);
                else t[v].pb(t[v * 2 + 1][j++]);
            }
        }
    }

    void get_ans(int l, int r, int v, int cl, int cr)
    {
        if (cr < l || r < cl) return;
        if (l <= cl && cr <= r)
        {
            li.pb({v, ans[v]});
            return;
        }
        int mid = (cl + cr) / 2;
        get_ans(l, r, v * 2, cl, mid);
        get_ans(l, r, v * 2 + 1, mid + 1, cr);
    }

    int get(int l, int r, int k)
    {
        li.clear();
        get_ans(l, r, 1, 0, pr - 1);
        int ans = 0;
        for(auto to: li)
            ans = max(ans, to.S);
        if (ans > k) return 0;
        int mx = 0;
        for(int i = 0; i < sz(li) - 1; i++)
        {
            int v = li[i].F, u = li[i + 1].F;
            mx = max(mx, t[v].back());
            if (t[u].back() + mx <= k) continue;
            int l = 0, r = sz(t[u]) - 1;
            while (l <= r)
            {
                int mid = (l + r) / 2;
                if (t[u][mid] >= mx) r = mid - 1;
                else
                {
                    ans = max(ans, mx + t[u][mid]);
                    l = mid + 1;
                }
            }
            if (ans > k) return 0;
        }
        return 1;
    }
};

DO t;

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif // LOCAL

    int n, q;
    cin >> n >> q;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    t.build(n);
    while (q--)
    {
        int l, r, k;
        cin >> l >> r >> k;
        l--; r--;
        cout << t.get(l, r, k) << endl;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 4 ms 1356 KB Output is correct
13 Correct 4 ms 1356 KB Output is correct
14 Correct 6 ms 1468 KB Output is correct
15 Correct 6 ms 1476 KB Output is correct
16 Correct 7 ms 1592 KB Output is correct
17 Correct 4 ms 956 KB Output is correct
18 Correct 5 ms 1356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1370 ms 195328 KB Output is correct
2 Correct 1404 ms 198568 KB Output is correct
3 Correct 1311 ms 196728 KB Output is correct
4 Correct 1276 ms 198636 KB Output is correct
5 Correct 2777 ms 196664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 22016 KB Output is correct
2 Correct 115 ms 22024 KB Output is correct
3 Correct 155 ms 22072 KB Output is correct
4 Correct 164 ms 22080 KB Output is correct
5 Correct 181 ms 22076 KB Output is correct
6 Correct 118 ms 21976 KB Output is correct
7 Correct 98 ms 21992 KB Output is correct
8 Correct 109 ms 21808 KB Output is correct
9 Correct 36 ms 1832 KB Output is correct
10 Correct 94 ms 21812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 4 ms 1356 KB Output is correct
13 Correct 4 ms 1356 KB Output is correct
14 Correct 6 ms 1468 KB Output is correct
15 Correct 6 ms 1476 KB Output is correct
16 Correct 7 ms 1592 KB Output is correct
17 Correct 4 ms 956 KB Output is correct
18 Correct 5 ms 1356 KB Output is correct
19 Correct 285 ms 44580 KB Output is correct
20 Correct 305 ms 44532 KB Output is correct
21 Correct 264 ms 44532 KB Output is correct
22 Correct 262 ms 44676 KB Output is correct
23 Correct 253 ms 44620 KB Output is correct
24 Correct 273 ms 44556 KB Output is correct
25 Correct 282 ms 44584 KB Output is correct
26 Correct 382 ms 44524 KB Output is correct
27 Correct 356 ms 44528 KB Output is correct
28 Correct 374 ms 44528 KB Output is correct
29 Correct 373 ms 44620 KB Output is correct
30 Correct 369 ms 44552 KB Output is correct
31 Correct 421 ms 44588 KB Output is correct
32 Correct 375 ms 44628 KB Output is correct
33 Correct 384 ms 44508 KB Output is correct
34 Correct 232 ms 44604 KB Output is correct
35 Correct 238 ms 44668 KB Output is correct
36 Correct 241 ms 44512 KB Output is correct
37 Correct 243 ms 45384 KB Output is correct
38 Correct 254 ms 45752 KB Output is correct
39 Correct 248 ms 45536 KB Output is correct
40 Correct 207 ms 28436 KB Output is correct
41 Correct 249 ms 45152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 4 ms 1356 KB Output is correct
13 Correct 4 ms 1356 KB Output is correct
14 Correct 6 ms 1468 KB Output is correct
15 Correct 6 ms 1476 KB Output is correct
16 Correct 7 ms 1592 KB Output is correct
17 Correct 4 ms 956 KB Output is correct
18 Correct 5 ms 1356 KB Output is correct
19 Correct 1370 ms 195328 KB Output is correct
20 Correct 1404 ms 198568 KB Output is correct
21 Correct 1311 ms 196728 KB Output is correct
22 Correct 1276 ms 198636 KB Output is correct
23 Correct 2777 ms 196664 KB Output is correct
24 Correct 115 ms 22016 KB Output is correct
25 Correct 115 ms 22024 KB Output is correct
26 Correct 155 ms 22072 KB Output is correct
27 Correct 164 ms 22080 KB Output is correct
28 Correct 181 ms 22076 KB Output is correct
29 Correct 118 ms 21976 KB Output is correct
30 Correct 98 ms 21992 KB Output is correct
31 Correct 109 ms 21808 KB Output is correct
32 Correct 36 ms 1832 KB Output is correct
33 Correct 94 ms 21812 KB Output is correct
34 Correct 285 ms 44580 KB Output is correct
35 Correct 305 ms 44532 KB Output is correct
36 Correct 264 ms 44532 KB Output is correct
37 Correct 262 ms 44676 KB Output is correct
38 Correct 253 ms 44620 KB Output is correct
39 Correct 273 ms 44556 KB Output is correct
40 Correct 282 ms 44584 KB Output is correct
41 Correct 382 ms 44524 KB Output is correct
42 Correct 356 ms 44528 KB Output is correct
43 Correct 374 ms 44528 KB Output is correct
44 Correct 373 ms 44620 KB Output is correct
45 Correct 369 ms 44552 KB Output is correct
46 Correct 421 ms 44588 KB Output is correct
47 Correct 375 ms 44628 KB Output is correct
48 Correct 384 ms 44508 KB Output is correct
49 Correct 232 ms 44604 KB Output is correct
50 Correct 238 ms 44668 KB Output is correct
51 Correct 241 ms 44512 KB Output is correct
52 Correct 243 ms 45384 KB Output is correct
53 Correct 254 ms 45752 KB Output is correct
54 Correct 248 ms 45536 KB Output is correct
55 Correct 207 ms 28436 KB Output is correct
56 Correct 249 ms 45152 KB Output is correct
57 Correct 1677 ms 212712 KB Output is correct
58 Correct 1549 ms 212944 KB Output is correct
59 Correct 1562 ms 213872 KB Output is correct
60 Correct 1560 ms 213916 KB Output is correct
61 Correct 1527 ms 213800 KB Output is correct
62 Correct 1527 ms 213848 KB Output is correct
63 Correct 1414 ms 224872 KB Output is correct
64 Correct 1506 ms 224880 KB Output is correct
65 Correct 2390 ms 214232 KB Output is correct
66 Correct 2399 ms 213900 KB Output is correct
67 Correct 2436 ms 213944 KB Output is correct
68 Correct 2396 ms 213048 KB Output is correct
69 Correct 2412 ms 212868 KB Output is correct
70 Correct 2374 ms 212988 KB Output is correct
71 Correct 2466 ms 213048 KB Output is correct
72 Correct 2419 ms 213048 KB Output is correct
73 Correct 1216 ms 223304 KB Output is correct
74 Correct 1264 ms 224344 KB Output is correct
75 Correct 1234 ms 223344 KB Output is correct
76 Correct 1307 ms 223380 KB Output is correct
77 Correct 1206 ms 223456 KB Output is correct
78 Correct 1354 ms 220652 KB Output is correct
79 Correct 1010 ms 121648 KB Output is correct
80 Correct 1253 ms 217888 KB Output is correct