洛谷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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现