Codeforces #261 D
D. Pashmak and Parmida's problem
time limit per test
3 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputParmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r andak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).
Help Pashmak with the test.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the answer to the problem.
简单的树状数组求逆序数。。。开始怎么也没想到。。。
答案没用long long wa一次
1 #include <cstring> 2 3 #include <iostream> 4 5 #include <algorithm> 6 7 #include <cstdio> 8 9 #include <cmath> 10 11 #include <map> 12 13 #include <cstdlib> 14 15 #define M(a,b) memset(a,b,sizeof(a)) 16 17 using namespace std; 18 19 20 21 int n; 22 23 int num[1000006]; 24 25 int savefro[1000006],saveb[1000006]; 26 27 int ans[1000006]; 28 29 int flag[1000006]; 30 31 map<int,int> front,back; 32 33 34 35 int bit[2000006],cnn; 36 37 38 39 int sum(int i) 40 41 { 42 43 int res = 0; 44 45 while(i>0) 46 47 { 48 49 res+=bit[i]; 50 51 i-=i&-i; 52 53 } 54 55 return res; 56 57 } 58 59 60 61 void add(int i,int x) 62 63 { 64 65 while(i<=cnn) 66 67 { 68 69 bit[i]+=x; 70 71 i+=i&-i; 72 73 } 74 75 } 76 77 78 79 int main() 80 81 { 82 83 while(scanf("%d",&n)==1) 84 85 { 86 87 front.clear(); 88 89 back.clear(); 90 91 M(saveb,0); 92 93 M(savefro,0); 94 95 M(ans,0); 96 97 M(flag,0); 98 99 M(bit,0); 100 101 long long res = 0; 102 103 for(int i = 0;i<n;i++) 104 105 { 106 107 scanf("%d",&num[i]); 108 109 } 110 111 for(int i = 0;i<n;i++) 112 113 { 114 115 savefro[i] = ++front[num[i]]; 116 117 } 118 119 cnn = n; 120 121 for(int i = n-1;i>=0;i--) 122 123 { 124 125 saveb[i] = ++back[num[i]]; 126 127 add(saveb[i],1); 128 129 res+=sum(savefro[i-1]-1); 130 131 // cout<<' '<<sum(savefro[i-1]-1)<<' '<<savefro[i-1]<<endl; 132 133 } 134 135 136 137 /*cout<<"savefro"<<endl; 138 139 for(int i = 0;i<n;i++) 140 141 { 142 143 cout<<savefro[i]<<' '; 144 145 } 146 147 cout<<endl; 148 149 150 151 cout<<"saveb"<<endl; 152 153 for(int i = 0;i<n;i++) 154 155 { 156 157 cout<<saveb[i]<<' '; 158 159 } 160 161 cout<<endl; 162 163 164 165 cout<<"flag"<<endl; 166 167 for(int i = 0;i<n;i++) 168 169 { 170 171 cout<<flag[i]<<' '; 172 173 } 174 175 cout<<endl;*/ 176 177 printf("%I64d\n",res); 178 179 } 180 181 return 0; 182 183 }