QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#814311#9877. Segment Treeucup-team2796#WA 7ms3972kbC++2311.8kb2024-12-14 16:41:282024-12-14 16:41:35

Judging History

你现在查看的是最新测评结果

  • [2024-12-14 16:41:35]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:3972kb
  • [2024-12-14 16:41:28]
  • 提交

answer

#line 1 "library/Template/template.hpp"
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}
#line 2 "library/Utility/fastio.hpp"
#include <unistd.h>
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf

uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio

using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */
#line 3 "sol.cpp"

int main() {
    int n;
    read(n);
    int m = 1 << n;
    vector<ll> C(m * 2);
    rep(i, 1, m * 2) read(C[i]);

    vector<ll> D(m * 2, INF);
    auto upd = [&](int i) -> void {
        for (;;) {
            D[i] = C[i];
            if (i < m)
                chmin(D[i], D[i * 2] + D[i * 2 + 1]);
            i >>= 1;
            if (i == 0)
                break;
        }
    };
    rrep(i, 1, m * 2) upd(i);
    show(C, D);
    int Q;
    read(Q);
    while (Q--) {
        int t;
        read(t);
        if (t == 1) {
            int j, x;
            read(j, x);
            C[j] = x;
            upd(j);
            show(C, D);
        } else {
            int L, R;
            read(L, R);

            int snode = L + m, tnode = R + m;
            ll Ls = 0, Rs = C[L + m];
            ll Lt, Rt;
            if (R == (1 << n)) {
                tnode--;
                Lt = C[R - 1 + m], Rt = 0;
            } else {
                Lt = 0, Rt = C[R + m];
            }
            ll ret = INF;
            for (;;) {
                if (snode == tnode) {
                    chmin(ret, Ls + D[snode] + Rt);
                }
                if (snode + 1 == tnode) {
                    chmin(ret, Rs + Lt);
                }

                int nxts = snode >> 1;
                if (nxts == 0)
                    break;
                if (snode == nxts * 2) {
                    ll nLs = min(Ls, Rs + D[nxts * 2 + 1] + D[nxts]);
                    ll nRs = min(Rs + D[nxts * 2 + 1], Ls + D[nxts]);
                    Ls = nLs, Rs = nRs;
                } else {
                    ll nLs = min(Ls + D[nxts * 2], Rs + D[nxts]);
                    ll nRs = min(Rs, Ls + D[nxts * 2] + D[nxts]);
                    Ls = nLs, Rs = nRs;
                }
                snode = nxts;

                int nxtt = tnode >> 1;
                if (tnode == nxtt * 2) {
                    ll nLt = min(Lt, Rt + D[nxtt * 2 + 1] + D[nxtt]);
                    ll nRt = min(Rt + D[nxtt * 2 + 1], Ls + D[nxtt]);
                    Lt = nLt, Rt = nRt;
                } else {
                    ll nLt = min(Lt + D[nxtt * 2], Rt + D[nxtt]);
                    ll nRt = min(Rt, Lt + D[nxtt * 2] + D[nxtt]);
                    Lt = nLt, Rt = nRt;
                }
                tnode = nxtt;
            }
            print(ret);
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3608kb

input:

3
7 1 14 3 9 4 8 2 6 5 5 13 8 2 3
10
2 0 1
2 0 4
2 4 6
2 4 8
2 3 5
1 6 30
2 3 5
2 4 6
1 1 10000000
2 0 8

output:

2
1
4
8
17
18
13
15

result:

ok 8 tokens

Test #2:

score: 0
Accepted
time: 3ms
memory: 3772kb

input:

1
7914575 2436426 4979445
199989
1 1 6190629
1 1 1407775
1 1 2804784
1 2 2631932
1 1 3078537
1 3 286918
1 2 3238506
1 3 3361868
1 2 9296263
1 3 4836991
1 3 2177068
1 3 4291757
1 1 594328
1 2 8996221
1 1 5531545
1 3 3575467
1 3 3206504
1 1 8344965
1 3 6045895
2 0 2
1 2 6248153
1 1 5797489
1 1 9766466...

output:

8344965
5684734
2756417
2512448
130126
7091295
7834895
6363152
6668726
4380822
8809904
4042733
8566868
8653391
3654574
7617913
8583126
4470761
4099069
2539201
7188565
8465921
4517278
1913351
7400947
5104744
1759308
6081288
3559555
3409112
3714298
8937580
4704960
5280672
9424416
1622556
2599805
18330...

result:

ok 1899 tokens

Test #3:

score: 0
Accepted
time: 7ms
memory: 3824kb

input:

1
3080713 6130142 8931932
199954
1 3 3859793
1 2 8302798
1 1 1363993
1 2 2817427
1 1 6031503
1 1 4197608
1 1 3453017
1 3 3258277
1 2 1243375
1 3 7997018
1 1 8659259
1 1 545422
1 1 1213295
1 2 9318329
1 2 1165990
1 1 3910911
1 2 9639614
1 2 3166127
1 1 2556789
1 1 2505213
2 1 2
1 1 8837030
1 1 996138...

output:

5671340
4103158
2278869
1251419
702774
1634200
9066441
3444042
4761391
1317349
996556
3444042
996556
996556
4884903
6746567
6746567
1389661
4920459
230651
935263
2028823
680623
1093324
1093324
680623
680623
369391
6136723
5192803
5192803
6136723
4301516
4578392
3566336
3566336
7599310
4756965
378391...

result:

ok 29717 tokens

Test #4:

score: 0
Accepted
time: 0ms
memory: 3972kb

input:

1
6313638 363583 8248153
199989
2 1 2
1 1 155990
1 2 4430056
2 0 2
2 1 2
1 1 6771887
1 1 9001299
2 0 1
1 3 2051074
2 1 2
2 0 1
1 1 3829876
2 0 1
1 3 8940076
2 1 2
2 0 1
2 0 2
2 0 2
1 1 2321211
1 2 8057327
2 0 2
1 1 553338
1 2 7877801
2 0 2
1 2 2505976
1 3 1153207
2 0 2
1 2 4561192
1 2 4540078
1 1 90...

output:

6677221
155990
4586046
4430056
2051074
4430056
4430056
8259932
4430056
3829876
3829876
2321211
553338
553338
5693285
4540078
4547501
5088001
1153207
3934794
1153207
3934794
3934794
3934794
7085662
3658305
3147631
3658305
6805936
3147631
3147631
853551
2267606
3727767
3727767
2645926
3727767
2645926
...

result:

ok 100061 tokens

Test #5:

score: -100
Wrong Answer
time: 7ms
memory: 3772kb

input:

2
4716625 8732769 4896438 9294402 7273885 4137152 2249944
199996
1 1 5186587
1 4 7722585
1 5 3539426
1 5 1298070
1 6 8806800
1 1 4206062
1 6 6971489
1 5 8825000
1 5 3448517
1 6 9944200
1 1 3672387
1 2 1617483
1 5 8197902
1 6 4298339
1 5 6260453
1 2 3666548
1 3 9334704
1 3 5244559
1 3 2160729
1 6 944...

output:

5334226
9572097
4807948
733673
8100027
6139742
6091424
5926345
8623714
12325259
6201853
1162428
2792985
6816822
9147939
8703527
5455802
2767961
4607887
7567091
1326121
3115123
3452276
7483661
3901199
2876292
3890889
78252
9798360
2638886
2415998
6562024
7215100
4673524
3244408
9171516
5178543
904555...

result:

wrong answer 31st words differ - expected: '8164525', found: '2415998'