fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAX_N = 1000000;
  5. int t[MAX_N];
  6. int n;
  7.  
  8. int read(int x)
  9. {
  10. int res = 0;
  11. for(;x>0;x-=(x&-x))res+=t[x]; // zastosowany jest tu sprytny trick , który zostanie wytłumaczony w później
  12. return res;
  13. }//funkcja pobiera wartość danego elementu w czasie O(lgN)
  14. void update(int x ,int w)
  15. {
  16. for(;x<=n;x+=(x&-x))t[x]+=w; // n to rozmiar wejścia
  17. }//funkcja aktualizuje dany element , oraz wszystkie inne go zawierające , w czasie O(lgN)
  18.  
  19. int main() {
  20. cin >> n;
  21. int x;
  22. for(int i = 0; i < n; ++i)
  23. {
  24. cin >> x;
  25. update(x, 1);
  26. }
  27.  
  28. cout << read(1);
  29. return 0;
  30. }
Success #stdin #stdout 0s 4296KB
stdin
10 
1 90 5 3 2 1 23 68 12 102
stdout
2