QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#252716#4037. Absolute Pairwise Distancearvindr9RE 98ms21312kbC++146.1kb2023-11-16 08:45:052023-11-16 08:45:06

Judging History

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

  • [2023-11-16 08:45:06]
  • 评测
  • 测评结果:RE
  • 用时:98ms
  • 内存:21312kb
  • [2023-11-16 08:45:05]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef int int2;
#define int long long
#define pi pair<int, int>
#define vi vector<int>
#define vii vector<vector<int>>
#define vpi vector<pi>
#define lep(i,l,r) for(int i=l;i<=r;++i)
#define rep(i,r,l) for(int i=r;i>=l;--i)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define f first
#define s second
const int inf = 1e18;

int t;


const int maxn = 1e5 + 5;
const int B = 400;

int les[maxn], lazy_les[B];
int sum_les[maxn], lazy_sum_les[B];
int tot = 0;
int totsum = 0;
int a[maxn];
vector<int> vals;
int delta[maxn];

struct Query {
    int l, r, idx;
    Query(int _l, int _r, int _idx): l(_l), r(_r), idx(_idx) {}
    bool operator <(const Query &other) {
        int sign = ((l/B)&1) ? 1 : -1;
        return l/B == other.l/B ? sign * r < sign * other.r : l < other.l;
    }
};

void insert(int x) {
    int i;
    for (i = x + 1; i % B != 0; i++) {
        les[i]++;
        sum_les[i] += vals[x];
    }
    for (i /= B; i < B; i++) {
        lazy_les[i]++;
        lazy_sum_les[i]+=vals[x];
    }
    tot++;
    totsum += vals[x];
}

int query_num_less(int x) {
    return les[x]+lazy_les[x/B];
}

int query_num_greater(int x) {
    return tot - query_num_less(x+1);
}

int query_sum_less(int x) {
    return sum_les[x] + lazy_sum_les[x/B];
}

int query_sum_greater(int x) {
    return totsum - query_sum_less(x+1);
}

typedef array<int,4> a4;
vector<a4> rights[maxn], lefts[maxn];

int2 main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    lep(i,1,n) {
        cin >> a[i];
        vals.pb(a[i]);
    }
    sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());
    lep(i,1,n) {
        a[i] = lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin();
    }
    vector<Query> queries;
    function<void(int,int,int)> make_query = [&](int l, int r, int idx) {
        if (l > r) swap(l,r);
        queries.pb({l,r,idx});
    };
    lep(qq,1,q) {
        int l1, r1, l2, r2;
        cin >> l1 >> r1 >> l2 >> r2;
        // need to ensure we consider things in the right order
        make_query(r1,r2,4*qq-3);
        make_query(r1,l2-1,4*qq-2);
        make_query(l1-1,r2,4*qq-1);
        make_query(l1-1,l2-1,4*qq);
    }
    int curl = 0;
    int curr = 0;
    sort(queries.begin(), queries.end());
    for (auto [l, r, idx]: queries) {
        // cout << "l: " << l << ", r: " << r << ", idx: " << idx << "\n";
        if (curr < r) {
            if (curl) lefts[curl].pb({curr,r,+1,idx});
            curr = r;
        }
        if (curl > l) {
            rights[curr].pb({l,curl,-1,idx});
            curl = l;
        }
        // cout << "curl: " << curl << ", curr: " << curr << "\n";
        if (curr > r) {
            if (curl) lefts[curl].pb({r,curr,-1,idx});
            curr = r;
        }
        if (curl < l) {
            rights[curr].pb({curl,l,+1,idx});
            curl=l;
        }
    }
    lep(i,1,n) {
        insert(a[i]);
        // if (!lefts[i].empty() or !rights[i].empty()) {cout << "i: " << i << "\n";}
        // if (!lefts[i].empty()) {cout << "lefts:\n";}
        for (auto [l, r, sign, idx]: lefts[i]) {
            // changing the right boundary
            lep(j,l+1,r) {
                int num_less = query_num_less(a[j]);
                int num_greater = query_num_greater(a[j]);
                int sum_less = query_sum_less(a[j]);
                int sum_greater = query_sum_greater(a[j]);
                int contrib = vals[a[j]] * num_less - sum_less + sum_greater - vals[a[j]] * num_greater;
                delta[idx] += sign * contrib;
                // cout << "j: " << j << ", contrib: " << contrib << ", idx: " << idx << "\n";
            }
        }
        // if (!rights[i].empty()) {cout << "rights:\n";}
        for (auto [l, r, sign, idx]: rights[i]) {
            lep(j,l+1,r) {
                int num_less = query_num_less(a[j]);
                int num_greater = query_num_greater(a[j]);
                int sum_less = query_sum_less(a[j]);
                int sum_greater = query_sum_greater(a[j]);
                int contrib = vals[a[j]] * num_less - sum_less + sum_greater - vals[a[j]] * num_greater;
                // cout << "j: " << j << ", contrib: " << contrib << ", idx: " << idx << "\n";
                // cout << "j:\n";
                // cout << "num_less: " << num_less << ", num_greater: " << num_greater << ", sum_less: " << sum_less << ", sum_greater: " << sum_greater << ", val: " << vals[a[j]] << ", a[j]: " << a[j] << "\n";
                delta[idx] += sign * contrib;
            }
        }
        // cout << "\n";
    }
    // cout << les[0] << " " << lazy_les[0] << "\n";

    vector<vector<int>> query_answers(q+1);
    int cur_ans = 0;
    for (auto [l,r,idx]: queries) {
        int actual_index = (idx + 3) / 4;
        cur_ans += delta[idx];
        // cout << "l: " << l << ", r: " << r << ", idx: " << idx << ", cur_ans: " << cur_ans << "\n";
        // cout << "delta: " << delta[idx] << "\n";
        query_answers[actual_index].pb(cur_ans);
    }
    lep(qq,1,q) {
        sort(query_answers[qq].begin(), query_answers[qq].end());
        assert((int)query_answers[qq].size() == 4);
        auto &x = query_answers[qq];
        // cout << "x: \n";
        // for (int y: x) {
        //     cout << y << " ";
        // }
        // cout << "\n";
        int ans = x[3] - x[2] - x[1] + x[0];
        cout << ans << "\n";
    }

/*
2 1
1 2
1 1 2 2
*/

/*
3 1
1 2 3    
1 2 2 3
l: 2, r: 3, idx: 1, cur_ans: 10
delta: 10
l: 0, r: 3, idx: 3, cur_ans: 5
delta: -5
l: 1, r: 2, idx: 2, cur_ans: 4
delta: -1
l: 0, r: 1, idx: 4, cur_ans: 4
delta: 0
5

should be
- (2,3) -> 5
- (0,3) -> 0
- (1,2) -> 1
- (0, 1) -> 0
resulting in 4
*/


    
} 

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 17ms
memory: 12248kb

input:

3531 5803
74176369 34532623 97215138 20242270 85176324 44458100 86497709 71971680 57465677 61041230 69951857 80200098 11594209 20849868 84413937 93600227 54771865 31195982 73364328 72732309 10451014 71263803 72054056 97836493 40190962 6061330 8001777 68117678 61121864 54581549 85284322 23842874 6478...

output:

4982399918307
38488424833854
50427592189965
20987588774846
14820726664063
12735883025703
35423534109248
62045165492746
9949038909804
39325291143520
9687088829356
3864891922388
22431762646598
3358380722891
65709151426723
12157633172439
13160383220118
193336411467198
69536721471102
210913413794
222627...

result:

ok 5803 numbers

Test #2:

score: 0
Accepted
time: 8ms
memory: 10712kb

input:

7123 563
41170243 49574901 40252585 38378040 49349320 95777180 92777114 68701843 8269695 69116801 71875492 79726119 34894486 39175970 33491099 9509053 31851236 67622455 99883066 6039354 95474172 23216311 40742840 21155813 61662659 70364687 77128184 937710 17834587 57277312 56522619 99379485 16940778...

output:

386380551092596
110449238652156
71394684802546
7600454099972
131705584966556
5170316849264
318741930490511
479051656741914
44140736680897
231729402927968
184430381271117
84396293483883
65335467971874
49640150006793
390535631756553
81543125885445
8253715937651
44258873130842
299199870748396
157239004...

result:

ok 563 numbers

Test #3:

score: 0
Accepted
time: 16ms
memory: 13224kb

input:

6026 5289
86439387 65035366 16420666 21984922 73262399 76852588 54155978 49360806 20124855 92796259 37385730 41182981 76131653 74700942 94769709 76568214 83485708 61871942 38481854 17929133 81475298 16602113 18106228 95696736 72006794 93191232 47063274 2304956 58032353 31316305 67559039 73489842 628...

output:

13576852392596
413021972227622
99607939375373
474680878191148
94292259536480
26271627959361
1091400567433
62952436748282
335636914667165
9905662489460
82564681744192
445502984448018
16194305287421
582567737068417
64208862777914
261172737624658
109730363437887
271415758185349
105087074337058
70109845...

result:

ok 5289 numbers

Test #4:

score: 0
Accepted
time: 12ms
memory: 12456kb

input:

5220 3588
76702935 38819792 71638425 80109018 94649077 35815970 13885523 86028834 11645900 53149060 36208348 74030342 2558530 17180765 38089131 24833191 5950019 59038688 54682401 20949605 93735658 93767834 35364905 1446805 10955183 40380572 35179937 37888900 48573387 62076084 42499545 43671870 71652...

output:

5346187310649
31418284823199
125761336665063
17353578079738
40963290804826
283391291681454
60317708129345
7928673280951
62988639548028
141478997882298
492618954850328
93712452632082
54611864864795
62847913076609
289994136623163
128873651244873
15551492663786
2208792278905
20661480087038
358606233375...

result:

ok 3588 numbers

Test #5:

score: 0
Accepted
time: 98ms
memory: 21312kb

input:

24293 21467
42015596 59663171 89805164 16690117 45693832 93880503 89158090 47904207 77658280 8332455 58610064 6580199 94339960 10302646 28006189 98994759 99027951 84875602 15254308 69852323 23890949 76646773 85719918 71133414 13601639 3256439 47732703 40033730 67303975 74090183 43041550 54854676 144...

output:

950043023328311
308821816824943
508832214606619
3400847103907531
201954192349476
227585550671387
3616171800395874
1430276165389264
670765489417634
148162666527246
1190437535259504
2683571578223139
4713453596993280
836273332596835
3372213907910584
528723712197333
3398033027623317
3436120193320314
107...

result:

ok 21467 numbers

Test #6:

score: 0
Accepted
time: 67ms
memory: 17144kb

input:

23272 13219
40034160 42407533 96451807 69804540 39282165 25334210 95721885 61953364 15300573 52683920 46742452 44507749 2069440 95739764 63457511 74218872 3642608 93446636 74017306 24660863 31888956 36836301 2412237 62299469 83193011 30891693 46111848 3935746 12407412 47312880 52243481 34399583 1657...

output:

1371590043081753
941241110604112
693285613681356
1673575466342725
1635910420773242
548639205974540
1935291397665475
14372154204350
8708252031342176
207772402401599
1959830317396466
5534398987518615
756854181455491
231717734030102
1320304008713496
328376859417580
7398665124181994
5024294622048940
358...

result:

ok 13219 numbers

Test #7:

score: 0
Accepted
time: 65ms
memory: 18372kb

input:

21522 14988
43497430 9743050 88762892 15034464 41508329 63130141 55232504 46472689 23840784 85244183 85325431 24384837 44468129 41654661 5839808 91718483 51798561 174131 96535490 47353081 53020170 13576005 48664820 35767941 33576009 75857366 35324099 34761433 97403282 65724866 50889839 29206989 4236...

output:

1393916685091568
625984040835011
1601474448797985
2070001256885029
70771370115465
1651993643352107
9992516468147581
753999822681676
8590920075960
1175045347588962
2943711443502258
3028204500544720
3841220677227758
7135074953023132
1017120870506566
3630301026871796
3174361436698632
1468605107362438
1...

result:

ok 14988 numbers

Test #8:

score: 0
Accepted
time: 58ms
memory: 19808kb

input:

5765 21519
75309859 95886648 93883466 32880950 34052502 86469530 42011149 96746507 10593374 89585010 9978209 35962516 93186183 85854843 6936121 91060920 75239687 81521047 365803 94417792 31861124 60157554 66723780 57731488 34970255 34639147 59920221 89926646 30312300 79416697 56161673 33256472 13400...

output:

334342262983088
21668417581622
18897376618788
126199648436180
106976731721959
652912670754
226020936000208
54803944101322
150988894766869
137790164334719
66538252836171
9620835668300
79049348979462
3120462207260
240732944857118
90747980261262
2259530086864
148420682984888
56710132173707
358096367464...

result:

ok 21519 numbers

Test #9:

score: 0
Accepted
time: 28ms
memory: 15012kb

input:

2820 10309
90194274 92822126 60630528 54021579 40699771 25256986 72643662 58524177 58295681 23750688 87769779 27388659 87898017 69458796 63191247 10523773 19115471 90424865 57728332 70273831 10066421 40713317 34511936 79919768 98174558 72301970 91768927 70115230 55412685 31448924 68083795 41628950 4...

output:

36686088418913
18523722151077
7230961497110
22005131932168
36965738782548
11106080064366
90772312954249
7838179262143
17475571722642
16800091821762
49478331555
34870965204306
21144760769879
2754144686954
26582902137370
22010951349380
76667117950686
85446978181619
47321199324594
11997629780388
130171...

result:

ok 10309 numbers

Test #10:

score: -100
Runtime Error

input:

17922 26288
78358526 94388578 79868149 70568640 439488 93628351 22184971 90004285 56914297 62058212 95734077 53735585 22150657 67999267 33565625 29440929 84665836 5498817 16780764 7835984 93917437 93715495 88137424 69026532 13148316 47104550 17805406 72791819 81459663 43094843 58436002 48811273 3539...

output:


result: