ACM-ICPC 2018徐州網絡賽-H題 Ryuji doesn't want to study
阿新 • • 發佈:2018-09-11
als ryu using 網絡 nan spa upd div names
C*M....死於update的一個long long寫成int了
心累 不想寫過程了 ********
樹狀數組,一個平的一個斜著的,怎麽斜都行
題庫鏈接:https://nanti.jisuanke.com/t/31460
#include <iostream> #include <cstring> #define ll long long #define lowbit(x) (x & -x) using namespace std; const int maxn = 1e5 + 20; int n, m; ll t[maxn]; ll t1[maxn]; ll ans[maxn]; ll search(int pos, int r) { ll sum = 0; r = n - r; while (pos) { sum += t[pos]; sum -= r * t1[pos]; pos -= lowbit(pos); } return sum; } void update(int pos, int val) { int x = pos; while (pos <= n) { t[pos] += (n - x + 1) * val; t1[pos] += val; pos += lowbit(pos); } } void update1(int pos, ll val) { val = val - search(pos, pos) + search(pos - 1,pos); int x = pos; while (pos <= n) { t[pos] += (n - x + 1) * val; t1[pos] += val; pos += lowbit(pos); } }void init() { memset(t, 0, sizeof(t)); memset(t1, 0, sizeof(t1)); for (int i = 1; i <= n; i++) update(i, ans[i]); } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> ans[i]; init(); while (m--) { int op; ll a, b; cin >> op >> a >> b; if (op == 1) cout << search(b, b) - search(a - 1, b) << endl; else update1(a, b); } return 0; }
ACM-ICPC 2018徐州網絡賽-H題 Ryuji doesn't want to study