Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int mas[1000001],n;
  4. struct Node {
  5. Node *l,*r;
  6. int left,right;
  7. int sum;
  8. Node (int l2,int r2){
  9. left=l2;
  10. right=r2;
  11. sum=0;
  12. l=r=NULL;
  13. }
  14. };
  15. int cnt=0;
  16. int f (Node *v,int a,int b){
  17. // a left right b
  18. if (!v->l){
  19. v->l=new Node (v->left,((v->left+v->right)>>1));
  20.  
  21. v->r=new Node (((v->left+v->right)>>1)+1,v->right);
  22. }
  23.  
  24. int left=v->left;
  25. int right=v->right;
  26. if (a<=left&&right<=b){
  27. cnt++;
  28. return v->sum;
  29. }
  30. //a b left right left right a b
  31. if (left>b||a>right){
  32. return 0;
  33. }
  34. return f(v->l,a,b)+f(v->r,a,b);
  35. }
  36. void update (Node *v,int a,int x){
  37. if (a==v->left&&a==v->right){
  38. v->sum=x;
  39. return ;
  40. }
  41. if (!v->l){
  42. v->l=new Node (v->left,((v->left+v->right)>>1));
  43.  
  44. v->r=new Node (((v->left+v->right)>>1)+1,v->right);
  45. }
  46. if (v->l->left<=a&&a<=v->l->right){
  47. update(v->l,a,x);
  48. }
  49. else{
  50. update(v->r,a,x);
  51. }
  52. v->sum=v->l->sum+v->r->sum;
  53. }
  54.  
  55. int main () {
  56. Node *root=new Node (1,1000000000);
  57. /*
  58. root->left=1;
  59. root->right=1000000000;
  60. root->sum=0;
  61. es shecdoma iyoo ^^^^^^^^^^^^^^^^
  62. */
  63. update (root,100000000,3);
  64. update (root,5,1);
  65. cout<<f(root,6,100000300)<<endl;
  66. cout<<cnt<<endl;
  67. return 0;
  68. }
  69. /*
  70. _ _ _ _
  71. | \ | | (_) | |
  72. | \| | _ ___ | | __ ___ ___ _ __ __ _
  73. | . ` | | | / __| | |/ / / __| / _ \ | '_ \ / _` |
  74. | |\ | | | | (__ | < \__ \ | (_) | | | | | | (_| |
  75. |_| \_| |_| \___| |_|\_\ |___/ \___/ |_| |_| \__,_|
  76.  
  77. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement