标签:顺序 iostream end -- names text sum out mina
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 62455 | Accepted: 23259 |
Description
Input
Output
Sample Input
5 9 1 0 5 4 3 1 2 3 0
Sample Output
6 0
Source
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 using namespace std; 6 int n,a[500005],tmp[500005],size; 7 long long ans,cnt[2000005]; 8 void init(int root,int l,int r,int x){ 9 if ((l==r)&&(l==x)) { 10 cnt[root]++; 11 return; 12 } 13 int mid=(l+r)>>1; 14 if (x<=mid) init(root<<1,l,mid,x); 15 else if (x>mid) init(root<<1|1,mid+1,r,x); 16 cnt[root]=cnt[root<<1]+cnt[root<<1|1]; 17 } 18 long long sum(int root,int l,int r,int x,int y){ 19 if ((x<=l)&&(y>=r)) return cnt[root]; 20 long long anss=0; 21 int mid=(l+r)>>1; 22 if (x<=mid) anss+=sum(root<<1,l,mid,x,y); 23 if (y>mid) anss+=sum(root<<1|1,mid+1,r,x,y); 24 return anss; 25 } 26 int main(){ 27 scanf("%d",&n); 28 while (n){ 29 memset(a,0,sizeof(a)); 30 memset(cnt,0,sizeof(cnt)); 31 memset(tmp,0,sizeof(tmp)); 32 size=0; 33 ans=0; 34 for (int i=1;i<=n;i++){ 35 scanf("%d",&a[i]); 36 tmp[i]=a[i]; 37 } 38 sort(tmp+1,tmp+1+n); 39 size=unique(tmp+1,tmp+1+n)-(tmp+1); 40 for (int i=1;i<=n;i++) 41 a[i]=lower_bound(tmp+1,tmp+1+size,a[i])-(tmp+1)+1; 42 for (int i=1;i<=n;i++){ 43 init(1,1,n,a[i]); 44 ans+=sum(1,1,n,a[i]+1,n); 45 } 46 printf("%lld\n",ans); 47 scanf("%d",&n); 48 } 49 return 0; 50 }
标签:顺序 iostream end -- names text sum out mina
原文地址:http://www.cnblogs.com/Lanly/p/7327844.html