Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n;
- class PersistentSegmentTree
- {
- public:
- struct noduri
- {
- int st, dr, Sum;
- };
- vector <int> radacina;
- vector <noduri> arbore;
- int nodes;
- void Start()
- {
- radacina.resize (n+2);
- arbore.resize (n<<4);
- nodes = 0;
- }
- int copy_node (int nod)
- {
- ++nodes;
- arbore[nodes] = arbore[nod];
- return nodes;
- }
- int adauga_val (int nod, int st, int dr, int val, int poz)
- {
- nod = copy_node(nod);
- if (st == dr)
- {
- arbore[nod].Sum += val;
- return nod;
- }
- int mijloc = (st+dr)>>1;
- if (poz<=mijloc)
- arbore[nod].st = adauga_val (arbore[nod].st, st, mijloc, val, poz);
- else arbore[nod].dr = adauga_val(arbore[nod].dr, mijloc+1, dr, val, poz);
- arbore[nod].Sum = arbore[arbore[nod].st].Sum+arbore[arbore[nod].dr].Sum;
- return nod;
- }
- int Sum_Query (int nod1, int nod2, int st, int dr, int a, int b)
- {
- if (a<=st && dr<=b)
- {
- return arbore[nod2].Sum-arbore[nod1].Sum;
- }
- int mijloc = (st+dr)>>1;
- int Sum_left = 0;
- int Sum_right = 0;
- if (a<=mijloc)
- Sum_left = Sum_Query(arbore[nod1].st, arbore[nod2].st, st, mijloc, a, b);
- if (b>mijloc)
- Sum_right = Sum_Query(arbore[nod1].dr, arbore[nod2].dr, mijloc+1, dr, a, b);
- return Sum_left+Sum_right;
- }
- } Arb;
- int main()
- {
- int q, x, y, a, b;
- ifstream fin ("qxy.in");
- ofstream fout ("qxy.out");
- fin >> n;
- Arb.Start();
- for (int i = 1; i<=n; ++i)
- {
- fin >> x;
- ++x;
- Arb.radacina[i] = Arb.adauga_val(Arb.radacina[i-1], 1, 1001, 1, x);
- }
- fin >> q;
- for (int i = 1; i<=q; ++i)
- {
- fin >> a >> b >> x >> y;
- fout << Arb.Sum_Query(Arb.radacina[a-1], Arb.radacina[b], 1, 1001, x+1, y+1) << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement