Rkhamim

Untitled

Sep 15th, 2021
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. long long int a[500005],b[500005],c[500005];
  4. #define M 998244353
  5. struct Node{
  6. int info;
  7. Node* left;
  8. Node* right;
  9. int child;
  10. };
  11. int Inorder(Node *cur,int z) {
  12. //if (z==0) return cur->info;
  13. int x=0;
  14. if(cur->left!=nullptr)x=cur->left->child;
  15. // cout<<x<<endl;
  16. if(cur->left!=nullptr&&cur->left->child>=z)
  17. {
  18. return Inorder(cur->left,z);
  19. }
  20. else if(x+1==z)return cur->info;
  21. else return Inorder(cur->right,z-x-1);
  22. }
  23. Node* getpos(Node* cur,int val)
  24. {
  25. cur->child++;
  26. if(cur->info>val)
  27. {
  28. if(cur->left==nullptr)return cur;
  29. return getpos(cur->left,val);
  30. }
  31. else
  32. {
  33. if(cur->right==nullptr)return cur;
  34. return getpos(cur->right,val);
  35. }
  36. }
  37. int main()
  38. {
  39. ios_base::sync_with_stdio(false);
  40. cin.tie(NULL);
  41. long long int t,i,j,k,w,y,z,n,m,x,l,r;
  42. cin>>x>>n;
  43. z=1;
  44. Node* root=new Node;
  45. root->info=x;
  46. root->left=nullptr;
  47. root->right=nullptr;
  48. root->child=1;
  49. for(i=1;i<=n;i++)
  50. {
  51. cin>>x>>y;
  52. Node* cur=new Node;
  53. cur->info=x;
  54. cur->child=1;
  55. cur->left=nullptr;
  56. cur->right=nullptr;
  57. Node* pos=getpos(root,x);
  58. if(pos->info>x)
  59. {
  60. pos->left=cur;
  61. }
  62. else
  63. {
  64. pos->right=cur;
  65. }
  66. Node* cur1=new Node;
  67. cur1->info=y;
  68. cur1->child=1;
  69. cur1->left=nullptr;
  70. cur1->right=nullptr;
  71. Node* pos1=getpos(root,y);
  72. if(pos1->info>y)
  73. {
  74. pos1->left=cur1;
  75. }
  76. else
  77. {
  78. pos1->right=cur1;
  79. }
  80. z++;
  81. cout<<Inorder(root,z)<<endl;
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment