牛客练习赛7 E 珂朵莉的数列(树状数组+爆long long解决方法)

 https://www.nowcoder.com/acm/contest/38/E

题意:

 

思路:

 树状数组维护。从大佬那里学习了如何处理爆long long的方法。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 typedef long long ll;
 7 const int maxn = 1000000+100;
 8 
 9 int n;
10 int a[maxn],b[maxn];
11 ll c[maxn];
12 
13 int lowbit(int x)
14 {
15     return x&(-x);
16 }
17 
18 void add(int x ,int d)
19 {
20     while(x<maxn)
21     {
22         c[x] += d;
23         x += lowbit(x);
24     }
25 }
26 
27 ll sum(int x)
28 {
29     ll ret = 0;
30     while(x>0)
31     {
32         ret += c[x];
33         x -= lowbit(x);
34     }
35     return ret;
36 }
37 
38 int main()
39 {
40     //freopen("in.txt","r",stdin);
41     scanf("%d",&n);
42     for(int i=1;i<=n;i++)
43     {
44         scanf("%d",&a[i]);
45         b[i] = a[i];
46     }
47     sort(b+1,b+n+1);
48     int num = unique(b+1,b+n+1)-(b+1);
49     ll ans1 = 0, ans2 = 0;
50     ll mx = 1e18;
51     for(int i=1;i<=n;i++)
52     {
53         a[i] = lower_bound(b+1,b+n+1,a[i])-(b+1);
54         a[i]++;
55         ans1 += (ll)(n-i+1)*(sum(n)-sum(a[i]));
56         if(ans1>=mx)  ans2 += ans1/mx, ans1%=mx;
57         add(a[i],i);
58     }
59     if(ans2)  printf("%lld%018lld\n",ans2,ans1);
60     else printf("%lld\n",ans1);
61     return 0;
62 }

 

posted @ 2017-12-02 09:40  Kayden_Cheung  阅读(454)  评论(0编辑  收藏  举报
//目录