正文
增加 9 行删除 9 行
22 行未改动232425262728293031323334353637383940414243444522 行未改动### 还有另一种建树状数组的方法,这里就只写倒序的了,反正正序也差不多:```cppsigned main(){scanf("%lld",&n);for(int i=1;i<=n;i++){scanf("%lld",&a[i].v); a[i].idx=i;}stable_sort(a+1,a+1+n,cmp);for(int i=n;i>=1;i--){add(a[i].idx);int ret=query(a[i].idx-1);ans+=ret;}printf("%lld\n",ans);return 0;}```但是,上一种是按值建树,重复元素所找到的pos是一样的,所以用stable_sort还是sort不影响;这种是按位置建树,有重复元素的时候,sort会把相同元素的位置搅乱,所以必须写stable_sort22 行未改动232425262728293031323334353637383940414243444522 行未改动### 还有另一种建树状数组的方法,这里就只写倒序的了,反正正序也差不多:```cppsigned main(){scanf("%lld",&n);for(int i=1;i<=n;i++){scanf("%lld",&a[i].v); a[i].idx=i;}stable_sort(a+1,a+1+n,cmp);for(int i=n;i>=1;i--){add(a[i].idx);int ret=query(a[i].idx-1);ans+=ret;}printf("%lld\n",ans);return 0;}```但是,上一种是按值建树,重复元素所找到的pos是一样的,所以用stable_sort还是sort不影响;这种是按位置建树,有重复元素的时候,sort会把相同元素的位置搅乱,所以必须写stable_sort