gjas06 @ 2019-08-10 19:57:59
为什么一个数放入树状数组后,不直接加上当前位置的对数,而是前一位?
by gjas06 @ 2019-08-10 20:03:17
附上代码```cpp
using namespace std;
long long x[500010],y[500010],z[500010],n,a,b=0,t=0; int lowbit(int x) { return x&-x; } bool cmp(int a,int b) { return x[a]>x[b]; } void build(int a) { int i; for(i=a;i<=n;i+=lowbit(i)) z[i]++; } long long sum(int a) { long long i,b=0; for(i=a;i>0;i-=lowbit(i)) b+=z[i]; return b; }
```c
#include<bits/stdc++.h>
using namespace std;
long long x[500010],y[500010],z[500010],n,a,b=0,t=0;
int lowbit(int x)
{
return x&-x;
}
bool cmp(int a,int b)
{
return x[a]>x[b];
}
void build(int a)
{
int i;
for(i=a;i<=n;i+=lowbit(i))
z[i]++;
}
long long sum(int a)
{
long long i,b=0;
for(i=a;i>0;i-=lowbit(i))
b+=z[i];
return b;
}
by gjas06 @ 2019-08-10 20:03:39
int main()
{
cin>>n;
for(a=1;a<=n;a++)
{ cin>>x[a]; y[a]=a; }
sort(y+1,y+n+1,cmp);
for(a=1;a<=n;a++)
{
if(x[y[a]]==x[y[a+1]]) t++;
else
{
b-=(1+t)*t/2;
t=0;
}
b+=sum(y[a]);
build(y[a]);
}
cout<<b;
return 0;
}
by gjas06 @ 2019-08-11 16:46:30
叫了10几次,终于过了
by gjas06 @ 2019-08-11 16:47:13
初始化应该将相同的数根据坐标从大到小排
by gjas06 @ 2019-08-11 16:56:49
cmp改为return x[a]>x[b]||x[a]==x[b]&&a>b;
by PAN_盼达明 @ 2019-08-29 20:02:16
暖?