Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Segment tree

void build(int node, int start, int end)
{
    if(start == end)
    {
        // Leaf node will have a single element
        tree[node] = A[start];
    }
    else
    {
        int mid = (start + end) / 2;
        // Recurse on the left child
        build(2*node, start, mid);
        // Recurse on the right child
        build(2*node+1, mid+1, end);
        // Internal node will have the sum of both of its children
        tree[node] = tree[2*node] + tree[2*node+1];
    }
}
Comment

how to code a segment tree in c++

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
bool isPower(long long n)
{
    long long f = 1;
 
    while (f <= n) {
        if (f == n) {
            return false;
        }
        else {
            f *= 2;
        }
    }
 
    return true;
 
 
 
 
}
int main()
{
    long long n;
    long long t;
 
    cin >> n >> t;
    vector<unsigned long long>v(n * 2);
    long long k;
    for (long long i = n; i < n*2; i++) {
        cin >> k;
        v[i] = k;
    }
    for (long long i = n - 1; i > 0; i--) {
        v[i] = v[2 * i] + v[2 * i + 1];
 
    }
 
    unsigned long long i;
    unsigned long long j;
    unsigned long long x = 1;
    unsigned long long sum = 0;
    unsigned long long sum1 = 0;
    long long zz;
    for (long long l = 0; l < t; l++) {
        sum = 0;
        x = 1;
        sum1 = 0;
        cin >> i;
        cin >> j;
 
        i += n - 1;
        j += n - 1;
        zz = i;
        if (i == j) {
            cout << v[j] << endl;
        }
        else {
            while (isPower(i)) {
 
                if ((i) % 2 == 0) {
                    sum += v[i];
 
                    i = i * x - 1;
                    x = 1;
                }
                else {
                    x = x * 2;
                    i = i / 2;
                }
 
 
            }
            sum += v[i];
            x = 1;
            while (isPower(j)) {
 
                if (j % 2 == 0) {
                    sum1 += v[j];
 
                    j = j * x - 1;
                    x = 1;
                }
 
 
                else {
                    x = x * 2;
                    j = j / 2;
                }
 
 
            }
            sum1 += v[j];
 
            cout << (sum1 - sum + v[zz])% 9223372036854775807 << endl;
        }
 
    }
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: palindrome no example 
Cpp :: Basic stack implementation in c++ 
Cpp :: if statement in c++ 
Cpp :: function template in c++ 
Cpp :: iteration in c++ 
Cpp :: inpout in Array c++ 
Cpp :: string to wstring conversion c++ 
Cpp :: sum function in c++ 
Cpp :: aliasing c++ 
Cpp :: c++ handling 
Cpp :: how to get part from the vector cpp 
Cpp :: do while loop c++ 
C :: c colourful text 
C :: wireshark tls client hello filter 
C :: manifest orientation portrait 
C :: adb switch to usb 
C :: C hello workld 
C :: octave sum all elements in matrix 
C :: is 33 prime number 
C :: for loop c 
C :: que es % en c 
C :: c int to string 
C :: Futter Square Button 
C :: list c 
C :: c if else 
C :: append to list in c 
C :: fopen in c example 
C :: C program to check whether character is lowercase or not using ASCII values 
C :: malloc contiguous 2d array 
C :: clear screen in c 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =