답안 #736063

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
736063 2023-05-05T07:27:18 Z alireza_kaviani Fish 2 (JOI22_fish2) C++17
100 / 100
2686 ms 59184 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define all(x)      (x).begin(),(x).end()
#define X           first
#define Y           second
#define sep         ' '
#define endl        '\n'
#define SZ(x)       ll(x.size())
#define lc          id << 1
#define rc          lc | 1

const ll MAXN = 1e5 + 10;
const ll LOG = 22;
const ll INF = 1e18;
const ll MOD = 1e9 + 7; //998244353; //1e9 + 9;

struct Data{
    ll dp, sum, nxt, cnt;

    Data(ll dp, ll sum, ll nxt, ll cnt){
        this->dp = dp;
        this->sum = sum;
        this->nxt = nxt;
        this->cnt = cnt;
    }
};

struct Node{
    bool empty;
    vector<Data> pref, suff;
    
    Node(){
        empty = true;
    }

    Node(int x, int prv, int nxt){
        empty = false;
        pref.push_back(Data(x, x, nxt, 1));
        suff.push_back(Data(x, x, prv, 1));
    }

    void Print(){
        return;
        cout << "Empty: " << empty << endl;
        for(Data i : pref){
            cout << i.dp << ' ' << i.cnt << ' ' << i.sum << ' ' << i.nxt << endl;
        }
        cout << "****" << endl;
        for(Data i : suff){
            cout << i.dp << ' ' << i.cnt << ' ' << i.sum << ' ' << i.nxt << endl;
        }
        cout << "=====" << endl;
    }
};

ll n, q, cnt = 0, A[MAXN];
Node seg[MAXN << 2];

ll calc(const vector<Data> &L, const vector<Data> &R, int n, int m){
    int ptr = 0, res = 0;
    for(int i = 0; i < n; i++){
        res += L[i].cnt;
        while(ptr < m && L[i].sum >= R[ptr].dp){
            ptr++;
        }
        if(i + 1 != n && (ptr == 0 || R[ptr - 1].sum + L[i].sum < L[i].nxt)){
            res = 0;
        }
    }
    return (ptr == m ? res : 0);
}

vector<Data> Merge(const vector<Data> &prefL, const vector<Data> &suffL, const vector<Data> &prefR){
    Data A = prefL.back();
    vector<Data> ans = prefL;
    int flag = 1;
    if(A.sum >= A.nxt){
        ans.pop_back();
        flag = 0;
    }
    ll cntR = 0;
    for(int i = 0; i < prefR.size(); i++){
        Data B = prefR[i];
        if(B.sum >= suffL.back().dp){
            cntR += B.cnt;
        }
        if(A.sum + B.sum >= B.nxt && i + 1 != SZ(prefR)){
            continue;
        }
        ll dp = max(A.dp, B.dp - A.sum);
        ll sum = min(INF, A.sum + B.sum);
        ll cnt = cntR;
        if(flag == 0){
            cnt = calc(suffL, prefR, SZ(suffL), i + 1);
            cnt += calc(prefR, suffL, i + 1, SZ(suffL));
        }
        ans.push_back(Data(dp, sum, B.nxt, cnt));
        cntR = 0;
        flag = 1;
    }
    return ans;
}

Node Merge(Node L, Node R){
    if(L.empty) return R;
    if(R.empty) return L;
    Node ans; ans.empty = false;
    ans.pref = Merge(L.pref, L.suff, R.pref);
    ans.suff = Merge(R.suff, R.pref, L.suff);
    L.Print();
    R.Print();
    ans.Print();
    return ans;
}

void build(int id = 1, int l = 1, int r = n + 1){
    if(r - l == 1){
        seg[id] = Node(A[l], A[l - 1], A[l + 1]);
        return;
    }
    int mid = l + r >> 1;
    build(lc, l, mid);
    build(rc, mid, r);
    seg[id] = Merge(seg[lc], seg[rc]);
}

void modify(int pos, int id = 1, int l = 1, int r = n + 1){
    if(r - l == 1){
        seg[id] = Node(A[l], A[l - 1], A[l + 1]);
        return;
    }
    int mid = l + r >> 1;
    if(pos < mid)   modify(pos, lc, l, mid);
    else    modify(pos, rc, mid, r);
    seg[id] = Merge(seg[lc], seg[rc]);
}

Node get(int ql, int qr, int id = 1, int l = 1, int r = n + 1){
    if(qr <= l || r <= ql)  return Node();
    if(ql <= l && r <= qr)  return seg[id];
    int mid = l + r >> 1;
    return Merge(get(ql, qr, lc, l, mid), get(ql, qr, rc, mid, r));
}

int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> A[i];
    }
    build();
    cin >> q;
    while(q--){
        int t;
        cin >> t;
        if(t == 1){
            int x, y;
            cin >> x >> y;
            A[x] = y;
            if(x != 1)
                modify(x - 1);
            modify(x);
            if(x != n)
                modify(x + 1);
        }
        if(t == 2){
            int l, r;
            cin >> l >> r;
            r++;
            Node ans = get(l, r);
            cout << ans.pref.back().cnt << endl;
        }
    }

    return 0;
}
/*

*/

Compilation message

fish2.cpp: In function 'std::vector<Data> Merge(const std::vector<Data>&, const std::vector<Data>&, const std::vector<Data>&)':
fish2.cpp:87:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |     for(int i = 0; i < prefR.size(); i++){
      |                    ~~^~~~~~~~~~~~~~
fish2.cpp: In function 'void build(int, int, int)':
fish2.cpp:126:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  126 |     int mid = l + r >> 1;
      |               ~~^~~
fish2.cpp: In function 'void modify(int, int, int, int)':
fish2.cpp:137:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  137 |     int mid = l + r >> 1;
      |               ~~^~~
fish2.cpp: In function 'Node get(int, int, int, int, int)':
fish2.cpp:146:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  146 |     int mid = l + r >> 1;
      |               ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 22228 KB Output is correct
2 Correct 11 ms 22228 KB Output is correct
3 Correct 11 ms 22228 KB Output is correct
4 Correct 12 ms 22228 KB Output is correct
5 Correct 16 ms 22484 KB Output is correct
6 Correct 14 ms 22484 KB Output is correct
7 Correct 16 ms 22372 KB Output is correct
8 Correct 14 ms 22372 KB Output is correct
9 Correct 14 ms 22356 KB Output is correct
10 Correct 13 ms 22356 KB Output is correct
11 Correct 12 ms 22356 KB Output is correct
12 Correct 14 ms 22384 KB Output is correct
13 Correct 13 ms 22320 KB Output is correct
14 Correct 15 ms 22376 KB Output is correct
15 Correct 15 ms 22400 KB Output is correct
16 Correct 14 ms 22420 KB Output is correct
17 Correct 14 ms 22356 KB Output is correct
18 Correct 14 ms 22380 KB Output is correct
19 Correct 14 ms 22356 KB Output is correct
20 Correct 13 ms 22384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 22228 KB Output is correct
2 Correct 90 ms 56516 KB Output is correct
3 Correct 92 ms 54096 KB Output is correct
4 Correct 91 ms 56424 KB Output is correct
5 Correct 110 ms 54316 KB Output is correct
6 Correct 85 ms 46884 KB Output is correct
7 Correct 99 ms 46280 KB Output is correct
8 Correct 86 ms 46848 KB Output is correct
9 Correct 85 ms 46432 KB Output is correct
10 Correct 117 ms 56924 KB Output is correct
11 Correct 88 ms 51876 KB Output is correct
12 Correct 82 ms 45900 KB Output is correct
13 Correct 81 ms 45764 KB Output is correct
14 Correct 87 ms 47764 KB Output is correct
15 Correct 88 ms 48420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 22228 KB Output is correct
2 Correct 11 ms 22228 KB Output is correct
3 Correct 11 ms 22228 KB Output is correct
4 Correct 12 ms 22228 KB Output is correct
5 Correct 16 ms 22484 KB Output is correct
6 Correct 14 ms 22484 KB Output is correct
7 Correct 16 ms 22372 KB Output is correct
8 Correct 14 ms 22372 KB Output is correct
9 Correct 14 ms 22356 KB Output is correct
10 Correct 13 ms 22356 KB Output is correct
11 Correct 12 ms 22356 KB Output is correct
12 Correct 14 ms 22384 KB Output is correct
13 Correct 13 ms 22320 KB Output is correct
14 Correct 15 ms 22376 KB Output is correct
15 Correct 15 ms 22400 KB Output is correct
16 Correct 14 ms 22420 KB Output is correct
17 Correct 14 ms 22356 KB Output is correct
18 Correct 14 ms 22380 KB Output is correct
19 Correct 14 ms 22356 KB Output is correct
20 Correct 13 ms 22384 KB Output is correct
21 Correct 11 ms 22228 KB Output is correct
22 Correct 90 ms 56516 KB Output is correct
23 Correct 92 ms 54096 KB Output is correct
24 Correct 91 ms 56424 KB Output is correct
25 Correct 110 ms 54316 KB Output is correct
26 Correct 85 ms 46884 KB Output is correct
27 Correct 99 ms 46280 KB Output is correct
28 Correct 86 ms 46848 KB Output is correct
29 Correct 85 ms 46432 KB Output is correct
30 Correct 117 ms 56924 KB Output is correct
31 Correct 88 ms 51876 KB Output is correct
32 Correct 82 ms 45900 KB Output is correct
33 Correct 81 ms 45764 KB Output is correct
34 Correct 87 ms 47764 KB Output is correct
35 Correct 88 ms 48420 KB Output is correct
36 Correct 105 ms 56652 KB Output is correct
37 Correct 123 ms 54256 KB Output is correct
38 Correct 102 ms 54256 KB Output is correct
39 Correct 109 ms 56708 KB Output is correct
40 Correct 123 ms 54276 KB Output is correct
41 Correct 98 ms 46940 KB Output is correct
42 Correct 97 ms 46992 KB Output is correct
43 Correct 111 ms 46668 KB Output is correct
44 Correct 92 ms 46468 KB Output is correct
45 Correct 113 ms 57084 KB Output is correct
46 Correct 105 ms 57320 KB Output is correct
47 Correct 95 ms 50796 KB Output is correct
48 Correct 118 ms 46060 KB Output is correct
49 Correct 84 ms 45916 KB Output is correct
50 Correct 98 ms 47876 KB Output is correct
51 Correct 117 ms 48608 KB Output is correct
52 Correct 92 ms 47816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 22228 KB Output is correct
2 Correct 90 ms 56516 KB Output is correct
3 Correct 92 ms 54096 KB Output is correct
4 Correct 91 ms 56424 KB Output is correct
5 Correct 110 ms 54316 KB Output is correct
6 Correct 85 ms 46884 KB Output is correct
7 Correct 99 ms 46280 KB Output is correct
8 Correct 86 ms 46848 KB Output is correct
9 Correct 85 ms 46432 KB Output is correct
10 Correct 117 ms 56924 KB Output is correct
11 Correct 88 ms 51876 KB Output is correct
12 Correct 82 ms 45900 KB Output is correct
13 Correct 81 ms 45764 KB Output is correct
14 Correct 87 ms 47764 KB Output is correct
15 Correct 88 ms 48420 KB Output is correct
16 Correct 11 ms 22228 KB Output is correct
17 Correct 895 ms 56064 KB Output is correct
18 Correct 810 ms 58532 KB Output is correct
19 Correct 881 ms 56188 KB Output is correct
20 Correct 925 ms 55772 KB Output is correct
21 Correct 928 ms 55884 KB Output is correct
22 Correct 761 ms 58448 KB Output is correct
23 Correct 898 ms 55680 KB Output is correct
24 Correct 921 ms 55900 KB Output is correct
25 Correct 887 ms 55992 KB Output is correct
26 Correct 900 ms 56040 KB Output is correct
27 Correct 527 ms 48792 KB Output is correct
28 Correct 543 ms 48904 KB Output is correct
29 Correct 547 ms 48812 KB Output is correct
30 Correct 686 ms 47932 KB Output is correct
31 Correct 681 ms 47832 KB Output is correct
32 Correct 840 ms 53224 KB Output is correct
33 Correct 880 ms 59184 KB Output is correct
34 Correct 841 ms 52980 KB Output is correct
35 Correct 812 ms 51964 KB Output is correct
36 Correct 843 ms 58956 KB Output is correct
37 Correct 510 ms 47392 KB Output is correct
38 Correct 527 ms 47444 KB Output is correct
39 Correct 580 ms 49740 KB Output is correct
40 Correct 583 ms 50380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 22228 KB Output is correct
2 Correct 90 ms 56516 KB Output is correct
3 Correct 92 ms 54096 KB Output is correct
4 Correct 91 ms 56424 KB Output is correct
5 Correct 110 ms 54316 KB Output is correct
6 Correct 85 ms 46884 KB Output is correct
7 Correct 99 ms 46280 KB Output is correct
8 Correct 86 ms 46848 KB Output is correct
9 Correct 85 ms 46432 KB Output is correct
10 Correct 117 ms 56924 KB Output is correct
11 Correct 88 ms 51876 KB Output is correct
12 Correct 82 ms 45900 KB Output is correct
13 Correct 81 ms 45764 KB Output is correct
14 Correct 87 ms 47764 KB Output is correct
15 Correct 88 ms 48420 KB Output is correct
16 Correct 11 ms 22228 KB Output is correct
17 Correct 2116 ms 57920 KB Output is correct
18 Correct 1897 ms 56292 KB Output is correct
19 Correct 1591 ms 55744 KB Output is correct
20 Correct 1360 ms 56068 KB Output is correct
21 Correct 1994 ms 57696 KB Output is correct
22 Correct 1898 ms 56224 KB Output is correct
23 Correct 2037 ms 55596 KB Output is correct
24 Correct 1778 ms 56228 KB Output is correct
25 Correct 1786 ms 55392 KB Output is correct
26 Correct 1062 ms 48732 KB Output is correct
27 Correct 1506 ms 49064 KB Output is correct
28 Correct 1256 ms 51020 KB Output is correct
29 Correct 1207 ms 48652 KB Output is correct
30 Correct 1559 ms 48952 KB Output is correct
31 Correct 1578 ms 51016 KB Output is correct
32 Correct 1829 ms 54036 KB Output is correct
33 Correct 1478 ms 53052 KB Output is correct
34 Correct 1787 ms 58728 KB Output is correct
35 Correct 1335 ms 58668 KB Output is correct
36 Correct 1813 ms 53148 KB Output is correct
37 Correct 1656 ms 51136 KB Output is correct
38 Correct 1024 ms 50964 KB Output is correct
39 Correct 1166 ms 50176 KB Output is correct
40 Correct 501 ms 50208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 22228 KB Output is correct
2 Correct 11 ms 22228 KB Output is correct
3 Correct 11 ms 22228 KB Output is correct
4 Correct 12 ms 22228 KB Output is correct
5 Correct 16 ms 22484 KB Output is correct
6 Correct 14 ms 22484 KB Output is correct
7 Correct 16 ms 22372 KB Output is correct
8 Correct 14 ms 22372 KB Output is correct
9 Correct 14 ms 22356 KB Output is correct
10 Correct 13 ms 22356 KB Output is correct
11 Correct 12 ms 22356 KB Output is correct
12 Correct 14 ms 22384 KB Output is correct
13 Correct 13 ms 22320 KB Output is correct
14 Correct 15 ms 22376 KB Output is correct
15 Correct 15 ms 22400 KB Output is correct
16 Correct 14 ms 22420 KB Output is correct
17 Correct 14 ms 22356 KB Output is correct
18 Correct 14 ms 22380 KB Output is correct
19 Correct 14 ms 22356 KB Output is correct
20 Correct 13 ms 22384 KB Output is correct
21 Correct 11 ms 22228 KB Output is correct
22 Correct 90 ms 56516 KB Output is correct
23 Correct 92 ms 54096 KB Output is correct
24 Correct 91 ms 56424 KB Output is correct
25 Correct 110 ms 54316 KB Output is correct
26 Correct 85 ms 46884 KB Output is correct
27 Correct 99 ms 46280 KB Output is correct
28 Correct 86 ms 46848 KB Output is correct
29 Correct 85 ms 46432 KB Output is correct
30 Correct 117 ms 56924 KB Output is correct
31 Correct 88 ms 51876 KB Output is correct
32 Correct 82 ms 45900 KB Output is correct
33 Correct 81 ms 45764 KB Output is correct
34 Correct 87 ms 47764 KB Output is correct
35 Correct 88 ms 48420 KB Output is correct
36 Correct 105 ms 56652 KB Output is correct
37 Correct 123 ms 54256 KB Output is correct
38 Correct 102 ms 54256 KB Output is correct
39 Correct 109 ms 56708 KB Output is correct
40 Correct 123 ms 54276 KB Output is correct
41 Correct 98 ms 46940 KB Output is correct
42 Correct 97 ms 46992 KB Output is correct
43 Correct 111 ms 46668 KB Output is correct
44 Correct 92 ms 46468 KB Output is correct
45 Correct 113 ms 57084 KB Output is correct
46 Correct 105 ms 57320 KB Output is correct
47 Correct 95 ms 50796 KB Output is correct
48 Correct 118 ms 46060 KB Output is correct
49 Correct 84 ms 45916 KB Output is correct
50 Correct 98 ms 47876 KB Output is correct
51 Correct 117 ms 48608 KB Output is correct
52 Correct 92 ms 47816 KB Output is correct
53 Correct 11 ms 22228 KB Output is correct
54 Correct 895 ms 56064 KB Output is correct
55 Correct 810 ms 58532 KB Output is correct
56 Correct 881 ms 56188 KB Output is correct
57 Correct 925 ms 55772 KB Output is correct
58 Correct 928 ms 55884 KB Output is correct
59 Correct 761 ms 58448 KB Output is correct
60 Correct 898 ms 55680 KB Output is correct
61 Correct 921 ms 55900 KB Output is correct
62 Correct 887 ms 55992 KB Output is correct
63 Correct 900 ms 56040 KB Output is correct
64 Correct 527 ms 48792 KB Output is correct
65 Correct 543 ms 48904 KB Output is correct
66 Correct 547 ms 48812 KB Output is correct
67 Correct 686 ms 47932 KB Output is correct
68 Correct 681 ms 47832 KB Output is correct
69 Correct 840 ms 53224 KB Output is correct
70 Correct 880 ms 59184 KB Output is correct
71 Correct 841 ms 52980 KB Output is correct
72 Correct 812 ms 51964 KB Output is correct
73 Correct 843 ms 58956 KB Output is correct
74 Correct 510 ms 47392 KB Output is correct
75 Correct 527 ms 47444 KB Output is correct
76 Correct 580 ms 49740 KB Output is correct
77 Correct 583 ms 50380 KB Output is correct
78 Correct 11 ms 22228 KB Output is correct
79 Correct 2116 ms 57920 KB Output is correct
80 Correct 1897 ms 56292 KB Output is correct
81 Correct 1591 ms 55744 KB Output is correct
82 Correct 1360 ms 56068 KB Output is correct
83 Correct 1994 ms 57696 KB Output is correct
84 Correct 1898 ms 56224 KB Output is correct
85 Correct 2037 ms 55596 KB Output is correct
86 Correct 1778 ms 56228 KB Output is correct
87 Correct 1786 ms 55392 KB Output is correct
88 Correct 1062 ms 48732 KB Output is correct
89 Correct 1506 ms 49064 KB Output is correct
90 Correct 1256 ms 51020 KB Output is correct
91 Correct 1207 ms 48652 KB Output is correct
92 Correct 1559 ms 48952 KB Output is correct
93 Correct 1578 ms 51016 KB Output is correct
94 Correct 1829 ms 54036 KB Output is correct
95 Correct 1478 ms 53052 KB Output is correct
96 Correct 1787 ms 58728 KB Output is correct
97 Correct 1335 ms 58668 KB Output is correct
98 Correct 1813 ms 53148 KB Output is correct
99 Correct 1656 ms 51136 KB Output is correct
100 Correct 1024 ms 50964 KB Output is correct
101 Correct 1166 ms 50176 KB Output is correct
102 Correct 501 ms 50208 KB Output is correct
103 Correct 2686 ms 55308 KB Output is correct
104 Correct 1973 ms 58332 KB Output is correct
105 Correct 1117 ms 56412 KB Output is correct
106 Correct 1025 ms 56396 KB Output is correct
107 Correct 2391 ms 55656 KB Output is correct
108 Correct 1973 ms 58424 KB Output is correct
109 Correct 1453 ms 55672 KB Output is correct
110 Correct 1271 ms 56128 KB Output is correct
111 Correct 1114 ms 55996 KB Output is correct
112 Correct 1008 ms 56088 KB Output is correct
113 Correct 1348 ms 48920 KB Output is correct
114 Correct 653 ms 48916 KB Output is correct
115 Correct 1703 ms 51084 KB Output is correct
116 Correct 1254 ms 51024 KB Output is correct
117 Correct 767 ms 48864 KB Output is correct
118 Correct 864 ms 50068 KB Output is correct
119 Correct 1557 ms 48940 KB Output is correct
120 Correct 1585 ms 51104 KB Output is correct
121 Correct 1123 ms 51044 KB Output is correct
122 Correct 1885 ms 54144 KB Output is correct
123 Correct 1461 ms 53596 KB Output is correct
124 Correct 1123 ms 52420 KB Output is correct
125 Correct 897 ms 51480 KB Output is correct
126 Correct 970 ms 52224 KB Output is correct
127 Correct 1618 ms 51488 KB Output is correct
128 Correct 717 ms 49520 KB Output is correct
129 Correct 1324 ms 50400 KB Output is correct
130 Correct 899 ms 50336 KB Output is correct