[洛谷] 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;
}

猜你喜欢

转载自blog.csdn.net/Zeolim/article/details/82428978