[关闭]
@Acqua 2018-12-08T07:08:26.000000Z 字数 997 阅读 1028

HDU5147 Sequence II(Dec. 8th, 2018)

逆序对 数据结构


题目来源

题意:给定长度为的序列s,求数对满足

思路:
1. 求出所有位置的顺序对数量
2. 维护指针扫描数组,令,则在中小于的数的数量即合法的的数量,这样就确定了第一个顺序对。
3. 反转数组,再次统计顺序对数量,就可以得到在中大于的顺序对数,则



代码:

  1. // La Lune
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. #define wt(x) (x & -x)
  7. using namespace std;
  8. typedef long long lune;
  9. const lune N = 1e5 + 5;
  10. lune n, c[N], seq[N], qes[N], a[N];
  11. inline void add(lune pos, lune x){
  12. for(lune i = pos; i <= n; i += wt(i))
  13. c[i] += x;
  14. }
  15. inline lune que(lune pos){
  16. lune res = 0;
  17. for(lune i = pos; i >= 1; i -= wt(i))
  18. res += c[i];
  19. return res;
  20. }
  21. int main(){
  22. lune T; scanf("%lld", &T);
  23. while(T--){
  24. memset(seq, 0, sizeof(seq));
  25. memset(qes, 0, sizeof(qes));
  26. memset(c, 0, sizeof(c));
  27. scanf("%lld", &n); lune ans = 0;
  28. for(lune i = 1; i <= n; i++){
  29. scanf("%lld", &a[i]);
  30. seq[i] = que(a[i]);
  31. add(a[i], 1);
  32. }
  33. memset(c, 0, sizeof(c));
  34. for(lune i = n; i >= 1; i--){
  35. qes[i] = (n - i) - que(a[i]);
  36. add(a[i], 1);
  37. }
  38. lune res = 0;
  39. for(lune i = 1; i <= n; i++){
  40. ans += res * qes[i];
  41. res += seq[i];
  42. }
  43. printf("%lld\n", ans);
  44. }
  45. return 0;
  46. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注