-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1908.cpp
More file actions
68 lines (56 loc) · 807 Bytes
/
1908.cpp
File metadata and controls
68 lines (56 loc) · 807 Bytes
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
#include<iostream>
using namespace std;
int n,now[500010],next[500010];
long long sum=0;
void merge(int l,int r)
{
if(l==r)
return;
int mid=(l+r)/2;
merge(l,mid);
merge(mid+1,r);
int left=l,right=mid+1,count=l;
while(left<=mid&&right<=r)
{
if(now[left]<=now[right])
{
next[count]=now[left];
left++;
count++;
}
else
{
next[count]=now[right];
count++;
right++;
sum+=mid-left+1;
}
}
while(left<=mid)
{
next[count]=now[left];
count++;
left++;
}
while(right<=r)
{
next[count]=now[right];
right++;
count++;
}
for(int i=l;i<=r;i++)
{
now[i]=next[i];
}
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>now[i];
}
merge(0,n-1);
cout<<sum;
return 0;
}