QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#606458 | #5433. Absolute Difference | QHhee004 | WA | 1ms | 5968kb | C++17 | 2.4kb | 2024-10-03 08:53:56 | 2024-10-03 08:53:58 |
Judging History
answer
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
const int N=1e5+5;
typedef long long LL;
typedef long double db;
db prea1[N],prea2[N],suba1[N],suba2[N];
int lena[N],lenb[N];
int min(int a,int b) {
if(a<b)return a;
return b;
}
int max(int a,int b) {
if(a<b)return b;
return a;
}
db palen(db t) {
if(lena[n]) return 1.0*t/lena[n];
else return 1.0/n;
}
db pblen(db t) {
if(lenb[m]) return 1.0*t/lenb[m];
else return 1.0/m;
}
struct section {
int l,r;
long double mid() {
return (l+r)/2.0;
}
long double len() {
return (r-l);
}
bool operator <(const section &t)const {
return l<t.l||l==t.l&&r<t.r;
}
} a[N],b[N],sa[5],sb[5];
int main() {
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++) {
scanf("%d%d",&a[i].l,&a[i].r);
lena[i]=lena[i-1]+a[i].r-a[i].l;
}
for(int i=1; i<=m; i++) {
scanf("%d%d",&b[i].l,&b[i].r);
lenb[i]=lenb[i-1]+b[i].r-b[i].l;
}
sort(a+1,a+1+n),sort(b+1,b+1+m);
for(int i=1; i<=n; i++) {
prea1[i]=prea1[i-1]+a[i].mid();
prea2[i]=prea2[i-1]+a[i].mid()*palen(a[i].len());
}
for(int i=n; i>=1; i--) {
suba1[i]=suba1[i+1]+a[i].mid();
suba2[i]=suba2[i+1]+a[i].mid()*palen(a[i].len());
}
db ans=0;
for(int i=1; i<=m; i++) {
int l=b[i].l,r=b[i].r;
db md=b[i].mid();
db c1=pblen(b[i].len()),c2=b[i].mid()*c1;
//c1*prea2-c2*prea1
int L=1,R=n,t1=0,t2=n+1;
while(L<=R) {
int mid=L+R>>1;
if(a[mid].r<=l)t1=mid,L=mid+1;
else R=mid-1;
}
ans=ans+fabs(c1*prea2[t1]-c2*prea1[t1]);
L=1,R=n;
while(L<=R) {
int mid=L+R>>1;
if(a[mid].l>=r)R=mid-1,t2=mid;
else L=mid+1;
}
ans=ans+fabs(c1*suba2[t2]-c2*suba1[t2]);
for(int j=t1+1; j<t2; j++) {
int al=a[j].l,ar=a[j].r,at=0,bt=0;
if(al<l) {
sa[++at]=section{al,l};
} else if(l<al) {
sb[++bt]=section{l,al};
}
if(ar>r) {
sa[++at]=section{r,ar};
}else if(r>ar) {
sb[++bt]=section{ar,r};
}
sa[0]=section{max(al,l),min(ar,r)};
sb[0]=section{max(al,l),min(ar,r)};
for(int aa=0; aa<=at; aa++) {
for(int bb=0; bb<=bt; bb++) {
if(sa[aa].l!=sb[bb].l||sa[aa].r!=sb[bb].r) {
ans=ans+palen(sa[aa].len())*pblen(sb[bb].len())*fabs(sa[aa].mid()-sb[bb].mid());
} else {
ans=ans+(sa[aa].r-sa[aa].l)/3.0l*palen(sa[aa].r-sa[aa].l)*pblen(sb[bb].r-sb[bb].l);
}
}
}
}
}
printf("%.12Lf\n",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5960kb
input:
1 1 0 1 0 1
output:
0.333333333333
result:
ok found '0.333333333', expected '0.333333333', error '0.000000000'
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 5968kb
input:
1 1 0 1 1 1
output:
0.000000000000
result:
wrong answer 1st numbers differ - expected: '0.5000000', found: '0.0000000', error = '0.5000000'