QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#807180#6295. 序列GoatGirl98100 ✓20ms9804kbC++146.2kb2024-12-09 19:39:302024-12-09 19:39:31

Judging History

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

  • [2024-12-09 19:39:31]
  • 评测
  • 测评结果:100
  • 用时:20ms
  • 内存:9804kb
  • [2024-12-09 19:39:30]
  • 提交

answer

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <functional>
typedef long long i64;
namespace FastIO
{
    const int BUFF_SZ = 1 << 20;
    char buf[BUFF_SZ], *p1 = buf, *p2 = buf;
    inline char nc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, BUFF_SZ, stdin), p1 == p2) ? EOF : *p1++; }
    int rd()
    {
        int ret = 0, f = 1;
        char ch = nc();

        while (ch < '0' || ch > '9')
        {
            if (ch == '-')
                f = -1;
            ch = nc();
        }
        while (ch >= '0' && ch <= '9')
        {
            ret = (ret << 1) + (ret << 3) + (ch ^ 48);
            ch = nc();
        }

        return f == 1 ? ret : -ret;
    }
    char Buf[BUFF_SZ], out[20];
    int P, out_size;
    void flush() { Buf[out_size] = '\0', fwrite(Buf, 1, out_size, stdout), out_size = 0; }
    void wr(i64 x, char ch = '\n')
    {
        if (out_size >= (BUFF_SZ >> 1))
            flush();

        if (x < 0)
            Buf[out_size++] = 45, x = -x;

        do
            out[++P] = (x % 10) ^ 48;
        while (x /= 10);

        do
            Buf[out_size++] = out[P];
        while (--P);
        Buf[out_size++] = ch;
    }
    struct IOFlush
    {
        ~IOFlush() { flush(); }
    } tail;
}
// 1-indexed
template <typename T, typename F>
struct rmq
{
    static const int b = 4, all_mask = 65535, B = 16;
    int n; // size of array
    std::vector<T>& a; // origin array
    std::vector<int> _log_2; // record of log_2(i)
    std::vector<std::vector<int>> st; // sparse table
    std::vector<int> masks; // optimize inside block
    F f;

    int op(int x, int y) { return f(a[x], a[y]) ? x : (a[x] == a[y]) ? std::min(x, y) : y; }
    // least significant set bit
    int lsb(int x) { return x & (-x); }
    int msb_index(int x) { return 31 ^ __builtin_clz(x); }

    rmq(F _f, std::vector<T>& _a) : f(_f), n(_a.size() - 1), a(_a),masks(_a.size()) { build(); }

    void build()
    {
        if (!n)
            return;
        int lim = n >> b;
        _log_2.resize(lim + 2);
        for (int i = 2; i < _log_2.size(); ++i)
            _log_2[i] = _log_2[i >> 1] + 1;

        // optimize : O(1) query inside block, use it when block_size <= bit_length (int : 32 long long : 64)
        int cur_mask = 0;
        for (int i = 1; i <= n; ++i)
        {
            // preserve B bits
            cur_mask = (cur_mask << 1) & all_mask; 
            while (cur_mask > 0 && op(i, i - msb_index(lsb(cur_mask))) == i)
                cur_mask ^= lsb(cur_mask);
            cur_mask |= 1;
            masks[i] = cur_mask;
        }

        // sparse table of target element in each block
        st.resize(_log_2[lim] + 1);
        st[0].resize(lim + 1);
        for (int i = 1; i <= lim; ++i)
            st[0][i] = query_small(i << b, (i << b) | (B - 1));
        for (int k = 1, pw = 1; (pw << 1) <= lim; pw <<= 1, ++k)
        {
            st[k].resize(lim - (pw << 1) + 2);
            for (int j = 1; j < st[k].size(); ++j)
                st[k][j] = op(st[k - 1][j], st[k - 1][j + pw]);
        }
    }
    // O(1) optimize
    int query_small(int l, int r)
    {
        int dist_from_r = msb_index(masks[r] & ((1 << (r - l + 1)) - 1));
        return r - dist_from_r;
    }

    // query after static rmq is built
    int query(int l, int r)
    {
        if (r - l + 1 <= B)
            return query_small(l, r);
        int ret = op(query_small(l, l + B - 1), query_small(r - B + 1, r));
        int belong_l = (l >> b) + 1, belong_r = (r >> b);
        if (belong_l < belong_r)
        {
            int dep = msb_index(belong_r - belong_l);
            ret = op(ret, op(st[dep][belong_l], st[dep][belong_r - (1 << dep)]));
        }
        return ret;
    }
};
// 1-indexed
template <typename T, typename F>
struct nearest_bound
{
    int n;
    std::vector<T>& a;
    std::vector<int> pre, suf;
    F f;
    bool op(int x, int y) { return f(a[x], a[y]); }
    // pre[i] = 0 or suf[i] = 0 means pre/suf nearest bound do not exist.
    nearest_bound(F _f, std::vector<T>& _a) : f(_f), n(_a.size() - 1), a(_a), pre(_a.size()), suf(_a.size())
    {
        std::vector<int> s;
        for (int i = 1; i <= n; ++i)
        {
            while (s.size() && !op(s.back(), i))
                s.pop_back();
            if (s.size())
                pre[i] = s.back();
            s.push_back(i);
        }
        s.clear();
        for (int i = n; i; --i)
        {
            while (s.size() && !op(s.back(), i))
                s.pop_back();
            if (s.size())
                suf[i] = s.back();
            s.push_back(i);            
        }
    }
};

