답안 #910153

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
910153 2024-01-18T00:28:18 Z bobbilyking 송신탑 (IOI22_towers) C++17
37 / 100
4000 ms 7504 KB
#include "towers.h"
#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")

#include<bits/stdc++.h>
#include<math.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef vector<ll> vl;
#define FD(i, r, l) for(ll i = r; i > (l); --i)

#define K first
#define V second
#define G(x) ll x; cin >> x;
#define GD(x) ld x; cin >> x;
#define GS(s) string s; cin >> s;
#define EX(x) { cout << x << '\n'; exit(0); }
#define A(a) (a).begin(), (a).end()
#define F(i, l, r) for (ll i = l; i < (r); ++i)

#define NN 100010
#define M 1000000007 // 998244353

ll h[NN];
ll n;

namespace seg {
    typedef pl T;
    T id={-1, -1};
    T f(T a, T b) {return pair(max(a.K, b.K), max(a.V, b.V)) ;}

    T t[2 * NN];
    ll n=NN;  // array size

    void init(ll p, ll value) {  // set value at position p
      for (p+=n, t[p].K = value; p /= 2;) t[p] = f(t[2*p], t[2*p+1]);
    }

    void modify(ll p, ll value) {  // set value at position p
      for (p+=n, t[p].V = max(t[p].V, value); p /= 2;) t[p] = f(t[2*p], t[2*p+1]);
    }

    T query(ll l, ll r) { // fold f on interval [l, r)
      T resl=id, resr=id;
      for (l += n, r += n; l < r; l /= 2, r /= 2) {
        if (l&1) resl = f(resl, t[l++]);
        if (r&1) resr = f(t[--r], resr);
      }
      return f(resl, resr);
    }
}

int brute_force(int L, int R, int D) {
        // vl dp(n+1, -1);/
    F(i, 0, 2*NN) seg::t[i].V = -1;


    ll res = 0;
    // ll res2 = 0;
    F(i, L, R+1) {
        ll ans = 1;
        // ll ans2 = 1;
        {
            // for (ll cur = i-1; cur >= L; cur--) if (h[cur] >= D + h[i]) ans2 = max(ans2, dp[cur] + 1);

            ll l = L - 1, r = i + 1;
            while (l+1<r) {
                ll m = (l+r)/2;
                if (seg::query(m, i).K >= D + h[i]) l = m;
                else r = m;
            }

            // if (i == 5) {
            //     cout << L << " " << l+1 << " " << seg::query(L, l+1).V << " " << endl;
            //     cout << seg::query(l, l+1).V << endl;
            // }
            ans = max(ans, seg::query(L, l+1).V + 1);
        }
        {
            // for (ll cur = i+1; cur <= R; cur++) if (h[cur] >= D + h[i]) dp[cur] = max(dp[cur], ans2);

            ll l = i, r = R+1;
            while (l+1<r) {
                ll m = (l+r)/2;
                if (seg::query(i, m+1).K >= D + h[i]) r = m;
                else l = m;
            }
            seg::modify(r, ans); 

            // if (ans2 == 3) cout << "TURNEd 3 AT " << i << endl;
        }
        res = max(res, ans);
        // res2 = max(ans, ans2);
        // cout << "ARRAYS FOR  " << i << endl;
        // F(i, L, R+1) cout << dp[i] << " "; cout << endl;

        // F(i, L, R+1) cout << seg::query(i, i+1).V << " "; cout << endl;
        
    }
    // assert(res == res2);
    return res;
}

namespace segval {
    typedef ll T;
    T id=0;
    T f(T a, T b) {return a+b;}

    T t[2 * NN];
    ll n=NN;  // array size

    void modify(ll p, T value) {  // set value at position p
      for (p+=n, t[p] = value; p /= 2;) t[p] = f(t[2*p], t[2*p+1]);
    }

