zoukankan      html  css  js  c++  java
  • [洛谷] P2068 统计和

    裸线段树

    //#pragma GCC optimize(2)
    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <cctype>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    #include <ctime>
    #include <vector>
    #include <fstream>
    #include <list>
    #include <iomanip>
    #include <numeric>
    using namespace std;
    typedef long long ll;
     
    const int MAXN = 1e6 + 10;
    
    int tree[MAXN] = {0};
    
    void oprt(int now, int fst, int lst, int point, int val)
    {
        if(point < fst || point > lst)
            return ;
        
        if(fst == lst)
        {
            tree[now] += val;
        }
    
        else
        {
            int mid =  fst + (lst - fst) / 2;
    
            oprt(now * 2, fst, mid, point, val);
    
            oprt(now * 2 + 1, mid + 1, lst, point, val);
    		
    		tree[now] = tree[now * 2] + tree[now * 2 + 1];
        }  
    }
    
    ll searh(int now, int fst, int lst, int x, int y)
    {
        if(y < fst || x > lst)
            return 0;
    
        if(fst >= x && lst <= y)
            return tree[now];
        
        ll sum = 0;
    
        int mid = fst + (lst - fst) / 2;
    
        sum += searh(now * 2, fst, mid, x, y);
    
        sum += searh(now * 2 + 1, mid + 1, lst, x, y);
    
        return sum;
    }
    
    
    int main()
    {
        //ios::sync_with_stdio(false);
     
        //cin.tie(0);     cout.tie(0);
     
        int n;
    
        cin>>n;
    
        int t;
    
        cin>>t;
    
        while(t--)
        {
            char mode;
            
            int a, b;
    
            cin>>mode>>a>>b;
    
            if(mode == 'x')
            {
                oprt(1, 1, n, a, b);
            }
            else
            {
                cout<<searh(1, 1, n, a, b)<<endl;
            }
        }
     
        return 0;
    }
  • 相关阅读:
    FTP Protocol
    File Operations
    Ubuntu Install Chinese Input Method
    Vim Intro
    ISA Introduction
    Fourier Transform
    Process/Thread Synchronization
    Process Synchronization-Example 2
    leetcode 栈和队列类型题
    leetcode 字符串类型题
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270456.html
Copyright © 2011-2022 走看看