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;
}