    T query(ll l, ll r) { // fold f on interval [l, r)
      T resl=id, resr=id;
      for (l += n, r += n; l < r; l /= 2, r /= 2) {
        if (l&1) resl = f(resl, t[l++]);
        if (r&1) resr = f(t[--r], resr);
      }
      return f(resl, resr);
    }
}
vl valleys;
void init(int N, std::vector<int> H) {
    n = N;
    F(i, 0, n) h[i] = H[i];

    F(i, 0, n) seg::init(i, H[i]);

    valleys.resize(n);
    F(i, 0, n) {
        valleys[i] = 1;
        if (i + 1 < n) valleys[i] &= h[i] < h[i+1];
        if (i > 0) valleys[i] &= h[i-1] > h[i];
        segval::modify(i, valleys[i]);
    }
}


void gen_graph(int L, int R, int D) {
    cout << "Query: " << L << " " << R << " " << D << endl;
    F(i, L, R+1) cout << h[i] << " "; cout << endl;

    vector<ll> peak(n), valley(n);
    F(i, L, R+1) {
        peak[i] = 1, valley[i] = 1;
        if (i > 0) {
            peak[i] &= h[i-1] < h[i];
            valley[i] &= h[i-1] > h[i];
        }
        if (i + 1 < n) {
            peak[i] &= h[i+1] < h[i];
            valley[i] &= h[i+1] > h[i];
        }

        // cout << h[i] << ": " << peak[i] << " " << valley[i] << endl;
    } 
    

    F(i, L, R+1) {
        if (valley[i]) {
            for (ll j = i + 1; j <= R; ++j) if (peak[j] and h[j] - h[i] >= D) {
                cout << "V" << i << " P" << j << endl; break;
            }
        } else if (peak[i]) {
            for (ll j = i + 1; j <= R; ++j) if (valley[j] and h[i] - h[j] >= D) {
                cout << "P" << i << " V" << j << endl; break;
            }
        }
    }
}

// if (valley[i]) {
//             for (ll l = i-1; l >= L; --l) if (peak[l] and h[l] - h[i] >= D) {
//                 cout << l << " " << i << endl; break;
//             }
//         }
//         for (ll r = i+1; r <= R; ++r) if (valley[r] and h[r] - h[i] >= D) {
//             cout << i << " " << r << endl; break;
//         }


int max_towers(int L, int R, int D) {
    if (L == R) return 1;
    if (D == 1) {
        if (!valleys[L] and h[L] < h[L+1]) segval::modify(L, 1);

        if (!valleys[R] and h[R] < h[R-1]) segval::modify(R, 1);

        int ans = segval::query(L, R+1);

        if (!valleys[R] and h[R] < h[R-1]) segval::modify(R, 0);

        if (!valleys[L] and h[L] < h[L+1]) segval::modify(L, 0);

        // ll temp = brute_force(L, R, D);
        // if (temp != ans) cout << L << " " << R << " " << D << ", " << temp << " " << ans << endl;
        // assert(temp == ans);
        return ans;
    }   
    // gen_graph(L, R, D);

    // cout << "Actual: " << brute_force(L, R, D) << endl;

    return brute_force(L, R, D);
}

Compilation message

towers.cpp: In function 'void gen_graph(int, int, int)':
towers.cpp:23:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   23 | #define F(i, l, r) for (ll i = l; i < (r); ++i)
      |                    ^~~
towers.cpp:148:5: note: in expansion of macro 'F'
  148 |     F(i, L, R+1) cout << h[i] << " "; cout << endl;
      |     ^
towers.cpp:148:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  148 |     F(i, L, R+1) cout << h[i] << " "; cout << endl;
      |                                       ^~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4019 ms 6252 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 3 ms 4952 KB Output is correct
3 Correct 3 ms 4952 KB Output is correct
4 Correct 3 ms 4952 KB Output is correct
5 Correct 3 ms 4952 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 3 ms 4952 KB Output is correct
8 Correct 2 ms 4952 KB Output is correct
9 Correct 2 ms 4952 KB Output is correct
10 Correct 2 ms 4952 KB Output is correct
11 Correct 2 ms 4952 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 2 ms 4440 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4440 KB Output is correct
16 Correct 2 ms 4440 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 2 ms 4440 KB Output is correct
21 Correct 1 ms 4440 KB Output is correct
22 Correct 2 ms 4440 KB Output is correct
23 Correct 2 ms 4440 KB Output is correct
24 Correct 1 ms 4440 KB Output is correct
25 Correct 3 ms 4952 KB Output is correct
26 Correct 6 ms 4952 KB Output is correct
27 Correct 5 ms 4952 KB Output is correct
28 Correct 5 ms 4952 KB Output is correct
29 Correct 5 ms 4952 KB Output is correct
30 Correct 6 ms 4952 KB Output is correct
31 Correct 6 ms 4952 KB Output is correct
32 Correct 6 ms 4952 KB Output is correct
33 Correct 6 ms 4952 KB Output is correct
34 Correct 6 ms 4952 KB Output is correct
35 Correct 6 ms 4952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 3 ms 4952 KB Output is correct
3 Correct 3 ms 4952 KB Output is correct
4 Correct 3 ms 4952 KB Output is correct
5 Correct 3 ms 4952 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 3 ms 4952 KB Output is correct
8 Correct 2 ms 4952 KB Output is correct
9 Correct 2 ms 4952 KB Output is correct
10 Correct 2 ms 4952 KB Output is correct
11 Correct 2 ms 4952 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 2 ms 4440 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4440 KB Output is correct
16 Correct 2 ms 4440 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 2 ms 4440 KB Output is correct
21 Correct 1 ms 4440 KB Output is correct
22 Correct 2 ms 4440 KB Output is correct
23 Correct 2 ms 4440 KB Output is correct
24 Correct 1 ms 4440 KB Output is correct
25 Correct 3 ms 4952 KB Output is correct
26 Correct 6 ms 4952 KB Output is correct
27 Correct 5 ms 4952 KB Output is correct
28 Correct 5 ms 4952 KB Output is correct
29 Correct 5 ms 4952 KB Output is correct
30 Correct 6 ms 4952 KB Output is correct
31 Correct 6 ms 4952 KB Output is correct
32 Correct 6 ms 4952 KB Output is correct
33 Correct 6 ms 4952 KB Output is correct
34 Correct 6 ms 4952 KB Output is correct
35 Correct 6 ms 4952 KB Output is correct
36 Correct 57 ms 6340 KB Output is correct
37 Correct 151 ms 7232 KB Output is correct
38 Correct 66 ms 7468 KB Output is correct
39 Correct 139 ms 7408 KB Output is correct
40 Correct 59 ms 7236 KB Output is correct
41 Correct 311 ms 7244 KB Output is correct
42 Correct 29 ms 7320 KB Output is correct
43 Correct 220 ms 7248 KB Output is correct
44 Correct 71 ms 7244 KB Output is correct
45 Correct 31 ms 7236 KB Output is correct
46 Correct 28 ms 7248 KB Output is correct
47 Correct 26 ms 7236 KB Output is correct
48 Correct 25 ms 7236 KB Output is correct
49 Correct 26 ms 7244 KB Output is correct
50 Correct 25 ms 7240 KB Output is correct
51 Correct 29 ms 7504 KB Output is correct
52 Correct 30 ms 7400 KB Output is correct
53 Correct 30 ms 7220 KB Output is correct
54 Correct 26 ms 7240 KB Output is correct
55 Correct 27 ms 7240 KB Output is correct
56 Correct 25 ms 7496 KB Output is correct
57 Correct 399 ms 7360 KB Output is correct
58 Correct 377 ms 7400 KB Output is correct
59 Correct 348 ms 7392 KB Output is correct
60 Correct 297 ms 7236 KB Output is correct
61 Correct 339 ms 7220 KB Output is correct
62 Correct 369 ms 7240 KB Output is correct
63 Correct 335 ms 7248 KB Output is correct
64 Correct 464 ms 7236 KB Output is correct
65 Correct 461 ms 7244 KB Output is correct
66 Correct 448 ms 7236 KB Output is correct
67 Correct 461 ms 7232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 469 ms 7256 KB Output is correct
2 Correct 622 ms 7248 KB Output is correct
3 Correct 612 ms 7236 KB Output is correct
4 Correct 556 ms 7236 KB Output is correct
5 Correct 565 ms 7240 KB Output is correct
6 Correct 577 ms 7380 KB Output is correct
7 Correct 549 ms 7244 KB Output is correct
8 Correct 512 ms 7232 KB Output is correct
9 Correct 573 ms 7240 KB Output is correct
10 Correct 574 ms 7236 KB Output is correct
11 Correct 554 ms 7400 KB Output is correct
12 Correct 600 ms 7248 KB Output is correct
13 Correct 571 ms 7240 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4440 KB Output is correct
16 Correct 1 ms 4440 KB Output is correct
17 Correct 25 ms 7396 KB Output is correct
18 Correct 26 ms 7244 KB Output is correct
19 Correct 27 ms 7240 KB Output is correct
20 Correct 28 ms 7220 KB Output is correct
21 Correct 26 ms 7244 KB Output is correct
22 Correct 26 ms 7232 KB Output is correct
23 Correct 25 ms 7244 KB Output is correct
24 Correct 25 ms 7332 KB Output is correct
25 Correct 25 ms 7236 KB Output is correct
26 Correct 29 ms 7248 KB Output is correct
27 Correct 1 ms 4440 KB Output is correct
28 Correct 2 ms 4440 KB Output is correct
29 Correct 1 ms 4440 KB Output is correct
30 Correct 1 ms 4440 KB Output is correct
31 Correct 1 ms 4436 KB Output is correct
32 Correct 2 ms 4440 KB Output is correct
33 Correct 1 ms 4440 KB Output is correct
34 Correct 1 ms 4440 KB Output is correct
35 Correct 2 ms 4440 KB Output is correct
36 Correct 2 ms 4440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4049 ms 5464 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 3 ms 4952 KB Output is correct
3 Correct 3 ms 4952 KB Output is correct
4 Correct 3 ms 4952 KB Output is correct
5 Correct 3 ms 4952 KB Output is correct
6 Correct 2 ms 4952 KB Output is correct
7 Correct 3 ms 4952 KB Output is correct
8 Correct 2 ms 4952 KB Output is correct
9 Correct 2 ms 4952 KB Output is correct
10 Correct 2 ms 4952 KB Output is correct
11 Correct 2 ms 4952 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 2 ms 4440 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4440 KB Output is correct
16 Correct 2 ms 4440 KB Output is correct
17 Correct 1 ms 4440 KB Output is correct
18 Correct 1 ms 4440 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 2 ms 4440 KB Output is correct
21 Correct 1 ms 4440 KB Output is correct
22 Correct 2 ms 4440 KB Output is correct
23 Correct 2 ms 4440 KB Output is correct
24 Correct 1 ms 4440 KB Output is correct
25 Correct 3 ms 4952 KB Output is correct
26 Correct 6 ms 4952 KB Output is correct
27 Correct 5 ms 4952 KB Output is correct
28 Correct 5 ms 4952 KB Output is correct
29 Correct 5 ms 4952 KB Output is correct
30 Correct 6 ms 4952 KB Output is correct
31 Correct 6 ms 4952 KB Output is correct
32 Correct 6 ms 4952 KB Output is correct
33 Correct 6 ms 4952 KB Output is correct
34 Correct 6 ms 4952 KB Output is correct
35 Correct 6 ms 4952 KB Output is correct
36 Correct 57 ms 6340 KB Output is correct
37 Correct 151 ms 7232 KB Output is correct
38 Correct 66 ms 7468 KB Output is correct
39 Correct 139 ms 7408 KB Output is correct
40 Correct 59 ms 7236 KB Output is correct
41 Correct 311 ms 7244 KB Output is correct
42 Correct 29 ms 7320 KB Output is correct
43 Correct 220 ms 7248 KB Output is correct
44 Correct 71 ms 7244 KB Output is correct
45 Correct 31 ms 7236 KB Output is correct
46 Correct 28 ms 7248 KB Output is correct
47 Correct 26 ms 7236 KB Output is correct
48 Correct 25 ms 7236 KB Output is correct
49 Correct 26 ms 7244 KB Output is correct
50 Correct 25 ms 7240 KB Output is correct
51 Correct 29 ms 7504 KB Output is correct
52 Correct 30 ms 7400 KB Output is correct
53 Correct 30 ms 7220 KB Output is correct
54 Correct 26 ms 7240 KB Output is correct
55 Correct 27 ms 7240 KB Output is correct
56 Correct 25 ms 7496 KB Output is correct
57 Correct 399 ms 7360 KB Output is correct
58 Correct 377 ms 7400 KB Output is correct
59 Correct 348 ms 7392 KB Output is correct
60 Correct 297 ms 7236 KB Output is correct
61 Correct 339 ms 7220 KB Output is correct
62 Correct 369 ms 7240 KB Output is correct
63 Correct 335 ms 7248 KB Output is correct
64 Correct 464 ms 7236 KB Output is correct
65 Correct 461 ms 7244 KB Output is correct
66 Correct 448 ms 7236 KB Output is correct
67 Correct 461 ms 7232 KB Output is correct
68 Correct 469 ms 7256 KB Output is correct
69 Correct 622 ms 7248 KB Output is correct
70 Correct 612 ms 7236 KB Output is correct
71 Correct 556 ms 7236 KB Output is correct
72 Correct 565 ms 7240 KB Output is correct
73 Correct 577 ms 7380 KB Output is correct
74 Correct 549 ms 7244 KB Output is correct
75 Correct 512 ms 7232 KB Output is correct
76 Correct 573 ms 7240 KB Output is correct
77 Correct 574 ms 7236 KB Output is correct
78 Correct 554 ms 7400 KB Output is correct
79 Correct 600 ms 7248 KB Output is correct
80 Correct 571 ms 7240 KB Output is correct
81 Correct 1 ms 4440 KB Output is correct
82 Correct 1 ms 4440 KB Output is correct
83 Correct 1 ms 4440 KB Output is correct
84 Correct 25 ms 7396 KB Output is correct
85 Correct 26 ms 7244 KB Output is correct
86 Correct 27 ms 7240 KB Output is correct
87 Correct 28 ms 7220 KB Output is correct
88 Correct 26 ms 7244 KB Output is correct
89 Correct 26 ms 7232 KB Output is correct
90 Correct 25 ms 7244 KB Output is correct
91 Correct 25 ms 7332 KB Output is correct
92 Correct 25 ms 7236 KB Output is correct
93 Correct 29 ms 7248 KB Output is correct
94 Correct 1 ms 4440 KB Output is correct
95 Correct 2 ms 4440 KB Output is correct
96 Correct 1 ms 4440 KB Output is correct
97 Correct 1 ms 4440 KB Output is correct
98 Correct 1 ms 4436 KB Output is correct
99 Correct 2 ms 4440 KB Output is correct
100 Correct 1 ms 4440 KB Output is correct
101 Correct 1 ms 4440 KB Output is correct
102 Correct 2 ms 4440 KB Output is correct
103 Correct 2 ms 4440 KB Output is correct
104 Execution timed out 4089 ms 6948 KB Time limit exceeded
105 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4019 ms 6252 KB Time limit exceeded
2 Halted 0 ms 0 KB -