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 ...
随机推荐
- WIN10安装VC6.0无法使用的解决办法
WIN10安装VC6.0无法使用的解决办法 VC6.0确实已经太老了 VC6.0实在是很久以前的开发工具了,现在的win10已经对该软件不兼容,但是为了能使抱着怀旧情节的初学者们能像教科书或老前辈们一 ...
- 自定义SWT控件四之其它下拉框
4.其它下拉框 4.1 添加联动二级多选择框(有添加按钮和删除按钮) package com.view.control.select; import java.util.ArrayList; impo ...
- Spring 源码学习(一)-容器的基础结构
关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料 展示的代码摘取了一些核心方法,去掉一些默认设置和日志输出,还有大多数错误异常也去掉了,小伙伴想看详细代码 ...
- abp(net core)+easyui+efcore实现仓储管理系统目录
abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...
- 微信JSSDK签名
微信JS-SDK说明文档 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115 生成签名 1.签名规则 参与签名的 ...
- restapi(4)- rest-mongo : MongoDB数据库前端的httpserver
完成了一套标准的rest风格数据库CRUD操作httpserver后发现有许多不足.主要是为了追求“通用”两个字,想把所有服务接口做的更“范generic”些,结果反而限制了目标数据库的特点,最终产生 ...
- JAVA基础知识(九)Java 异常
Throwable是Error和Exception的基类 Exception(异常) :是程序本身可以处理的异常. Error(错误): 是程序无法处理的错误.这些错误表示故障发生于虚拟机自身.或者发 ...
- react-navigation报错
用react-navigation配置路由时,出现如下报错或白屏. 我的代码原来是 import {AppRegistry} from 'react-native'; import App from ...
- stm8s和stm8l低功耗对比
在低功耗应用中,一般来说mcu是常态halt模式,然后偶尔被唤醒(外部中断或者内部定时唤醒)进入运行模式.所以对比低功耗性能,一般来说只需要对比run模式和halt下的功耗即可,因为项目选用的是通过内 ...
- 《机器学习技法》---对偶SVM
1.对偶问题的推导 为什么要求解对偶问题?一是对偶问题往往更容易求解,二是可以自然的引入核函数. 1.1 用拉格朗日函数将原问题转化为“无约束”等价问题 原问题是: 写出它的拉格朗日函数: 然后我们的 ...