UESTC_秋实大哥与花 2015 UESTC Training for Data Structures<Problem B>
2015-05-01 01:11
573 查看
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
) *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 + 50; 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 = 0; while(idx > 0) { res += f[idx]; idx -= lowbit(idx); } return res; } ll sumg(ll idx) { ll res = 0; while(idx > 0) { res += g[idx]; idx -= lowbit(idx); } return res; } int main(int argc,char *argv[]) { ll m; scanf("%lld%lld",&n,&m); memset(f,0,sizeof(f)); memset(g,0,sizeof(g)); for(int j = 1 ; j <= n ; ++ j) { ll v; scanf("%lld",&v); updataf(j,v*j); updataf(j-1,-v*(j-1)); updatag(j,v); updatag(j-1,-v); } while(m--) { ll i,j,v; scanf("%lld%lld%lld",&i,&j,&v); updataf(j,v*j); updataf(i-1,-v*(i-1)); updatag(j,v); updatag(i-1,-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 0; }
相关文章推荐
- UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>
- UESTC_秋实大哥与小朋友 2015 UESTC Training for Data Structures<Problem A>
- UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>
- UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>
- UESTC_秋实大哥与家 2015 UESTC Training for Data Structures<Problem E>
- UESTC_秋实大哥与妹纸 2015 UESTC Training for Data Structures<Problem F>
- UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<Problem G>
- UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>
- UESTC_秋实大哥下棋 2015 UESTC Training for Data Structures<Problem I>
- UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>
- UESTC_秋实大哥与连锁快餐店 2015 UESTC Training for Graph Theory<Problem A>
- UESTC_秋实大哥带我飞 2015 UESTC Training for Graph Theory<Problem B>
- UESTC_秋实大哥与时空漫游 2015 UESTC Training for Graph Theory<Problem C>
- UESTC_Islands 2015 UESTC Training for Data Structures<Problem J>
- UESTC_全都是秋实大哥 2015 UESTC Training for Search Algorithm & String<Problem J>
- UESTC_秋实大哥掰手指 2015 UESTC Training for Dynamic Programming<Problem B>
- UESTC_Sliding Window 2015 UESTC Training for Data Structures<Problem K>
- UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm & String<Problem K>
- UESTC_Rain in ACStar 2015 UESTC Training for Data Structures<Problem L>
- 2015 UESTC Training for Data Structures 秋实大哥下棋