caioj 1362 NOIP2015普及组 第三题 求和

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 /**
 	caioj 1362 2015 普及组第三题 
 */
#include<cstdio>
const int N=110000;
const int NN=10007;
int n,m;
struct node{
	long long id;
	long long x;
	int c;
};
node a[N];
long long ans;
void qsort1(int l,int r){
	int i,j;
	i=l;j=r;
	node x=a[(i+j)>>1];
	while(i<=j){
		while(a[i].c<x.c||
			a[i].c==x.c&&a[i].id%2<x.id%2) i++;
		while(a[j].c>x.c||
			a[j].c==x.c&&a[j].id%2>x.id%2) j--;
		if(i<=j){
			node t=a[j];
			a[j]=a[i];
			a[i]=t;
			i++;j--;
		}		
		
	}
	if(l<j) qsort1(l,j);
	if(i<r) qsort1(i,r);
}
int main(){
	freopen("caioj1362.in","r",stdin);
// 	freopen("caioj1362.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int  i=1;i<=n;i++) {
		scanf("%lld",&a[i].x);
		a[i].id=i;
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i].c);
	}
	qsort1(1,n);
	a[n+1].c=m+1;
//	for(int i=1;i<=n;i++) printf("%d %d %d\n",a[i].c,a[i].id,a[i].x);
//	printf("------------------------\n");
	
	ans=0;//l
	int r=1;int l;
	int s1,s2,s3;
	while(r<=n){
		l=r;
		while(a[r].c==a[r+1].c&&a[r].id%2==a[r+1].id%2) r++;
		//统计l-r的结果
		if(r==l){
			
		}else{
			s1=s2=s3=0;
			for(int i=l;i<=r;i++){
				s1=(s1+a[i].x)%NN;
				s2=(s2+a[i].id)%NN;
				s3=(s3+(r-l-1)*a[i].id*a[i].x)%NN; 
			
			}
		//	printf("%d %d %d\n",s1,s2,s3);
			ans=(ans+s1*s2+s3)%NN;
		}
		r++;
	}
	printf("%lld",ans);
}
posted @ 2017-11-10 23:04  Exception2017  阅读(101)  评论(0编辑  收藏  举报