• 线段树


    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll  long long
    #define PII  pair<int, int>
    using namespace std;
    int dir[5][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } ,{ 0,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 1e9 + 7;
    const int N = 2e5+5;
    //if(x<0 || x>=r || y<0 || y>=c)
    //1000000000000000000
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    
    int A[100000] = { 0,1,2,3,4,5,6 };
    
    int tree[N];
    
    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];
        }
    }
    
    void update(int node, int start, int end, int idx, int val)
    {
        if (start == end)
        {
            // Leaf node
            A[idx] += val;
            tree[node] += val;
        }
        else
        {
            int mid = (start + end) / 2;
            if (start <= idx && idx <= mid)
            {
                // If idx is in the left child, recurse on the left child
                update(2 * node, start, mid, idx, val);
            }
            else
            {
                // if idx is in the right child, recurse on the right child
                update(2 * node + 1, mid + 1, end, idx, val);
            }
            // Internal node will have the sum of both of its children
            tree[node] = tree[2 * node] + tree[2 * node + 1];
        }
    }
    int query(int node, int start, int end, int l, int r)
    {
        if (r < start || end < l)
        {
            // range represented by a node is completely outside the given range
            return 0;
        }
        if (l <= start && end <= r)
        {
            // range represented by a node is completely inside the given range
            return tree[node];
        }
        // range represented by a node is partially inside and partially outside the given range
        int mid = (start + end) / 2;
        int p1 = query(2 * node, start, mid, l, r);
        int p2 = query(2 * node + 1, mid + 1, end, l, r);
        return (p1 + p2);
    }
    int main()
    {
        int size = 6;
        build(1, 1, size);
        int res=query(1, 1, size, 2, 3);
        cout << res<<endl;
        return 0;
    }
  • 相关阅读:
    【题解】Codeforces Round #600(Div.2)
    【题解】CF 1073 G. Another LCP
    【题解】CF 1129C. Morse Code
    【题解】CF 1200E. Compress Words
    Cpp Chapter 8: Adventures in Functions Part4
    Cpp Chapter 8: Adventures in Functions Part3
    Python Chapter 9: 使用Tkinter进行GUI程序设计 Exercise
    Python Chapter 9: 使用Tkinter进行GUI程序设计 Part 4
    Python Chapter 9: 使用Tkinter进行GUI程序设计 Part 3
    Python Chapter 9: 使用Tkinter进行GUI程序设计 Part 2
  • 原文地址:https://www.cnblogs.com/dealer/p/12410431.html
Copyright © 2020-2023  润新知