该浏览器不支持canvas

ACM-ICPC 2018徐州网络赛-H题 Ryuji doesn't want to study

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

 

posted @ 2018-09-10 22:35  真是啰嗦  阅读(203)  评论(0)    收藏  举报