fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <set>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. struct node
  8. {
  9. int value, position;
  10. bool left, right;
  11. bool operator < (const node& a) const
  12. {
  13. return value < a.value;
  14. }
  15. };
  16.  
  17. int main()
  18. {
  19. int n;
  20. cin >> n;
  21.  
  22. vector < node > a(n);
  23. set < node > s;
  24.  
  25. for (auto &i: a)
  26. {
  27. cin >> i.value;
  28. i.left=i.right=0;
  29. }
  30.  
  31. a[0].position=1;
  32. s.insert(a[0]);
  33.  
  34. for (int i=1; i<n; i++)
  35. {
  36. auto it=s.upper_bound(a[i]);
  37. auto it2=it; --it2;
  38. if (it==s.begin())
  39. {
  40. a[i].position=2*it->position;
  41. s.insert(a[i]);
  42. it->left=1;
  43. }
  44. else if (it==s.end())
  45. {
  46. a[i].position=2*(--it)->position+1;
  47. s.insert(a[i]);
  48. it->right=1;
  49. }
  50. else
  51. {
  52. if (it2->right==0)
  53. {
  54. a[i].position=2*it2->position+1;
  55. s.insert(a[i]);
  56. it2->right=1;
  57. }
  58. else
  59. {
  60. a[i].position=2*it->position;
  61. s.insert(a[i]);
  62. it->left=1;
  63. }
  64. }
  65. }
  66.  
  67. for (auto i: a) cout << i.position << ' ';
  68. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:42:12: error: assignment of member 'node::left' in read-only object
    it->left=1;
            ^
prog.cpp:48:13: error: assignment of member 'node::right' in read-only object
    it->right=1;
             ^
prog.cpp:56:15: error: assignment of member 'node::right' in read-only object
     it2->right=1;
               ^
prog.cpp:62:13: error: assignment of member 'node::left' in read-only object
     it->left=1;
             ^
stdout
Standard output is empty