Ryuji doesn't want to study 2018徐州icpc网络赛 树状数组
Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i].
Unfortunately, the longer he learns, the fewer he gets.
That means, if he reads books from ll to rr, he will get a[l] \times L + a[l+1] \times (L-1) + \cdots + a[r-1] \times 2 + a[r]a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r](LL is the length of [ ll, rr ] that equals to r - l + 1r−l+1).
Now Ryuji has qq questions, you should answer him:
11. If the question type is 11, you should answer how much knowledge he will get after he reads books [ ll, rr ].
22. If the question type is 22, Ryuji will change the ith book's knowledge to a new value.
Input
First line contains two integers nn and qq (nn, q \le 100000q≤100000).
The next line contains n integers represent a[i]( a[i] \le 1e9)a[i](a[i]≤1e9) .
Then in next qq line each line contains three integers aa, bb, cc, if a = 1a=1, it means question type is 11, and bb, cc represents [ ll , rr ]. if a = 2a=2 , it means question type is 22 , and bb, cc means Ryuji changes the bth book' knowledge to cc
Output
For each question, output one line with one integer represent the answer.
样例输入复制
5 3
1 2 3 4 5
1 1 3
2 5 0
1 4 5
样例输出复制
10
8
题目来源
题意:有n个数,你可以对这n个数进行m次操作,可以进行的操作有两种,第一种将b位置的数置为c,第二种求a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r]
分析:注意表达式:a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r]可以化为:
化成这个式子后,我们每次只要维护a[i]*(n-i+1)和a[i]就可以了,更新的时候加上c-a[i]就可以将a[i]更新为c
做题目做少了,打网络赛的时候都没有想到这样化简式子
参考博客:https://blog.csdn.net/qq_39826163/article/details/82586489
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e5+10;
const ll mod = 2e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
ll n, m, q, a[maxn], t1[maxn], t2[maxn];
ll lowbit( ll x ) {
return x&(-x);
}
void update1( ll x, ll v ) {
while( x <= n ) {
t1[x] += v;
x += lowbit(x);
}
}
void update2( ll x, ll v ) {
while( x <= n ) {
t2[x] += v;
x += lowbit(x);
}
}
ll query1( ll x ) {
ll sum = 0;
while(x) {
sum += t1[x];
x -= lowbit(x);
}
return sum;
}
ll query2( ll x ) {
ll sum = 0;
while(x) {
sum += t2[x];
x -= lowbit(x);
}
return sum;
}
int main() {
scanf("%lld%lld",&n,&m);
for( ll i = 1; i <= n; i ++ ) {
scanf("%lld",&a[i]);
update1(i,a[i]);
update2(i,a[i]*(n-i+1));
}
while( m -- ) {
ll k, b, c;
scanf("%lld%lld%lld",&k,&b,&c);
if( k == 1 ) {
ll sum1 = query1(c) - query1(b-1);
ll sum2 = query2(c) - query2(b-1);
ll ans = sum2-(n-c)*sum1;
printf("%lld\n",ans);
} else {
update1(b,c-a[b]);
update2(b,(c-a[b])*(n-b+1));
a[b] = c;
}
}
return 0;
}
Ryuji doesn't want to study 2018徐州icpc网络赛 树状数组的更多相关文章
- Trace 2018徐州icpc网络赛 (二分)(树状数组)
Trace There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx ...
- Trace 2018徐州icpc网络赛 思维+二分
There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy) ...
- Features Track 2018徐州icpc网络赛 思维
Morgana is learning computer vision, and he likes cats, too. One day he wants to find the cat moveme ...
- Ryuji doesn't want to study 2018 徐州赛区网络预赛
题意: 1.区间求 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) ...
- ICPC 2018 徐州赛区网络赛
ACM-ICPC 2018 徐州赛区网络赛 去年博客记录过这场比赛经历:该死的水题 一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进. D. Easy Math 题意: ...
- ACM-ICPC 2018 徐州赛区(网络赛)
目录 A. Hard to prepare B.BE, GE or NE F.Features Track G.Trace H.Ryuji doesn't want to study I.Charac ...
- Supreme Number 2018沈阳icpc网络赛 找规律
A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying ...
- 2019 徐州icpc网络赛 E. XKC's basketball team
题库链接: https://nanti.jisuanke.com/t/41387 题目大意 给定n个数,与一个数m,求ai右边最后一个至少比ai大m的数与这个数之间有多少个数 思路 对于每一个数,利用 ...
- 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 ...
随机推荐
- 通过ping命令了解三层转发流程
ping命令:因特网包探索器.本文主要通过路由器两端不同网段PC互ping来讲解三层转发流程. 例子:PC-A是如何 ping 通 PC-C 的,有几种情况? 说明:1.在条件1阶段PC-C不会刷新a ...
- 【JDK】JDK源码分析-LinkedHashMap
概述 前文「JDK源码分析-HashMap(1)」分析了 HashMap 主要方法的实现原理(其他问题以后分析),本文分析下 LinkedHashMap. 先看一下 LinkedHashMap 的类继 ...
- 【iOS】XIB 调整视图大小
使用 XIB 创建视图的时候,拖拽 UIView 到画布时,大小是不可调整的,如何自由调整大小呢? 选中 UIView 并打开属性面板,将 Simulated Metrics 中的 Size 设为 F ...
- 语音控制单片机工作【百度语音识别,串口发送数据到单片机】【pyqt源码+软件】!!
前些天闲着没事,就做了个语音识别结合串口发送指令的软件,用的是pyqt写的,软件打开后对着笔记本的话筒说话, 他就能识别返回文字结果,然后匹配语音中的关键词,如果有关键词就发送关键词对应的命令,比如语 ...
- 自己动手,开发轻量级,高性能http服务器。
前言 http协议是互联网上使用最广泛的通讯协议了.web通讯也是基于http协议:对应c#开发者来说,asp.net core是最新的开发web应用平台.由于最近要开发一套人脸识别系统,对通讯效率的 ...
- Spring JdbcTemplate之使用详解
最近在项目中使用到了 Spring 的 JdbcTemplate, 中间遇到了好多坑, 所以花一些时间对 JdbcTemplate 的使用做了一个总结, 方便以后自己的查看.文章中贴出来的API都是经 ...
- 手机APP测试之Fiddler
之前测试基本上是web端,突然接手了一个要在指定pad上测试APP的任务,于是决定研究研究pad抓包.最开始考虑有jmeter进行抓包测试,发现抓不到(可能方法有问题,后续还需继续研究),然后用fid ...
- HelloDjango 系列教程:博客从“裸奔”到“有皮肤”
文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 在此之前我们已经编写了博客的首页视图,并且配置了 URL 和模板,让 django 能够正确地处理 HTTP 请求并返回合适的 ...
- 【游记】NOIP2019复赛
声明 我的游记是一个完整的体系,如果没有阅读过往届文章,阅读可能会受到障碍. ~~~上一篇游记的传送门~~~ 前言 (编辑中)
- Go基础语法学习
Go语言基础 Go是一门类似C的编译型语言,但是它的编译速度非常快.这门语言的关键字总共也就二十五个,比英文字母还少一个,这对于我们的学习来说就简单了很多.先让我们看一眼这些关键字都长什么样: 下面列 ...