RainX_69

HACKEREARTH SEGMENT TREE PROBLEMS

Mar 1st, 2023
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.77 KB | Source Code | 0 0
  1. 1) https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/shivam-and-expensive-birthday-gift-da58b2f0/
  2.  
  3. 2) https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/distinct-integers-in-range-66eca44b/
  4.  
  5. 3) https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/k-th-bit-faae0e0d/
  6.  
  7. 4) https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/easy-queries-751f9372/?sort=recent-comments
  8.  
  9. ---------------------------------------------------------------------------------------------------------------------------------------
  10.  
  11. Answer 1 ->
  12. #include<bits/stdc++.h>
  13. using namespace std;
  14.  
  15. vector<long long> tree,arr;
  16.  
  17. void update(int start, int end, int parent, int index, int type){
  18.     if(start==end){
  19.         if(type==1){
  20.             arr[start]++;
  21.             tree[parent]++;
  22.         }
  23.         if(type==2 && arr[start]>0){
  24.             arr[start]--;
  25.             tree[parent]--;
  26.         }
  27.         return;
  28.     }
  29.     int mid=(start+end)/2;
  30.     if(index>mid){
  31.         update(mid+1,end,2*parent+2,index,type);    
  32.     }
  33.     else{
  34.         update(start,mid,2*parent+1,index,type);
  35.     }
  36.     tree[parent]=tree[2*parent+1]+tree[2*parent+2];
  37. }
  38.  
  39. long long query(int start, int end, int parent, int qstart, int qend){
  40.     if(qstart>end || qend<start){
  41.         return 0;
  42.     }
  43.     if(qstart<=start && qend>=end){
  44.         return tree[parent];
  45.     }
  46.     int mid=(start+end)/2;
  47.     long long L=query(start,mid,2*parent+1,qstart,qend);
  48.     long long R=query(mid+1,end,2*parent+2,qstart,qend);
  49.     return L+R;
  50. }
  51.  
  52. int main(){
  53.     long long N,Q;
  54.     scanf("%lld %lld ", &N, &Q);
  55.    
  56.     tree.resize(4*N+5,0);
  57.     arr.resize(N,0);
  58.  
  59.     for(int i=0;i<Q;i++){
  60.         long long type;
  61.         scanf("%lld", &type);
  62.         if(type==1 || type==2){
  63.             long long index;
  64.             scanf("%lld", &index);
  65.             update(0,N-1,0,index-1,type);
  66.         }
  67.         else{
  68.             long long l,r;
  69.             scanf("%lld %lld", &l, &r);
  70.             int ans=query(0,N-1,0,l-1,r-1);
  71.             cout<<ans<<endl;
  72.         }
  73.     }
  74. }
  75.  
  76. ---------------------------------------------------------------------------------------------------------------------------------------
  77. Answer 2->
  78. #include <bits/stdc++.h>
  79. using namespace std;
  80.  
  81. vector<bitset<5001>> treeA,treeB;
  82. vector<int> A,B;
  83.  
  84. void build(int start, int end, int parent){
  85.     if(start>end){
  86.         return;
  87.     }
  88.     if(start==end){
  89.         treeA[parent].set(A[start]);
  90.         treeB[parent].set(B[start]);
  91.         return;
  92.     }
  93.     int mid=(start+end)/2;
  94.     build(start,mid,2*parent+1);
  95.     build(mid+1,end,2*parent+2);
  96.     treeA[parent]=treeA[2*parent+1] | treeA[2*parent+2];
  97.     treeB[parent]=treeB[2*parent+1] | treeB[2*parent+2];
  98.     return;
  99. }
  100.  
  101. bitset<5001> query(int start, int end, int parent, int qstart, int qend, char type){
  102.     if(qend<start || qstart>end){
  103.         return bitset<5001>();
  104.     }
  105.     if(qstart<=start && qend>=end){
  106.         if(type=='A'){
  107.             return treeA[parent];
  108.         }
  109.         else{
  110.             return treeB[parent];
  111.         }
  112.     }
  113.     int mid=(start+end)/2;
  114.     auto L=query(start,mid,2*parent+1,qstart,qend,type);
  115.     auto R=query(mid+1,end,2*parent+2,qstart,qend,type);
  116.     return L | R;
  117. }
  118.  
  119.  
  120. int main() {
  121.     int N;
  122.     cin>>N;
  123.  
  124.     A.resize(N);
  125.     B.resize(N);
  126.  
  127.     for(int i=0;i<N;i++){
  128.         cin>>A[i];
  129.     }
  130.     for(int i=0;i<N;i++){
  131.         cin>>B[i];
  132.     }
  133.    
  134.     treeA.resize(4*N+5);
  135.     treeB.resize(4*N+5);
  136.  
  137.     build(0,N-1,0);
  138.  
  139.     int Q;
  140.     cin>>Q;
  141.    
  142.     while(Q--){
  143.         int a,b,c,d;
  144.         cin>>a>>b>>c>>d;
  145.         auto AQ=query(0,N-1,0,a-1,b-1,'A');
  146.         auto BQ=query(0,N-1,0,c-1,d-1,'B');
  147.         cout<<(AQ | BQ).count()<<endl;
  148.     }
  149. }
  150. ---------------------------------------------------------------------------------------------------------------------------------------
  151. Answer 3->
  152. #include <bits/stdc++.h>
  153. using namespace std;
  154.  
  155. vector<int> tree;
  156.  
  157. void build(int start, int end, int parent){
  158.     if(start==end){
  159.         tree[parent]=1;
  160.         return;
  161.     }
  162.     int mid=(start+end)/2;
  163.     build(start,mid,2*parent+1);
  164.     build(mid+1,end,2*parent+2);
  165.     tree[parent]=tree[2*parent+1]+tree[2*parent+2];
  166. }
  167.  
  168. void update(int start, int end, int parent, int index){
  169.     if(start==end){
  170.         tree[parent]=0;
  171.         return;
  172.     }
  173.     int mid=(start+end)/2;
  174.     if(index>mid){
  175.         update(mid+1,end,2*parent+2,index);    
  176.     }
  177.     else{
  178.         update(start,mid,2*parent+1,index);
  179.     }
  180.     tree[parent]=tree[2*parent+1]+tree[2*parent+2];
  181. }
  182.  
  183. int query(int start, int end, int parent, int K){
  184.     if(start==end){
  185.         return K==0 ? start : INT_MAX;
  186.     }
  187.     int mid=(start+end)/2;
  188.     if(tree[2*parent+1]>K){
  189.         return query(start,mid,2*parent+1,K);
  190.     }
  191.     else{
  192.         return query(mid+1,end,2*parent+2,K-tree[2*parent+1]);
  193.     }
  194. }
  195.  
  196. int main() {
  197.     int N;
  198.     cin>>N;
  199.  
  200.     tree.resize(4*N+5);
  201.     build(0,N-1,0);
  202.  
  203.     int Q;
  204.     cin>>Q;
  205.  
  206.     while(Q--){
  207.         int type;
  208.         cin>>type;
  209.         if(type==1){
  210.             int K;
  211.             cin>>K;
  212.             int ans=query(0,N-1,0,K-1);
  213.             if(ans==INT_MAX){
  214.                 cout<<-1<<endl;
  215.             }          
  216.             else{
  217.                 cout<<ans+1<<endl;
  218.             }
  219.         }
  220.         else{
  221.             int index;
  222.             cin>>index;
  223.             update(0,N-1,0,index-1);
  224.         }
  225.     }
  226. }
  227. ---------------------------------------------------------------------------------------------------------------------------------------
  228. Answer 4->
  229. /*
  230. #include <bits/stdc++.h>
  231. using namespace std;
  232.  
  233. pair<int,int> tree[4*100001+5];  // {first,second}={min,max}
  234. int arr[100001];
  235.  
  236. void build(int start, int end, int parent){
  237.     if(start==end){
  238.         if(arr[start]==1){
  239.             tree[parent]={start,start};
  240.         }
  241.         else{
  242.             tree[parent]={INT_MIN,INT_MAX};
  243.         }
  244.         return;
  245.     }
  246.     int mid=(start+end)/2;
  247.     build(start,mid,2*parent+1);
  248.     build(mid+1,end,2*parent+2);
  249.     tree[parent].first=max(tree[2*parent+1].first,tree[2*parent+2].first);
  250.     tree[parent].second=min(tree[2*parent+1].second,tree[2*parent+2].second);
  251.     return;
  252. }
  253.  
  254. void update(int start, int end, int parent, int index){
  255.     if(start==end){
  256.         tree[parent]={start,start};
  257.         return;
  258.     }
  259.     int mid=(start+end)/2;
  260.     if(index>mid){
  261.         update(mid+1,end,2*parent+2,index);
  262.     }
  263.     else{
  264.         update(start,mid,2*parent+1,index);    
  265.     }
  266.     tree[parent].first=max(tree[2*parent+1].first,tree[2*parent+2].first);
  267.     tree[parent].second=min(tree[2*parent+1].second,tree[2*parent+2].second);
  268. }
  269.  
  270. int query(int start, int end, int parent, int qstart, int qend, char type){  
  271.     if(qstart>end || qend<start){
  272.         return type=='L' ? INT_MIN : INT_MAX;
  273.     }
  274.     if(qstart<=start && qend>=end){
  275.         return type=='L' ? tree[parent].first : tree[parent].second;
  276.     }
  277.     int mid=(start+end)/2;
  278.     int Lans=query(start,mid,2*parent+1,qstart,qend,type);
  279.     int Rans=query(mid+1,end,2*parent+2,qstart,qend,type);
  280.     return type=='L' ? max(Lans,Rans) : min(Lans,Rans);
  281. }
  282.  
  283. int main() {
  284.     int N;
  285.     cin>>N;
  286.    
  287.     int Q;
  288.     cin>>Q;
  289.  
  290.     for(int i=0;i<N;i++){
  291.         cin>>arr[i];
  292.     }
  293.  
  294.     build(0,N-1,0);
  295.  
  296.     while(Q--){
  297.         int type,index;
  298.         cin>>type>>index;
  299.  
  300.         if(type==0){
  301.             int L=query(0,N-1,0,0,index-1,'L');
  302.             int R=query(0,N-1,0,index+1,N-1,'R');
  303.             if(L==INT_MIN){
  304.                 L=-1;
  305.             }
  306.             if(R==INT_MAX){
  307.                 R=-1;
  308.             }
  309.             cout<<L<<" "<<R<<endl;
  310.         }
  311.         else if(arr[index]==0){
  312.             arr[index]=1;
  313.             update(0,N-1,0,index);
  314.         }
  315.     }
  316. }
  317. */
  318.  
  319. #include <bits/stdc++.h>
  320. using namespace std;
  321.  
  322. int main() {
  323.     int N;
  324.     cin>>N;
  325.    
  326.     int Q;
  327.     cin>>Q;
  328.  
  329.     vector<int> arr(N);
  330.     set<int> s;
  331.  
  332.     for(int i=0;i<N;i++){
  333.         cin>>arr[i];
  334.         if(arr[i]==1){
  335.             s.insert(i);
  336.         }
  337.     }
  338.  
  339.     while(Q--){
  340.         int type,index;
  341.         cin>>type>>index;
  342.  
  343.         if(type==0){
  344.             int L=-1;
  345.             int R=-1;
  346.             auto itr1=s.upper_bound(index-1);
  347.             auto itr2=s.upper_bound(index);
  348.             if(itr1!=s.begin()){
  349.                 L=*prev(itr1);
  350.             }
  351.             if(itr2!=s.end()){
  352.                 R=*itr2;
  353.             }
  354.             cout<<L<<" "<<R<<endl;
  355.         }
  356.         else if(arr[index]==0){
  357.             arr[index]=1;
  358.             s.insert(index);
  359.         }
  360.     }
  361. }
  362.  
Advertisement
Add Comment
Please, Sign In to add comment