Advertisement
vladm98

Untitled

Sep 5th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. int n;
  5. class PersistentSegmentTree
  6. {
  7. public:
  8. struct noduri
  9. {
  10. int st, dr, Sum;
  11. };
  12. vector <int> radacina;
  13. vector <noduri> arbore;
  14. int nodes;
  15. void Start()
  16. {
  17. radacina.resize (n+2);
  18. arbore.resize (n<<4);
  19. nodes = 0;
  20. }
  21. int copy_node (int nod)
  22. {
  23. ++nodes;
  24. arbore[nodes] = arbore[nod];
  25. return nodes;
  26. }
  27. int adauga_val (int nod, int st, int dr, int val, int poz)
  28. {
  29. nod = copy_node(nod);
  30. if (st == dr)
  31. {
  32. arbore[nod].Sum += val;
  33. return nod;
  34. }
  35. int mijloc = (st+dr)>>1;
  36. if (poz<=mijloc)
  37. arbore[nod].st = adauga_val (arbore[nod].st, st, mijloc, val, poz);
  38. else arbore[nod].dr = adauga_val(arbore[nod].dr, mijloc+1, dr, val, poz);
  39. arbore[nod].Sum = arbore[arbore[nod].st].Sum+arbore[arbore[nod].dr].Sum;
  40. return nod;
  41. }
  42. int Sum_Query (int nod1, int nod2, int st, int dr, int a, int b)
  43. {
  44. if (a<=st && dr<=b)
  45. {
  46. return arbore[nod2].Sum-arbore[nod1].Sum;
  47. }
  48. int mijloc = (st+dr)>>1;
  49. int Sum_left = 0;
  50. int Sum_right = 0;
  51. if (a<=mijloc)
  52. Sum_left = Sum_Query(arbore[nod1].st, arbore[nod2].st, st, mijloc, a, b);
  53. if (b>mijloc)
  54. Sum_right = Sum_Query(arbore[nod1].dr, arbore[nod2].dr, mijloc+1, dr, a, b);
  55. return Sum_left+Sum_right;
  56. }
  57. } Arb;
  58. int main()
  59. {
  60. int q, x, y, a, b;
  61. ifstream fin ("qxy.in");
  62. ofstream fout ("qxy.out");
  63. fin >> n;
  64. Arb.Start();
  65. for (int i = 1; i<=n; ++i)
  66. {
  67. fin >> x;
  68. ++x;
  69. Arb.radacina[i] = Arb.adauga_val(Arb.radacina[i-1], 1, 1001, 1, x);
  70. }
  71. fin >> q;
  72. for (int i = 1; i<=q; ++i)
  73. {
  74. fin >> a >> b >> x >> y;
  75. fout << Arb.Sum_Query(Arb.radacina[a-1], Arb.radacina[b], 1, 1001, x+1, y+1) << '\n';
  76. }
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement