QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#105708#5500. BarsMHJMBSWA 527ms4324kbC++203.0kb2023-05-15 06:16:202023-05-15 06:16:24

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-15 06:16:24]
  • 评测
  • 测评结果:WA
  • 用时:527ms
  • 内存:4324kb
  • [2023-05-15 06:16:20]
  • 提交

answer

#include "bits/stdc++.h"

#define fastio ios::sync_with_stdio(0), cin.tie(nullptr)

using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using tiii = tuple<int,int,int>;

struct SegTree {
    vector<pii> seg_l, seg_r;
    int lr_start;
    int lr_size = 1;

    SegTree(vector<int> &a) {
        setSize(a.size());

        lr_start = seg_l.size() - lr_size;

        for(int i = 0; i < a.size(); i++) {
            seg_l[lr_start + i] = {a[i],i};
            seg_r[lr_start + i] = {a[i],i};
        }

        for(int i = lr_start-1; 0 <= i; i--) {
            seg_r[i] = max(seg_r[2*i+1], seg_r[2*i+2]);
            if(seg_l[2*i+1].first == seg_l[2*i+2].first) seg_l[i] = min(seg_l[2*i+1], seg_l[2*i+2]);
            else seg_l[i] = max(seg_l[2*i+1], seg_l[2*i+2]);
        }
    }

    void setSize(int array_size) {
        while(lr_size < array_size) lr_size *= 2;
        seg_l.resize(2*lr_size - 1, {INT_MIN, -1});
        seg_r.resize(2*lr_size - 1, {INT_MIN, -1});
    }

    pii query_r(int l, int r, int lx = 0, int rx = -1, int i = 0) {
        if(rx == -1) rx = lr_size-1;

        int m = (lx+rx)/2;

        if(rx < l || r < lx) return {INT_MIN, -1};
        if(l <= lx && rx <= r) return seg_r[i];
        return max(query_r(l, r, lx, m, 2*i+1), query_r(l, r, m+1, rx, 2*i+2));
    }

    pii query_l(int l, int r, int lx = 0, int rx = -1, int i = 0) {
        if(rx == -1) rx = lr_size-1;

        int m = (lx+rx)/2;

        if(rx < l || r < lx) return {INT_MIN, -1};
        if(l <= lx && rx <= r) return seg_l[i];

        pii lc = query_l(l, r, lx, m, 2*i+1), rc = query_l(l, r, m+1, rx, 2*i+2);
        if(lc.first == rc.first) return min(lc, rc);
        return max(lc, rc);
    }
};

void turn_on(int s, int e, SegTree &seg, vector<int> &p, vector<bool> &on) {
    if(s+1 == e) return;
    auto [best_mid_l, i_l] = seg.query_l(s+1, e-1);
    auto [best_mid_r, i_r] = seg.query_r(s+1, e-1);

    if(best_mid_r < p[s] && best_mid_r < p[e] || best_mid_l == min(p[s],p[e]) && best_mid_r == min(p[s],p[e])) return;
    
    int new_on_i;
    if(p[s] < p[e]) new_on_i = i_l;
    else new_on_i = i_r;

    on[new_on_i] = true;

    turn_on(s, new_on_i, seg, p, on);
    turn_on(new_on_i, e, seg, p, on);
}

int main() {
    fastio;

    int t;
    cin >> t;

    while(t--) {
        int n;
        cin >> n;

        vector<int> p(n);
        for(int &pi : p) cin >> pi;

        vector<bool> on(n, false);
        on[0] = on[n-1] = true;

        SegTree seg(p);

        turn_on(0, n-1, seg, p, on);

        ll ans = 0;

        for(int i = 0, counter = 0; i < n; i++, counter++) {
            if(on[i]) {
                ans += counter * ll(p[i]);
                counter = 0;
            }
        }
        for(int i = n-1, counter = 0; i >= 0; i--, counter++) {
            if(on[i]) {
                ans += counter * ll(p[i]);
                counter = 0;
            }
        }

        cout << ans << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3444kb

input:

2
4
5 2 2 6
5
1 5 4 4 1

output:

33
29

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 527ms
memory: 4324kb

input:

10000
4
5 2 2 6
5
1 5 4 4 1
197
763787596 15221694 898228999 187472305 466351873 822742732 437754202 800092772 843092246 915675776 166265020 346340615 796714085 497548541 182089610 64356048 363276768 181268733 257949015 236568898 752096761 928725929 443146784 114577469 833053207 38120723 14891030 41...

output:

33
29
381268324603
658930892444
543073796232
458946673513
296420749955
872966641760
630432425409
583595848800
225238172692
715347519911
462034667948
273545943459
585094154153
200443831314
336548821022
483213442818
602558460217
586771956643
400264139273
826365121407
975377092201
920978353624
26408806...

result:

wrong answer 3rd lines differ - expected: '382465638565', found: '381268324603'