B - 秋实大哥与花

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status

秋实大哥是一个儒雅之人,昼听笙歌夜醉眠,若非月下即花前。

所以秋实大哥精心照料了很多花朵。现在所有的花朵排成了一行,每朵花有一个愉悦值。

秋实大哥每天要对着某一段连续的花朵歌唱,然后这些花朵的愉悦值都会增加一个相同的值v(v可能为负)。

同时他想知道每次他唱完歌后这一段连续的花朵的愉悦值总和是多少。

Input

第一行有一个整数n,表示花朵的总数目。

第二行包含n个整数ai,表示第i朵花初始的愉悦值。

第三行包含一个整数m,表示秋实大哥唱了m天的歌。

接下来m行,每行包含三个整数l r v,表示秋实大哥对着[l,r]这个区间内的花朵歌唱,每朵花的愉悦值增加了v。

1≤n,m,ai,|v|≤100000,1≤l≤r≤n。

Output

输出共m行,第i行表示秋实大哥完成第i天的歌唱后,那一段花朵的愉悦值总和。

Sample input and output

Sample Input Sample Output
3
0 0 0
3
1 2 1
1 2 -1
1 3 1
2
0
3

解题报告:

没啥好说的,线段树模板基础题。。唯一需要注意的就是long long了.

当然我是使用的树状数组:

题目中操作属于区间更新 - 区间查询<查询点也是区间嘛>类型,可使用树状数组来实现.

令C[i]为从1至i号花的共同更新之和.

首先考虑更新,add[l,r,v] → c[r] += v , c[l-1] -=v;

Sum[x] = 从 1 至 x 号花的value之和.

Sum[x] = c[1]*1 + c[2]*2 + c[3] * 3 ..... + c[x] * x + (c[x+1] + .... C[n] ) *x;

= (segema)f(x) + (segema)g(x) * x;

= 两个树状数组,一个维护 c[i]*i ,一个维护c[i]

之后考虑区间查询:

Query(l , r)

= sum[r] - sum[l-1]

 #include <iostream>
#include <cstring>
typedef long long ll;
using namespace std;
const int maxn = 1e5 + ;
ll n;
ll f[maxn],g[maxn]; inline ll lowbit(ll idx)
{
return idx & (-idx);
} void updataf(ll idx,ll res)
{
if (!idx)
return;
while(idx <= n)
{
f[idx] += res;
idx += lowbit(idx);
}
} void updatag(ll idx,ll res)
{
if (!idx)
return;
while(idx <= n)
{
g[idx] += res;
idx += lowbit(idx);
}
} ll sumf(ll idx)
{
ll res = ;
while(idx > )
{
res += f[idx];
idx -= lowbit(idx);
}
return res;
} ll sumg(ll idx)
{
ll res = ;
while(idx > )
{
res += g[idx];
idx -= lowbit(idx);
}
return res;
} int main(int argc,char *argv[])
{
ll m;
scanf("%lld%lld",&n,&m);
memset(f,,sizeof(f));
memset(g,,sizeof(g));
for(int j = ; j <= n ; ++ j)
{
ll v;
scanf("%lld",&v);
updataf(j,v*j);
updataf(j-,-v*(j-));
updatag(j,v);
updatag(j-,-v);
}
while(m--)
{
ll i,j,v;
scanf("%lld%lld%lld",&i,&j,&v);
updataf(j,v*j);
updataf(i-,-v*(i-));
updatag(j,v);
updatag(i-,-v);
i--;
ll res1 = sumf(i) + i*(sumg(n)-sumg(i));
ll res2 = sumf(j) + j*(sumg(n)-sumg(j));
printf("%lld\n",res2 - res1);
}
return ;
}

UESTC_秋实大哥与花 2015 UESTC Training for Data Structures<Problem B>的更多相关文章

  1. UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>

    N - 秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  2. UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>

    H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  3. UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<Problem G>

    G - 秋实大哥去打工 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  4. UESTC_秋实大哥与家 2015 UESTC Training for Data Structures<Problem E>

    E - 秋实大哥与家 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  5. UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>

    D - 秋实大哥与战争 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  6. UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>

    C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  7. UESTC_秋实大哥与小朋友 2015 UESTC Training for Data Structures<Problem A>

    A - 秋实大哥与小朋友 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  8. UESTC_秋实大哥掰手指 2015 UESTC Training for Dynamic Programming<Problem B>

    B - 秋实大哥掰手指 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 2048/1024KB (Java/Others) Submit ...

  9. UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>

    M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

随机推荐

  1. SoftLayerDebug

  2. 【转载】C代码优化方案

    C代码优化方案 1.选择合适的算法和数据结构2.使用尽量小的数据类型3.减少运算的强度 (1)查表(游戏程序员必修课) (2)求余运算 (3)平方运算 (4)用移位实现乘除法运算 (5)避免不必要的整 ...

  3. WPF ICommand 用法

    基础类,继承与ICommand接口 using System; using System.Collections.Generic; using System.Linq; using System.Te ...

  4. Block内的强引用

    众所周知,当某个对象持有着一个Block的时候,如果在Block内部使用强引用反过来持有这个对象,就会导致引用循环.为了避免引用循环,可以使用__weak修饰符,苹果的官方文档在用代码演示__weak ...

  5. [HDU 1317]XYZZY[SPFA变形][最长路]

    题意: 一个图, 点权代表走到该点可获得的能量值. 可正可负. 一个人从1 号出发,带有100点能量. 问是否有一种方案可使人在能量值>0的时候走到n. 思路: 这个题首先要注意点权. 其实就是 ...

  6. LeetCode227:Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  7. linux逻辑卷管理

    近期在进行linux充电,依据网络资料自己整理的资料,分享一下 ---------------------------------------------------------- Linux逻辑卷管 ...

  8. Python模块学习笔记— —time与datatime

    Python提供了多个内置模块用于操作日期时间.像calendar,time,datetime.首先对time模块中最经常使用的几个函数作一个介绍,它提供的接口与C标准库time.h基本一致.然后再介 ...

  9. UIButton-初识IOS

    今天,我学到了所有app经常用到的UIButton控件,废话不多说,这些都是我学习的时候总结的一些,希望可以帮到以后的初学者,IOS初学不应该直接拖拽,感觉不易于理解,所以我总结的基本上全是纯代码编辑 ...

  10. 一个简单方法:构造xml的document,并将其转换为string

    首先,构造一个document对象: Document doc = null; try { doc = DocumentBuilderFactory.newInstance() .newDocumen ...