QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#764188 | #5433. Absolute Difference | fosov | TL | 0ms | 0kb | C++14 | 2.5kb | 2024-11-20 05:11:49 | 2024-11-20 05:11:49 |
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lll __int128
#define INF 0x3f3f3f3f
#define LNF 0x3f3f3f3f3f3f3f3fll
#define MOD 998244353
#define pii pair<int, int>
#define ld long double
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define N 20
int main() {
#ifdef TEST
freopen("zz.in", "r+", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << setprecision(18);
int n, m; cin >> n >> m;
vector<pii> a(n), b(m);
ll ca = 0, cb = 0;
for (int i = 0; i < n; ++ i) cin >> a[i].fi >> a[i].se, ca += a[i].se - a[i].fi;
for (int i = 0; i < m; ++ i) cin >> b[i].fi >> b[i].se, cb += b[i].se - b[i].fi;
sort(all(a)), sort(all(b));
auto pa = [&](int l, int r) {
return ca ? (ld) (r - l) / ca : (ld) 1 / n;
};
auto pb = [&](int l, int r) {
return cb ? (ld) (r - l) / cb : (ld) 1 / m;
};
auto w = [&](int l, int r) {
return l + (ld) (r - l) / 2;
};
ld sum = 0;
for (int i = 0; i < m; ++ i) sum += w(b[i].fi, b[i].se) * pb(b[i].fi, b[i].se);
ld sl = 0, sr = 0, res = 0;
for (int i = 0, l = 0, r = 0; i < n; ++ i) {
while (l < n && b[l].se < a[i].fi) sl += w(b[i].fi, b[i].se) * pb(b[i].fi, b[i].se), ++ l;
while (r < n && b[r].fi <= a[i].se) sr += w(b[i].fi, b[i].se) * pb(b[i].fi, b[i].se), ++ r;
res += w(a[i].fi, a[i].se) * pa(a[i].fi, a[i].se) * l - sl;
res += (sum - sr) - w(a[i].fi, a[i].se) * pa(a[i].fi, a[i].se) * (n-r);
queue<pii> cur;
for (int j = l; j < r; ++ j) cur.emplace(b[j]);
while (!cur.empty()) {
auto [cl, cr] = cur.front(); cur.pop();
if (max(cl, a[i].fi) < min(cr, a[i].se)) {
cur.emplace(cl, a[i].fi);
cur.emplace(a[i].se, cr);
cur.emplace(max(cl, a[i].fi), min(cr, a[i].se));
continue;
}
if (cl == a[i].fi && cr == a[i].se) {
ld d = a[i].se - a[i].fi;
res += d * pa(cl, cr) * pb(cl, cr) / 3;
continue;
}
if (cr <= a[i].fi) {
res += w(a[i].fi, a[i].se) * pa(a[i].fi, a[i].se) - w(cl, cr) * pb(cl, cr);
continue;
}
if (cl >= a[i].se) {
res += w(cl, cr) * pb(cl, cr) - w(a[i].fi, a[i].se) * pa(a[i].fi, a[i].se);
continue;
}
}
}
cout << res << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
1 1 0 1 0 1