fork download
  1. class Solution {
  2. private:
  3. vector<int>tree;
  4.  
  5. int buildTree(int start, int end, int p){
  6. if(start == end) return tree[p] = 1;
  7. int mid = (start+end) / 2;
  8. return tree[p] = buildTree(start,mid,2*p + 1) + buildTree(mid+1,end,2*p + 2);
  9. }
  10.  
  11. static bool cmp(vector<int> &a,vector<int> &b) {
  12. if(a[0] == b[0]) return a[1] > b[1];
  13. return a[0] < b[0];
  14. }
  15.  
  16. void getIndex(int start, int end, int value, int p, int &index) {
  17.  
  18. if(start > end) return;
  19.  
  20. if(start == end){
  21. tree[p]--;
  22. index = start;
  23. return ;
  24. }
  25.  
  26. int mid = (start+end) / 2;
  27. if(tree[2*p+1] - value > 0){
  28. tree[p]--;
  29. getIndex(start,mid,value,2*p+1,index);
  30. }else {
  31. tree[p]--;
  32. value -= tree[2*p+1];
  33. getIndex(mid+1,end,value,2*p+2,index);
  34. }
  35. return ;
  36. }
  37.  
  38. public:
  39. vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
  40.  
  41. sort(people.begin(),people.end(),cmp);
  42.  
  43. int n = people.size();
  44. int size=1;
  45.  
  46. while(size<n)
  47. size*=2;
  48. int arrsize = size-1;
  49. size*=2;
  50. tree.resize(size, 0);
  51.  
  52. buildTree(0,n-1,0);
  53. vector<vector<int>>res(n);
  54.  
  55. for(int i=0; i< n; i++) {
  56. int index = 0;
  57. getIndex(0,n-1,people[i][1],0,index);
  58. res[index].push_back(people[i][0]) ;
  59. res[index].push_back(people[i][1]);
  60. }
  61. return res;
  62. }
  63. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:3:5: error: ‘vector’ does not name a type
     vector<int>tree;
     ^~~~~~
prog.cpp:11:21: error: ‘vector’ has not been declared
     static bool cmp(vector<int> &a,vector<int> &b) {
                     ^~~~~~
prog.cpp:11:27: error: expected ‘,’ or ‘...’ before ‘<’ token
     static bool cmp(vector<int> &a,vector<int> &b) {
                           ^
prog.cpp:39:5: error: ‘vector’ does not name a type
     vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
     ^~~~~~
prog.cpp: In member function ‘int Solution::buildTree(int, int, int)’:
prog.cpp:6:33: error: ‘tree’ was not declared in this scope
         if(start == end) return tree[p] = 1;
                                 ^~~~
prog.cpp:8:16: error: ‘tree’ was not declared in this scope
         return tree[p] = buildTree(start,mid,2*p + 1) + buildTree(mid+1,end,2*p + 2);
                ^~~~
prog.cpp: In static member function ‘static bool Solution::cmp(int)’:
prog.cpp:12:12: error: ‘a’ was not declared in this scope
         if(a[0] == b[0]) return a[1] > b[1];
            ^
prog.cpp:12:20: error: ‘b’ was not declared in this scope
         if(a[0] == b[0]) return a[1] > b[1];
                    ^
prog.cpp:13:16: error: ‘a’ was not declared in this scope
         return a[0] < b[0];
                ^
prog.cpp:13:23: error: ‘b’ was not declared in this scope
         return a[0] < b[0];
                       ^
prog.cpp: In member function ‘void Solution::getIndex(int, int, int, int, int&)’:
prog.cpp:21:13: error: ‘tree’ was not declared in this scope
             tree[p]--;
             ^~~~
prog.cpp:27:12: error: ‘tree’ was not declared in this scope
         if(tree[2*p+1] - value > 0){
            ^~~~
stdout
Standard output is empty