Advertisement
a53

SecventaK

a53
Dec 30th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. using namespace std;
  4.  
  5. ifstream f("secventak.in");
  6. ofstream g("secventak.out");
  7. long long n,i,sol, suma,a[100001],b[100001],poz[100001],aib[100001],k,x,j;
  8.  
  9. void quik(long long inf, long long sup) {
  10. long long x, i, j, t;
  11. i = inf;
  12. j = sup;
  13. x = a[(i + j) / 2];
  14. do {
  15. while ( (i < sup) && (a[i] < x) ) i++;
  16. while ( (j > inf) &&(a[j] > x) ) j--;
  17. if ( i<= j ) {
  18. t = a[i];
  19. a[i] = a[j];
  20. a[j] = t;
  21. t=poz[i];
  22. poz[i]=poz[j];
  23. poz[j]=t;
  24. i++;
  25. j--;
  26. }
  27. } while ( i <= j );
  28. if ( inf < j ) quik(inf, j);
  29. if ( i < sup ) quik(i, sup);
  30. }
  31.  
  32. void sor(long long inf, long long sup) {
  33. long long x, i1, j, t;
  34. i1 = inf;
  35. j = sup;
  36. x = poz[(i1 + j) / 2];
  37. do {
  38. while ( (i1 < sup) && (poz[i1] < x) ) i1++;
  39. while ( (j > inf) &&(poz[j] > x) ) j--;
  40. if ( i1<= j ) {
  41.  
  42. t=poz[i1];
  43. poz[i1]=poz[j];
  44. poz[j]=t;
  45. i1++;
  46. j--;
  47. }
  48. } while ( i1 <= j );
  49. if ( inf < j ) sor(inf, j);
  50. if ( i1 < sup ) sor(i1, sup);
  51. }
  52.  
  53.  
  54. int zeros(int x)
  55. {
  56. return (x^(x-1))&x ;
  57. }
  58.  
  59. void add(int x)
  60. {
  61. int j;
  62. for(j=x; j<=n;j+=zeros(j)) aib[j]++;
  63. }
  64.  
  65. int cal(int x)
  66. {
  67. int j, ret=0;
  68. for(j=x; j>0; j-=zeros(j)) ret+=aib[j];
  69. return ret;
  70. }
  71.  
  72. int main()
  73. {
  74. f>>n>>k;
  75. suma=0;
  76. for(i=1;i<=n;i++)
  77. {
  78. f>>x;
  79. suma=suma+x;
  80. a[i]=suma-k*i;
  81. poz[i]=i;
  82. }
  83.  
  84. quik(1,n);
  85. i=1;
  86. while(i<n)
  87. {
  88. if(a[i]==a[i+1])
  89. {
  90. j=i;
  91. while((a[i]==a[i+1])and(i<n))i++;
  92. sor(j,i);
  93. }
  94. else i++;
  95.  
  96. }
  97. for(i=1;i<=n;i++)b[poz[i]]=i;
  98. for(i=1;i<=n;i++)
  99. {
  100. sol=sol+cal(b[i]);
  101. if(a[b[i]]>=0)sol++;
  102. add(b[i]);
  103. }
  104. g<<sol;
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement