Advertisement
Guest User

Segment Tree (No Lazy)

a guest
Feb 7th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. /*
  2. Paradeigma tou Segment Tree:
  3. Otan pianei to athrisma twn stoixeiwn
  4. Kai otan ginete update prosthetei kapio sigkekrimeno arithmo stis theseis
  5. Liga logia gia avto:
  6. To segment tree einai ena binary tree data strucure pou o kathe "komvos"
  7. apothikevkei mia geniki timi gia to interval pou einai ipevthino
  8. diladi an apothikevoume athrismata(paradeigma mas) tote o "komvos" pou en ipevthinos gia
  9. tin perioxi 1-8 xerei to athrisma twn stoixeiwn apo to 1 ws to 8
  10. Avto petyxenete apo to oti pernei tin pliroforia apo ta paidia tou(binary tree)
  11. opou to arsistero paidi eshi p tin arxi ws tin mesi (dld apothikevei to athrisma p to 1 ws 4)
  12. kai to dexi paidi eshi p tin mesi+1(gia na men iparxoun colisions) ws to telos(dld apothikevei to athrisma p to 5 ws 8)
  13. alla an einai fyllo dld exei tin pliroforia gia ena stoixeio aplos pianoume tin pliroforia p ton pinaka mas
  14. ston kodika einai: seg[cth]=pin[s];
  15. */
  16. #include<iostream>
  17.  
  18. using namespace std;
  19.  
  20. int pin[100000];///Pinakas me to data
  21. int seg[200010];///To segment tree mas
  22. int n;///To plithos twn stoixeiwn
  23.  
  24. ///To recurence relation
  25. int rec(int a, int b)
  26. {
  27.     ///Edw tha grafoume tin sxesi tou patera me ta paidia tou
  28.     ///Sigkekrimena to pateras einai to athrisma twn 2 paidiwn
  29.     ///Tha mporouse na einai to pio megalo apo ta 2
  30.     ///tote tha stelname:
  31.     ///return max(a, b);
  32.     return a+b;
  33. }
  34.  
  35. ///Gia arxikopoiisi tou Segment Tree
  36. ///s=>arxi t interval
  37. ///e=>telos t inetrval
  38. ///cth=> pou eimaste sto Segment Tree
  39. void reset(int s, int e, int cth)
  40. {
  41.     ///Leaf Periptosi
  42.     if(s==e)///Otan I arxi en ison tou telous
  43.     {
  44.         seg[cth]=0; ///midenizoume
  45.         return; ///klinoume to maxazi
  46.     }
  47.  
  48.     ///Oxi Leaf Periptosi
  49.     int mid=(s+e)/2;///Vriskoume to middle
  50.  
  51.     reset(s, mid, cth*2);///pame sto aristero paidi
  52.     reset(mid+1, e, cth*2+1);///pame sto dexi paidi
  53.  
  54.     ///To recurence relation!!!(stelnoume ta 2 paithkia)
  55.     seg[cth]=rec(seg[cth*2], seg[cth*2+1]);
  56.  
  57.     return;///klinoume to maxazi
  58. }
  59.  
  60. ///Gia na dwsoume times sto Segment Tree
  61. ///s=>arxi t interval
  62. ///e=>telos t inetrval
  63. ///cth=>pou eimaste sto Segment Tree
  64. void create(int s, int e, int cth)
  65. {
  66.     ///Leaf Periptosi
  67.     if(s==e)///Otan I arxi en ison tou telous
  68.     {
  69.         seg[cth]=pin[s];///Dioume stin akrea periptosi tin timi
  70.         return;
  71.     }
  72.  
  73.     int mid=(s+e)/2;///Vriskoume to middle
  74.  
  75.     create(s, mid, cth*2);///pame sto aristero paidi
  76.     create(mid+1, e, cth*2+1);///pame sto dexi paidi
  77.  
  78.     ///To recurence relation!!!(stelnoume ta 2 paithkia)
  79.     seg[cth]=rec(seg[cth*2], seg[cth*2+1]);
  80.  
  81.     return;
  82. }
  83.  
  84. ///Otan theloume na antlisoume plirofories apo to segment tree
  85. ///ters=>arxi avtoy p theloume
  86. ///tere=>telos avtoy p theloume
  87. ///s=>arxi t interval
  88. ///e=>telos t inetrval
  89. ///cth=>pou eimaste sto Segment Tree
  90. int query(int ters, int tere, int s, int e, int cth)
  91. {
  92.     ///an fkoume ektos oriwn
  93.     if(tere<s || ters>e)
  94.     {
  95.         ///epistrefoume mia timi pou den tha mas epireasei
  96.         ///px sto athrisma to 0
  97.         return 0;
  98.     }
  99.  
  100.     ///an o "komvos" en mes sta oreia mas
  101.     if(ters<=s && e<=tere)
  102.     {
  103.         ///aplos stelnoume tin timi
  104.         return seg[cth];
  105.     }
  106.  
  107.     int mid=(s+e)/2;///Vriskoume to middle
  108.  
  109.     ///Pianoume pliroforia apo to aristero paidi
  110.     int part1=query(ters, tere, s, mid, cth*2);
  111.  
  112.     ///Pianoume pliroforia apo to dexi paidi
  113.     int part2=query(ters, tere, mid+1, e, cth*2+1);
  114.  
  115.     ///Epistrefoume to recurence relation
  116.     return rec(part1, part2);
  117. }
  118.  
  119. ///Otan theloume na ananeosoume plirofories apo to segment tree
  120. ///Edw aplos prosthetoume to num sta stoixeia
  121. ///ters=>arxi avtoy p theloume
  122. ///tere=>telos avtoy p theloume
  123. ///s=>arxi t interval
  124. ///e=>telos t inetrval
  125. ///num=>timi alagis
  126. ///cth=>pou eimaste sto Segment Tree
  127. void updall(int ters, int tere, int s, int e, int num, int cth)
  128. {
  129.     ///an fkoume ektos oriwn
  130.     if(tere<s || ters>e)
  131.     {
  132.         ///klinoume to maxazi
  133.         return;
  134.     }
  135.  
  136.     ///otan ftasoume se fyllo
  137.     if(s==e)
  138.     {
  139.         ///aplos prothetoume
  140.         seg[cth]+=num;
  141.         ///klinoume to maxazi
  142.         return;
  143.     }
  144.  
  145.     int mid=(s+e)/2;///Vriskoume to middle
  146.  
  147.     ///kanoume update to aristero paidi
  148.     updall(ters, tere, s, mid, num, cth*2);
  149.     ///kanoume update to dexi paidi
  150.     updall(ters, tere, mid+1, e, num, cth*2+1);
  151.  
  152.     ///To recurence relation!!!(stelnoume ta 2 paithkia)
  153.     seg[cth]=rec(seg[cth*2], seg[cth*2+1]);
  154.  
  155.     return;///klinoume to maxazi
  156. }
  157.  
  158. ///Ena paradeigma pws doulefkoun ta Functions
  159. int main()
  160. {
  161.  
  162.     int i;///Gia to for
  163.     cin>>n;///Diavazoume ta stoixeia
  164.     reset(1, n, 1);///midenizoume to segment tree
  165.     ///diavazoume ta stoixeia
  166.     for(i=1; i<=n; i++)
  167.         cin>>pin[i];
  168.     ///diavazoume ta stoixeia
  169.  
  170.     ///Dimiourgoume to segment tree
  171.     create(1, n, 1);
  172.  
  173.     ///Typwnw to athrisma twn stoixeiwn 1->n
  174.     cout<<query(1, n, 1, n, 1)<<"\n";
  175.  
  176.     ///Mexri tin mesi proisthetw 15 se kathe stoixeio
  177.     updall(1, n/2, 1, n, 15, 1);
  178.  
  179.     ///kai thorw tin mageia pou egine ;)
  180.     cout<<query(1, n, 1, n, 1)<<"\n";
  181.  
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement