Contest2994 - 2021秋组队训练赛第二十一场

文章目录

H题【树状数组】

Ryuji doesn’t want to study
蚌埠住了,add函数里面要ll val;二维树状数组的板子题,有公式推导的部分

#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
const int N = 1e6 + 1000;
const int mod = 11092019;
const int inf = 0x7f7f7f7f;
const int eps = 1e-8;
ll read()
{
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while ('0' <= c && c <= '9')
        x = x * 10 + c - '0', c = getchar();
    return x * f;
}
ll n, q, res;
ll a[N], c[3][N];
ll lowbit(ll x) { return x & -x; }
void add(int x, ll val, int id)//就nm离谱
{
    for (; x <= n; x += lowbit(x))
        c[id][x] += val;
}
ll ask(int x, int id)
{
    ll ans = 0;
    for (; x; x -= lowbit(x))
        ans += c[id][x];
    return ans;
}
int main()
{
    // IOS;
    cin >> n >> q;
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
        add(i, a[i], 0);
        add(i, i * a[i], 1);
    }
    res = 0;
    while (q--)
    {
        ll x, l, r;
        scanf("%lld%lld%lld", &x, &l, &r);
        if (x == 1)
        {
            if (l > r)
            {
                puts("0");
                continue;
            }
            res = (r + 1) * (ask(r, 0) - ask(l - 1, 0));
            res -= ask(r, 1) - ask(l - 1, 1);
            printf("%lld\n", res);
        }
        else
        {
            // add(l, r - (ask(l, 0) - ask(l - 1, 0)), 0);//这种实际上就是每次修改树状数组里面的
            // add(l, l * r - (ask(l, 1) - ask(l - 1, 1)), 1);
            add(l, r - a[l], 0);
            add(l, l * (r - a[l]), 1);
            a[l] = r;//注意每次要更新a[]数组
        }
    }
    return 0;
}
/**
【Code is far away from bug with the animal protecting!!!】
休息一下~休息一下~
 ̄ ̄ ̄ ̄ ̄\/ ̄ ̄ ̄ ̄
    ∧_∧    
  ;;(??ω?)  
 _旦_(っ(,,■)__
 |l ̄l|| ̄し?し? ̄|i
**/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WTcrazy _

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值