ACM-ICPC 2018 徐州赛区网络预赛H Ryuji doesn't want to study(树状数组)题解
题意:给你数组a,有两个操作 1 l r,计算l到r的答案:a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] (L is the length of [ l, r ] that equals to r - l + 1),或者 2 i b:把第i个换成b
思路:用一个树状数组存i的前缀和,再用一个树状数组存(n - i + 1)*a[ i ]的前缀和,这样算出后面那个的区间差减去前一个的区间差的某个倍数就会成为答案。
代码:
- #include<queue>
- #include<cstring>
- #include<set>
- #include<map>
- #include<stack>
- #include<cmath>
- #include<vector>
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- typedef long long ll;
- const int maxn = + ;
- const int seed = ;
- const ll MOD = 1e9 + ;
- const int INF = 0x3f3f3f3f;
- using namespace std;
- ll sum[][maxn], a[maxn];
- ll n;
- int lowbit(int x){
- return x&(-x);
- }
- void update(int x, ll v, int id){
- for(int i = x; i <= n; i += lowbit(i)){
- sum[id][i] += v;
- }
- }
- ll query(int x, int id){
- ll cnt = ;
- for(int i = x; i > ; i -= lowbit(i)){
- cnt += sum[id][i];
- }
- return cnt;
- }
- int main(){
- ll q;
- while(~scanf("%lld%lld", &n, &q)){
- memset(sum, , sizeof(sum));
- for(int i = ; i <= n; i++){
- scanf("%lld", &a[i]);
- update(i, a[i], );
- update(i, a[i] * (n - i + ), );
- }
- while(q--){
- ll u, v, w;
- scanf("%lld%lld%lld", &u, &v, &w);
- if(u == ){
- printf("%lld\n", query(w, ) - query(v - , ) - (n - w) * (query(w, ) - query(v - , )));
- }
- else{
- update(v, -a[v] + w, );
- update(v, (-a[v] + w) * (n - v + ), );
- a[v] = w;
- }
- }
- }
- return ;
- }
ACM-ICPC 2018 徐州赛区网络预赛H Ryuji doesn't want to study(树状数组)题解的更多相关文章
- ACM-ICPC 2018 徐州赛区网络预赛 H Ryuji doesn't want to study (树状数组差分)
https://nanti.jisuanke.com/t/31460 题意 两个操作.1:查询区间[l,r]的和,设长度为L=r-l+1, sum=a[l]*L+a[l+1]*(L-1)+...+a[ ...
- ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study
262144K Ryuji is not a good student, and he doesn't want to study. But there are n books he should ...
- ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study (线段树)
Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, ea ...
- ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study(树状数组)
Output For each question, output one line with one integer represent the answer. 样例输入 5 3 1 2 3 4 5 ...
- 计蒜客 1460.Ryuji doesn't want to study-树状数组 or 线段树 (ACM-ICPC 2018 徐州赛区网络预赛 H)
H.Ryuji doesn't want to study 27.34% 1000ms 262144K Ryuji is not a good student, and he doesn't wa ...
- ACM-ICPC 2018 徐州赛区网络预赛 G Trace(逆向,两颗线段树写法)
https://nanti.jisuanke.com/t/31459 思路 凡是后面的轨迹对前面的轨迹有影响的,可以尝试从后往前扫 区间修改需要push_down,单点更新所以不需要push_up(用 ...
- ACM-ICPC 2018 徐州赛区网络预赛 B(dp || 博弈(未完成)
传送门 题面: In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl n ...
- ACM-ICPC 2018 徐州赛区网络预赛 D 杜教筛 前缀和
链接 https://nanti.jisuanke.com/t/31456 参考题解 https://blog.csdn.net/ftx456789/article/details/82590044 ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)
ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...
随机推荐
- CF573C Bear and Drawing 构造+树论
正解:构造 解题报告: 传送门! 这题首先可以画下图找下规律,,,然后通过找规律可以发现,最终的方案一定是一条主干+一些枝条,而且这些枝条的分杈一定小于等于2 明确一下主干的定义,最左边的节点和最右边 ...
- 通过phantomjs 进行页面截图
本文章参考了使用phantomjs操作DOM并对页面进行截图需要注意的几个问题 及phantomjs使用说明 这两篇文章,初次接触phantomjs的童鞋可以去看下这两篇原文 在学习中可以看下 pha ...
- javascript php 数组 json 对比 总结
看到这个博客,终于明白JavaScript 里只有一种 Array,不存在索引数组和关联数组. 1.在js中所有要素都是继承自Object对象的,任何对象都能通过obj["name" ...
- 虫师的性能测试思想html网页学习
http://www.cnblogs.com/fnng/category/387349.html
- SQL 3
SQL SELECT DISTINCT 语句 SELECT DISTINCT 语句用于返回唯一不同的值. SQL SELECT DISTINCT 语句 在表中,一个列可能会包含多个重复值,有时您也许希 ...
- 扩展的friend语法
Java中没有定义friend. friend用于声明类的友元,可以无视类中成员的属性.无论成员3p中的哪一种,友元类或友元函数都可以访问,破坏了封装性,此关键字备受争议.专家会建议程序员使用get/ ...
- go for cryptocurrency
https://blog.conformal.com/category/btcd/ https://github.com/btcsuite/btcd/tree/master/docs https:// ...
- SVN出现xcrun: error: invalid active developer path(Mac)
Mac升级了系统,配置PHPStorm的SVN,出现如下错误: 具体提示的内容是:xcrun: error: invalid active developer path (/Library/Devel ...
- Spark2.x学习笔记:Spark SQL快速入门
Spark SQL快速入门 本地表 (1)准备数据 [root@node1 ~]# mkdir /tmp/data [root@node1 ~]# cat data/ml-1m/users.dat | ...
- Selenium - Xpath 使用方法
由于最新版火狐不在支持FireBug等开发工具,可以通过https://ftp.mozilla.org/pub/firefox/releases/下载49版本以下的火狐就可以增加Firebug等扩展了 ...