Title

洛谷P2068统计和

P2068 统计和

思路

单点修改 + 区间查询

线段树/树状数组板子题

代码

#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define lowbit(x) x & -x
const int maxn = 5e5 + 5;
const int inf = 0x7f7f7f7f;

struct custom_hash 
{
	static uint64_t splitmix64(uint64_t x) 
    {
		x ^= x << 13;
		x ^= x >> 7;
		x ^= x << 17;
		return x; 
	}
	size_t operator () (uint64_t x) const 
    {
		static const uint64_t FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count(); // 时间戳
		return splitmix64(x + FIXED_RANDOM);
	}
};

int fenwick[maxn];

int n = 0, w = 0;

void modify(int pos, int x)
{
    while(pos <= n)
    {
        fenwick[pos] += x;
        pos += lowbit(pos);
    }
}

int query(int pos)
{
    int ans = 0;
    while(pos)
    {
        ans += fenwick[pos];
        pos -= lowbit(pos);
    }
    return ans;
}

void solve()
{
    std::cin >> n >> w;
    char op = 0;
    int a = 0, b = 0;
    for (int i = 1; i <= w; i++)
    {
        std::cin >> op >> a >> b;
        if (op == 'x')
        {
            modify(a, b);
        }
        else
        {
            std::cout << query(b) - query(a - 1) << endl;
        }
    }
}

signed main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr); std::cout.tie(nullptr);
    //freopen("out.txt", "w", stdout);
    int t = 1;
    //std::cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}
posted @   栗悟饭与龟功気波  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示