int main()
{
    int n = FastIO::rd(), q = FastIO::rd();
    std::vector<int> a(n + 1);
    for (int i = 1; i <= n; ++i)
        a[i] = FastIO::rd();
    auto f = [](int a, int b) { return a < b; };
    nearest_bound<int, decltype(f)> near_lower_bound(f, a);
    rmq<int, decltype(f)> min_query(f, a); 

    // pre / suf[i] : sum of interval which i is tail / head
    // s_pre / s_suf[i] : sum of interval \in [1, i] / [i, n]
    std::vector<i64> pre(n + 1), s_pre(n + 1), suf(n + 1), s_suf(n + 1);

    for (int i = 1; i <= n; ++i)
        pre[i] = pre[near_lower_bound.pre[i]] + 1ll * a[i] * (i - near_lower_bound.pre[i]), s_pre[i] = s_pre[i - 1] + pre[i];
    for (int i = n; i; --i)
        suf[i] = suf[near_lower_bound.suf[i]] + 1ll * a[i] * (near_lower_bound.suf[i] - i), s_suf[i] = s_suf[i + 1] + suf[i];
    
    std::function<i64(int, int)> solve = [&](int l, int r)
    {
        int pivot = min_query.query(l, r);
        // interval which covers pivot
        i64 cover_pivot = 1ll * (pivot - l + 1) * (r - pivot + 1) * a[pivot];
        // interval \in [l, pivot - 1]
        i64 pivot_left = (s_suf[l] - s_suf[pivot] - 1ll * (pivot - l) * suf[pivot]);
        // interval \in [pivot + 1, r]
        i64 pivot_right = (s_pre[r] - s_pre[pivot] - 1ll * (r - pivot) * pre[pivot]);
        return cover_pivot + pivot_left + pivot_right;
    };
    while (q--)
    {
        int l = FastIO::rd(), r = FastIO::rd();
        FastIO::wr(solve(l, r));
    }
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 0ms
memory: 3632kb

input:

100 100
318638321 -676975993 -774173967 -145467618 902596785 -87338335 776396781 583037673 -60732572 -884626701 -951213302 205254095 639816161 732575976 673658959 445315507 230194932 -938847546 -269398572 -945809504 832573907 -140810325 -279174703 -44486048 -559721002 687955471 208624227 -700433784 ...

output:

-775934200189
-1219823512911
-55765585702
-1074436923070
-142395435965
-3175808844233
-904690520894
-3999632894336
-7610614382
-1071443272915
-548134975425
-196154935201
-227502218875
-861050302761
-1842477777589
-27472207018
-863656393712
-23879364913
-349661069829
-191236982311
-6094536043
-166117...

result:

ok 100 lines

Test #2:

score: 10
Accepted
time: 0ms
memory: 4912kb

input:

100 100000
330487256 899579028 732827294 602593291 45541163 699126787 -866145051 -276760250 -266963770 -973765229 -306351438 610145618 275965879 -613170589 8911208 -760203856 574470436 -168880482 392098938 -827483099 -611368343 234488672 211596637 -135262795 616365186 -418453448 -145178033 -67279906...

output:

-1541655937632
-28407353721
-1299166475127
-327229237073
-885509994376
-829534625411
-905551573526
-116628564640
-1087265236840
-570944738832
-30881634162
-146939356163
-77146696029
-525240965076
667272116
-188217175453
-371603162809
-94276266665
-30419152376
-2371889929067
-278767026819
-1788796436...

result:

ok 100000 lines

Test #3:

score: 10
Accepted
time: 7ms
memory: 5368kb

input:

5000 100000
330655326 -570635776 -220541152 -285388364 749212228 -893727788 -3353257 -730636999 -711570069 -228740962 -660641626 -972890968 940941623 129259831 -990492569 -122397061 -45091823 -3563192 35952750 607943021 -235859901 -49565167 -25128155 518355952 -539692037 159837647 -317730690 4851018...

output:

-5036527550611710
-4562376802606931
-1605773382127620
-7917299099945577
-4789049420636409
-186777716280465
-1205190033536
-1412600485449643
-819356462466
-5435294613714760
-12921059863359
-46844583166121
-179730417918948
-1489032537696505
-518944812681082
-1457288103474643
-8503444315306383
-2146415...

result:

ok 100000 lines

Test #4:

score: 10
Accepted
time: 10ms
memory: 5332kb

input:

5000 100000
330789782 -458317431 -124242450 -995773688 -835334567 572995095 804867095 194751790 221235080 796775181 -514577047 -786803882 184432030 723204167 -72028673 387848375 747748558 128690640 180532529 -391199730 494043582 -988318051 -644004718 -676735968 -887557440 622470523 -885269545 981925...

output:

-711430502136003
-824604885134786
-3783928196723637
-1510925929783844
-422109419515403
-2020973353262281
-4664070229379670
-2377140364052860
-3761380037638018
-78378923183355
-610484229437406
-2710152327536
-592589973711975
-514474983933612
-8634612208316748
-82479608383143
-18805439646611
-52024009...

result:

ok 100000 lines

Test #5:

score: 10
Accepted
time: 4ms
memory: 9148kb

input:

100000 10
318772777 -564657648 -677875265 -855852942 -681950010 -620615453 -562866514 -639057185 872072577 140889442 -952632369 538824827 -116693432 -820963335 -555360792 955560943 -976964688 -954077360 -124818793 55047746 -585006257 920436792 -898051266 907905679 -907586405 -996895300 -358914628 -2...

output:

-126685025760974224
-1328528668247679412
-3419528864161116876
-773946985988435647
-620109423937283243
-1993761282594992601
-2043184846861080228
-598392669179977941
-95700006578042397
-1427750249015756515

result:

ok 10 lines

Test #6:

score: 10
Accepted
time: 4ms
memory: 9148kb

input:

100000 10
318823198 282768099 -104892340 -48505615 602893133 790018363 -259783882 -560471845 953439052 -279848372 -629422696 -409827972 941792750 -598234209 -210936831 73161158 -948085001 -904482173 197834080 485675582 -42856995 -950030996 748918214 459746209 101351335 250333852 233564669 -285736255...

output:

-397170852016794559
-340504037150195928
-8525814374310227
-2556549052394183179
-1371153165760631542
-760895007269931395
-652402730978949202
-855143419363
-1697114572656153098
-59603369884711301

result:

ok 10 lines

Test #7:

score: 10
Accepted
time: 20ms
memory: 9640kb

input:

100000 100000
318856812 847718597 992924159 -226101946 743627346 -417042740 -57728794 207746264 -423972396 -560340248 -982293817 -326435289 215794440 -449748125 18679143 200722517 -360487640 -871418715 -302891887 772760806 -397252036 -295331951 -868930016 160973229 -559356839 365992071 628550867 375...

output:

-661081304363772826
-16559596874860321
-508272099316401522
-3132173078323798376
-3364758782572597
-79182916791222450
-18355933794733130
-1003089425445045023
-279243794890174615
-1808339909113245690
-410973648512026261
-152598419718223596
-40919121884665806
-18692391713531426
-1183741146813285906
-36...

result:

ok 100000 lines

Test #8:

score: 10
Accepted
time: 10ms
memory: 9676kb

input:

100000 100000
318890426 -734814552 -56742989 -403698277 884361559 523379804 144326294 975964373 346099803 -840832124 517351417 -243042606 -510203870 -301262041 248295117 328283876 374593367 -838355257 -803617854 -940153971 -751647077 506850740 -634261891 -137799751 927418634 481650290 -976462936 -96...

output:

-438413370216872501
-243393954812047220
-1436061505900963673
-84571750515179765
-87150922853606907
-839612595384811412
-2105356713097510162
-515973354214092393
-1182280245739057543
-1337300076880728
-1271270557567306396
-20427606333987497
-118845072657777413
-346047938914920403
-49915044723616895
-5...

result:

ok 100000 lines

Test #9:

score: 10
Accepted
time: 19ms
memory: 9804kb

input:

100000 100000
17538 57635 88697 -53948 -5686 80893 68229 -5700 58174 -22989 62469 -50994 70836 64548 36622 -31134 -9840 52158 56356 28094 -23232 -41206 -9324 -83232 -32643 -28813 48240 -96664 -85083 86334 37490 -75978 82159 37784 -25061 -76839 85626 9724 66455 83304 -92733 5921 -50399 -248 -82542 97...

output:

-20991302328081
-84369116656227
-95330996972539
-161935030453125
-35968679313696
-161453018115475
-193590341836614
-1754827372378
-103809801424885
-158759431717191
-327956886607964
-2644667200031
-4514678657359
-93552243774979
-11648655486356
-63928490113887
-87031184885854
-194589733590350
-1070718...

result:

ok 100000 lines

Test #10:

score: 10
Accepted
time: 15ms
memory: 9676kb

input:

100000 100000
19221 -2145 -63973 -26606 -4630 42125 -7305 -65912 -77554 34170 94697 -97398 48575 -96313 -74423 60284 -91670 31914 61395 57916 90064 12146 16928 -39239 83954 -82969 7759 -861 -81440 -43271 -92646 63540 -8243 -17128 -90371 -79174 -7418 5863 -97207 50444 312 -67531 17017 78529 -43946 -8...

output:

-369020398544022
-62559019419230
-2757838852672
-298863849191716
-277900343556874
-50414737007568
-422707944768946
-2280373180841
-96775973803026
-18001384633821
-87091776612639
-4391967845695
-1351841902935
-767203317260
-58450198730304
-155047665904204
-191409308285523
-24411876665602
-19417023841...

result:

ok 100000 lines