Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include<fstream>
  2. using namespace std;
  3.  
  4. int getParent(int index)
  5. {
  6. return index - (index & -index);
  7. }
  8.  
  9. int getNext(int index)
  10. {
  11. return index + (index & -index);
  12. }
  13. void aggiorna(int binaryIndexedTree[], int val, int index, int n)
  14. {
  15. while(index < n)
  16. {
  17. binaryIndexedTree[index] += val;
  18. index = getNext(index);
  19. }
  20. }
  21.  
  22. int ottienisomma(int binaryIndexedTree[], int index)
  23. {
  24. index = index + 1;
  25. int sum = 0;
  26. while(index > 0)
  27. {
  28. sum += binaryIndexedTree[index];
  29. index = getParent(index);
  30. }
  31. return sum;
  32. }
  33.  
  34.  
  35. int main()
  36. {
  37. ifstream in("input.txt");
  38. ofstream out("output.txt");
  39. int A[100000], n;
  40. in>>n;
  41. for(int i=0;i<n;i++)
  42. in>>A[i];
  43. int fenwick[100000]={0};
  44. int valorimaggiori[100000]={0};
  45. int valoriminori[100000]={0};
  46. for(int i=0;i<n;i++)
  47. {
  48. if(A[i]<i+1)
  49. fenwick[i]=1;
  50. valorimaggiori[i]=i-ottienisomma(fenwick, i);
  51. int valoriminimitotali=A[i]-1;
  52. valoriminori[i]=valoriminimitotali-valorimaggiori[i];
  53. aggiorna(fenwick, 1, i, n);
  54. out<<valoriminori[i]+valorimaggiori[i]<<" ";
  55. }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement