QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#252718 | #4037. Absolute Pairwise Distance | arvindr9 | RE | 558ms | 63784kb | C++14 | 6.1kb | 2023-11-16 08:48:27 | 2023-11-16 08:48:27 |
Judging History
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 = 3e5 + 5;
const int B = 600;
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: 23ms
memory: 27752kb
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: 24680kb
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: 21ms
memory: 27080kb
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: 21ms
memory: 26960kb
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: 106ms
memory: 35976kb
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: 78ms
memory: 30664kb
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: 75ms
memory: 31984kb
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: 78ms
memory: 35012kb
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: 31ms
memory: 28864kb
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: 0
Accepted
time: 104ms
memory: 38316kb
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:
876519285943318 191445528176750 331694640557283 745853658705581 31865177126080 3286266613132290 1306011210182755 1649949488467668 3069536331277614 24317963844253 353573666568278 1611872388252603 22400128762115 208100832725593 22960021698592 791955605781436 448878667542222 4752171840490562 1920031984...
result:
ok 26288 numbers
Test #11:
score: 0
Accepted
time: 36ms
memory: 26464kb
input:
15254 5419 23759171 14398863 55270407 43706573 44678298 67530032 92592120 62281082 24445144 38403056 73864789 64998018 29791277 57113571 57871629 48391201 96538824 22387807 93818995 19332322 61142675 43164249 308943 74103587 46301666 26566515 89342515 5503555 77763021 56013466 15695446 2244016 19549...
output:
327969156735008 1480691582971922 237081285040832 138079076573930 14427855755332 385885721913258 70720825292106 81487109670966 16489894883997 642786282250491 2267868261383217 2607520471802536 1646931840250258 108663283850354 667972873116410 1655506939362462 155858616538842 729381845577308 41453646873...
result:
ok 5419 numbers
Test #12:
score: 0
Accepted
time: 97ms
memory: 34028kb
input:
17531 20025 89079913 95968767 67951680 52572930 57209077 7082212 77033680 29884871 43213842 59062998 92273353 32608841 53955729 31447051 59219017 84519161 48471766 7752841 78397194 41616261 98643355 12421529 93669325 39244298 76001755 2116687 75492810 79421218 15596184 23633662 89582880 55092499 604...
output:
698576838001654 2973053072636441 1833120886165237 1486949557410341 5925274359562102 216565463559254 1796106521689493 836791285670216 1414727915454730 106596810948479 2877995854358919 235079733730934 31201521554613 3042893209407129 115558265617223 616367410181758 3377776307555249 295960305298925 3649...
result:
ok 20025 numbers
Test #13:
score: 0
Accepted
time: 89ms
memory: 34212kb
input:
2016 28025 12798889 35776185 33985605 30906795 82494982 17396057 67069492 24290293 15253193 47917498 45997113 87936784 68388214 9520714 20889240 64849148 75080485 94049772 7268719 76532360 50522558 55614870 55389553 16148127 5903384 90101525 12377922 2201182 42757227 69293408 10886606 6137700 602860...
output:
20129709197003 938986681748 11865409506185 1924459381408 41698860259613 46095255619004 28544355381714 432504022908 14733321836971 31659883009638 1480277567734 2390275538516 31394504055664 9855556093026 33917518331678 6979349353605 9892619167504 429276344980 444803715781 865329708448 56906156098188 1...
result:
ok 28025 numbers
Test #14:
score: 0
Accepted
time: 131ms
memory: 39504kb
input:
25605 29190 56393888 43675568 30608768 11331936 83874156 3497015 89741541 28546104 15970842 35786145 74700604 10110105 30269269 10854297 42232542 6035859 84680730 33985283 72974489 45094504 67466236 54108221 77474352 16491485 25474577 28983138 39026828 90542575 77332778 10439075 72415087 50025703 23...
output:
5363348659571712 816455088116379 4905380583761188 390895601187508 1100342088323280 5355922627565475 4600909459783512 1946114014369987 2255017653763562 1138892837646226 1689775972128879 2224325088920086 3854029637902801 106057789563226 1349803223387122 2081549530328252 2084107261651164 14782841178713...
result:
ok 29190 numbers
Test #15:
score: 0
Accepted
time: 139ms
memory: 40692kb
input:
18735 31763 47972601 79276275 4668983 54104880 42506513 11920591 81447054 86313401 46405970 26689505 647618 92801665 63163216 90130266 69002595 94916048 7422346 34415314 91071725 19010124 58899318 7799583 87050672 91153819 29188070 75902447 31307782 14972114 32600168 14533399 62268169 31068958 70862...
output:
946550530556344 224588319662310 1594210357388688 640510408516530 2413928477968702 2876709710509904 1820558481247742 218473127454708 134013977869194 317396336549721 580888581251749 1903932497357252 379392629706009 439333795783946 82172507312723 4120619275582918 314456574694572 1015577801642579 282269...
result:
ok 31763 numbers
Test #16:
score: 0
Accepted
time: 254ms
memory: 47740kb
input:
51320 41754 89847568 20178499 83185553 10397681 69485359 56906591 22782464 40735141 53331790 38339199 78014614 22002068 84360783 77517105 55889956 31387225 72718313 71785228 4148619 50371045 39047172 61490870 92099668 26602456 87193821 35895863 19892151 14979786 57830212 36905284 30294157 86658104 6...
output:
871969678370951 796278778098292 2219013502606909 444656116198729 16484794258625693 16199660383388572 4112977967307052 1727189326382961 41239986229406529 2925497299337505 5922362293845599 36976347490558814 11675707520569753 11615696054869953 16941507543314613 7511955498703955 6294057110256975 4884994...
result:
ok 41754 numbers
Test #17:
score: 0
Accepted
time: 558ms
memory: 63784kb
input:
87657 71009 34623063 47677363 95147631 17600790 89529096 74235244 8242944 82519342 23839538 12783790 65923884 40656281 32144213 35767242 67964843 41873013 50858136 28381322 79319636 41401240 99322340 4561733 97309509 97946744 9906896 32148467 69516446 19361810 98795121 77948358 15725334 11247369 572...
output:
1722586268168591 7073783081047802 14078105231789531 3651179789964329 10524693013676604 38690091467053046 27635911920786212 15320028504827418 4987333812070181 24124667059955225 314811532043202 10595290237812254 38545761001475175 23788244769228192 146539350073246 16402435303463568 101661421999677226 6...
result:
ok 71009 numbers
Test #18:
score: -100
Runtime Error
input:
100000 100000 79130244 57461331 67889374 44393448 18109279 41193846 4425706 90278748 84827342 60719858 37897767 12436514 74870786 53080270 23739772 94859180 48949722 63848337 11144151 89354087 33973464 61392053 33891526 5505014 37629057 19730344 16319661 52860499 28309665 85830100 70092676 43107736 ...