QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#606461#5433. Absolute DifferenceQHhee004WA 1ms5980kbC++172.8kb2024-10-03 09:09:072024-10-03 09:09:08

Judging History

你现在查看的是最新测评结果

  • [2024-10-03 09:09:08]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5980kb
  • [2024-10-03 09:09:07]
  • 提交

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;
	}
	void print() {
		printf("	[%d %d]\n",l,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]+palen(a[i].len());
//		printf("[%d %d %.12Lf]\n",a[i].l,a[i].r,a[i].mid());
//		printf("i=%d %.12Lf %.12Lf\n",i,prea1[i],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
//		printf("%.12Lf c2=%.12Lf\n",c1,c2);
		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]);
//		printf("[pre2=%.12Lf pre1=%.12Lf]\n",prea2[t1],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]);
//		printf("[%d %d]\n",t1,t2);
//		printf("[c1=%.Lf c2=%.Lf]\n",c1,c2);
		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++) {
//					sa[aa].print();
					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());
//						printf("ans=%.12Lf\n",ans);
					} else {
						ans=ans+(sa[aa].r-sa[aa].l)/3.0l*palen(sa[aa].len())*pblen(sb[bb].len());
					}
				}
			}
		}
	}
	printf("%.12Lf\n",ans);
	return 0;
}


详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 5980kb

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: 0ms
memory: 3980kb

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: 0ms
memory: 4000kb

input:

1 1
-1000000000 1000000000
-1000000000 1000000000

output:

666666666.666666666686

result:

ok found '666666666.666666627', expected '666666666.666666627', error '0.000000000'

Test #4:

score: 0
Accepted
time: 0ms
memory: 5980kb

input:

1 1
-1000000000 0
0 1000000000

output:

1000000000.000000000000

result:

ok found '1000000000.000000000', expected '1000000000.000000000', error '0.000000000'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3928kb

input:

1 1
-1000000000 -1000000000
-1000000000 1000000000

output:

1000000000.000000000000

result:

ok found '1000000000.000000000', expected '1000000000.000000000', error '0.000000000'

Test #6:

score: -100
Wrong Answer
time: 0ms
memory: 3940kb

input:

1 1
-999999999 1000000000
-1000000000 -1000000000

output:

500000000.500000000000

result:

wrong answer 1st numbers differ - expected: '1000000000.5000000', found: '500000000.5000000', error = '0.5000000'