QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#188745 | #4037. Absolute Pairwise Distance | training4usaco | WA | 11ms | 16556kb | C++14 | 4.0kb | 2023-09-26 13:09:34 | 2023-09-26 13:09:35 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define ff first
#define ss second
using namespace std;
const int INF = 1e9 + 7;
const int MAXN = 1e5 + 5;
const int BLOCK = 350;
int n, q;
int a[MAXN], b[MAXN];
pii compress[MAXN];
int diff[4 * MAXN];
int ans[MAXN];
int l = 1, r = 1, totans;
int qcnt = 0;
int bl[MAXN], br[MAXN], which[MAXN];
int innercnt[MAXN], innerpsum[MAXN];
int blockcnt[MAXN], blockpsum[MAXN];
struct Query {
int l, r, i, type;
};
Query queries[4 * MAXN];
vector<Query> ops[MAXN];
bool comp(Query a, Query b) {
if((a.l / BLOCK) == (b.l / BLOCK)) {
return a.r < b.r;
}
return (a.l / BLOCK) < (b.l / BLOCK);
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
cin >> n >> q;
for(int i = 1; i <= n; ++i) {
cin >> a[i]; compress[i] = {a[i], i};
}
sort(compress + 1, compress + 1 + n);
int val = 1;
for(int i = 1; i <= n; ++i) {
if(i > 1 && compress[i - 1].ff != compress[i].ff) ++val;
b[compress[i].ss] = val;
}
for(int i = 1; i <= q; ++i) {
int l1, r1, l2, r2; cin >> l1 >> r1 >> l2 >> r2;
queries[++qcnt] = {r1, r2, i, 1};
if(l1 > 1) queries[++qcnt] = {l1 - 1, r2, i, -1};
if(l2 > 1) queries[++qcnt] = {r1, l2 - 1, i, -1};
if(l1 > 1 && l2 > 1) queries[++qcnt] = {l1 - 1, l2 - 1, i, 1};
}
sort(queries + 1, queries + 1 + qcnt, comp);
for(int i = 1; i <= qcnt; ++i) {
diff[i] = 0;
// cout << queries[i].l << " " << queries[i].r << " " << queries[i].i << " " << queries[i].type << endl;
// cout << "hello" << endl;
if(queries[i].r > r) ops[l].push_back({r + 1, queries[i].r, i, 1});
if(queries[i].r < r) ops[l].push_back({queries[i].r + 1, r, i, -1}); // pointer will move from qry r + 1 to r
if(queries[i].l > l) ops[queries[i].r].push_back({l + 1, queries[i].l, i, 1});
if(queries[i].l < l) ops[queries[i].r].push_back({queries[i].l + 1, l, i, -1});
l = queries[i].l; r = queries[i].r;
// cout << l << " " << r << endl;
}
int bucket = n / BLOCK + 1;
for(int i = 1; i <= bucket; ++i) {
bl[i] = br[i - 1] + 1;
br[i] = br[i - 1] + BLOCK;
}
br[bucket] = n;
for(int i = 1; i <= bucket; ++i) {
for(int j = bl[i]; j <= br[i]; ++j) which[j] = i;
}
// for(int i = 1; i <= n; ++i) {
// cout << bl[which[i]] << " " << br[which[i]] << endl;
// }
// for(int i = 1; i <= n; ++i) {
// cout << a[i] << " " << b[i] << endl;
// }
// for(int i = 1; i <= n; ++i) {
// cout << i << ": ";
// for(auto qry : ops[i]) {
// cout << "(" << qry.l << "," << qry.r << "," << qry.i << "," << qry.type << ")";
// }
// cout << endl;
// }
int sum = 0;
for(int i = 1; i <= n; ++i) {
for(int j = which[b[i]] + 1; j <= bucket; ++j) {
// cout << "hello" << endl;
++blockcnt[j]; blockpsum[j] += a[i];
}
for(int j = b[i]; j <= br[which[b[i]]]; ++j) {
++innercnt[j]; innerpsum[j] += a[i];
}
sum += a[i];
// cout << "sum: " << sum << endl;
for(auto qry : ops[i]) {
for(int j = qry.l; j <= qry.r; ++j) {
// cout << a[j] << " " << (2 * (innercnt[b[j]] + blockcnt[which[b[j]] + 1]) - i) << " " << sum << " " << -2 * (innerpsum[b[j]] + blockpsum[which[b[j]] + 1]) << endl;
diff[qry.i] += qry.type * (a[j] * (2 * (innercnt[b[j]] + blockcnt[which[b[j]] + 1]) - i) + sum - 2 * (innerpsum[b[j]] + blockpsum[which[b[j]] + 1]));
// cout << qry.type << " " << diff[qry.i] << endl;
}
}
}
for(int i = 1; i <= qcnt; ++i) {
diff[i] += diff[i - 1];
ans[queries[i].i] += queries[i].type * diff[i];
}
for(int i = 1; i <= q; ++i) cout << ans[i] << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 11ms
memory: 16556kb
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:
4850865260343 37486645411138 49351279250695 19919251801808 14749987359665 12751094702523 35060737686376 59981480629290 9935192956226 38470191272266 9692467041480 3859329597598 21385007097620 3241212173483 63465099860637 12061877822665 12536968824818 188772203803492 68171338447598 210029093374 209243...
result:
wrong answer 1st numbers differ - expected: '4982399918307', found: '4850865260343'