Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <string>
- #include <algorithm>
- #include <map>
- using namespace std;
- using ll = long long;
- #define pii pair <int,int>
- void cv(vector <int> v){
- for (auto x: v) cout<<x<<' ';
- cout<<'\n';
- }
- struct sct{
- int l,r,mn,mx,sum, zeros, ch1,ch2;
- };
- void csc(sct x){
- cout<<"l = "<<x.l<<" r = "<<x.r<<" mn = "<<x.mn<<" mx = "<<x.mx<<" sum = "<<x.sum<<"zeros = "<<x.zeros<<" ch1 = "<<x.ch1<<" ch2 = "<<x.ch2<<'\n';
- }
- void cinfo(sct x){
- cout<<x.mn<<'/'<<x.mx<<'/'<<x.sum<<'\n';
- }
- vector <int> ar = {0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- vector <sct> tree;
- /*на массиве: корень имеет номер 1
- Дети вершины V и имеют номера (2*V, 2*V + 1)
- Номер родителя в вершины U в такой вариации - (U/2)
- дети отрезка [L, R] занимаются отрезками [L, M], [M + 1; R], где M = (L + R)/2
- */
- void make(int idx, int li, int ri){
- sct sm; //some
- //cout<<"lr = "<<li<<' '<<ri<<'\n';
- sm.l = li;
- sm.r = ri;
- if (li == ri){
- sm.ch1 = -1;
- sm.ch2 = -1;
- sm.sum = ar[li];
- sm.mx = ar[li];
- sm.mn = ar[li];
- if (ar[li] == 0) sm.zeros = 1;
- else sm.zeros = 0;
- tree[idx] = sm;
- //csc(sm);
- //cout<<'\n';
- }
- else{
- int mi = (li + ri) / 2;
- sm.ch1 = idx * 2;
- sm.ch2 = idx * 2 + 1;
- make(idx * 2, li, mi);
- make(idx * 2 + 1, mi+1, ri);
- sm.mn = min(tree[sm.ch1].mn, tree[sm.ch2].mn);
- sm.mx = max(tree[sm.ch1].mx, tree[sm.ch2].mx);
- sm.sum = tree[sm.ch1].sum + tree[sm.ch2].sum;
- sm.zeros = tree[sm.ch1].zeros + tree[sm.ch2].zeros;
- tree[idx] = sm;
- }
- }
- int L = -1, R = -1;
- int intersct( int l, int r){
- if (l >= L && r <= R){
- return 2;
- }
- else if (l <= R && r > R){
- return 1;
- }
- else if (r >= L && l < L){
- return 1;
- }
- else return 0;
- }
- int inf = 2e9;
- int get_min(sct S){
- int inter = intersct(S.l, S.r);
- //csc(S);
- //cout<<"inter = "<<inter<<"\n\n";
- if (inter == 2){
- return S.mn;
- }
- else if (inter == 0){
- return inf;
- }
- else if (inter == 1){
- int a = get_min(tree[S.ch1]);
- int b = get_min(tree[S.ch2]);
- return min(a,b);
- }
- }
- int get_sum(sct S){
- int inter = intersct(S.l, S.r);
- //csc(S);
- //cout<<"inter = "<<inter<<"\n\n";
- if (inter == 2){
- return S.sum;
- }
- else if (inter == 0){
- return 0;
- }
- else if (inter == 1){
- int a = get_sum(tree[S.ch1]);
- int b = get_sum(tree[S.ch2]);
- return a + b;
- }
- }
- int get_max(sct S){
- int inter = intersct(S.l, S.r);
- //csc(S);
- //cout<<"inter = "<<inter<<"\n\n";
- if (inter == 2){
- return S.mx;
- }
- else if (inter == 0){
- return -inf;
- }
- else if (inter == 1){
- int a = get_max(tree[S.ch1]);
- int b = get_max(tree[S.ch2]);
- return max(a,b);
- }
- }
- int get_zeros(sct S){
- int inter = intersct(S.l, S.r);
- //csc(S);
- //cout<<"inter = "<<inter<<"\n\n";
- if (inter == 2){
- return S.zeros;
- }
- else if (inter == 0){
- return 0;
- }
- else if (inter == 1){
- int a = get_zeros(tree[S.ch1]);
- int b = get_zeros(tree[S.ch2]);
- return a + b;
- }
- }
- int main()
- {
- ar.clear();
- int N,K;
- cin>>N;
- ar.resize(N);
- for (int &x: ar) cin>>x;
- cin>>K;
- int lg = log(N) / log(2);
- if (pow(2, lg) < N) lg++;
- int sz = 2 * pow(2, lg);
- tree.resize(sz);
- make(1, 0, N-1);
- for (int i = 0; i < K; ++i){
- cin>>L>>R;
- L--;
- R--;
- //cout<<"L = "<<L<<" R = "<<R<<'\n';
- cout<<get_zeros(tree[1])<<' ';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement