QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#605308 | #5433. Absolute Difference | QHhee004 | WA | 1ms | 6012kb | C++17 | 2.5kb | 2024-10-02 16:39:07 | 2024-10-02 16:39:07 |
Judging History
answer
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
const int N=1e5+5;
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;
}
struct section {
int l,r;
long double mid() {
return (l+r)/2.0;
}
bool operator <(const section &t)const {
return l<t.l||l==t.l&&r<t.r;
}
} a[N],b[N],sa[5],sb[5];
typedef long long LL;
typedef long double db;
db prea[N],suba[N];
int lena[N],lenb[N];
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;
}
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++)
prea[i]=prea[i-1]+a[i].mid();
for(int i=n; i>=1; i--)
suba[i]=suba[i+1]+a[i].mid();
db ans=0;
for(int i=1; i<=m; i++) {
int l=b[i].l,r=b[i].r;
db sum=0,md=b[i].mid();
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;
}
sum=sum+(md*t1-prea[t1])*palen(lena[t1])*pblen(r-l);
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;
}
sum=sum+(suba[t2]-md*t2)*palen(lena[n]-lena[t2-1])*pblen(r-l);
t1++,t2--;
db g=palen(1)*pblen(1);
sum*=g;
for(int j=t1; 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)};
// printf("[%d %d]\n",at,bt);
for(int aa=0; aa<=at; aa++) {
for(int bb=0; bb<=bt; bb++) {
// printf("[%d %d] [%d %d]\n",sa[aa].l,sa[aa].r,sb[bb].l,sb[bb].r);
if(sa[aa].l!=sb[bb].l||sa[aa].r!=sb[bb].r) {
// printf(" sum=%.12Lf\n",sum);
sum=sum+abs(sa[aa].mid()-sb[bb].mid())*palen(sa[aa].r-sa[aa].l)*pblen(sb[bb].r-sb[bb].l);
// printf(" sum=%.12Lf\n",sum);
} else {
sum=sum+(sa[aa].r-sa[aa].l)/3.0l*palen(sa[aa].r-sa[aa].l)*pblen(sb[bb].r-sb[bb].l);
}
}
}
}
ans=ans+sum;
}
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: 5968kb
input:
1 1 0 1 0 1
output:
0.333333333333
result:
ok found '0.333333333', expected '0.333333333', error '0.000000000'
Test #2:
score: 0
Accepted
time: 1ms
memory: 5976kb
input:
1 1 0 1 1 1
output:
0.500000000000
result:
ok found '0.500000000', expected '0.500000000', error '0.000000000'
Test #3:
score: 0
Accepted
time: 1ms
memory: 5968kb
input:
1 1 -1000000000 1000000000 -1000000000 1000000000
output:
666666666.666666666686
result:
ok found '666666666.666666627', expected '666666666.666666627', error '0.000000000'
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 6012kb
input:
1 1 -1000000000 0 0 1000000000
output:
0.000000001000
result:
wrong answer 1st numbers differ - expected: '1000000000.0000000', found: '0.0000000', error = '1.0000000'