码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-09-11 01:02:04      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:i++   str   .com   cstring   const   max   tps   https   ini   

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

标签:i++   str   .com   cstring   const   max   tps   https   ini   

原文地址:https://www.cnblogs.com/qq965921539/p/9623617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!