当前位置:网站首页>AcWing 242. A simple integer problem (tree array + difference)
AcWing 242. A simple integer problem (tree array + difference)
2022-07-06 11:17:00 【Jacob* ̄▽ ̄*】


Pre knowledge :
Tree array 、 Prefix sum and difference .
The question :
Ideas :
The instructions of this question are “ The interval increases ”,“ Single point query ”.
These two instructions are Naked difference What can be accomplished , The former O(1), the latter O(n), But according to the title , Element number n And the number of inquiries m The data range of is 1e5, If you use naked difference , complete m Single point query requires O(n^2), Obviously illegal , We should abandon .
The correct approach : Tree array + Difference , Or say Use the difference group to build a tree array .
The basic instruction of tree array is :“ Single point increase ”,“ Interval query ”, When combined with differential array, it can be realized “ The interval increases ”,“ Single point query ” 了 !
( Single point increase of the difference array , Corresponding to the original array interval increase , Before the differential array query i Item prefix and corresponding original array query a[i])
Time complexity :
O(mlogn)
Code :
/* Save the difference group into the tree array , The original array is the prefix of the tree array and , Turn the problem into a tree array ,
Original array No x The number is the tree array 1~x And */
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5+10;
int n, m;
int a[N], c[N];
int lowbit(int x) {return x & (-x);}
int ask(int x){
int ans = 0;
while(x) {ans+=c[x], x-=lowbit(x);}
return ans;
}
void add(int x, int y)
{
for(; x<=n; x+=lowbit(x)) c[x]+=y;
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;++i)
{
scanf("%lld",&a[i]);
add(i, a[i]-a[i-1]);// use add Construct a differential tree array
}
while(m--)
{
char op[2];
int l, r, d, x;
scanf("%s",op);
if(*op=='C') {
scanf("%lld%lld%lld", &l, &r, &d);
add(l, d), add(r+1, -d);// use O(1) The complexity of interval modification
}
else{
scanf("%lld", &x);
printf("%lld\n", ask(x));
}
}
return 0;
}
边栏推荐
- 报错解决 —— io.UnsupportedOperation: can‘t do nonzero end-relative seeks
- @Controller, @service, @repository, @component differences
- [recommended by bloggers] C WinForm regularly sends email (with source code)
- 一键提取pdf中的表格
- MySQL completely uninstalled (windows, MAC, Linux)
- 02 staff information management after the actual project
- Idea import / export settings file
- QT creator specify editor settings
- QT creator custom build process
- Tcp/ip protocol (UDP)
猜你喜欢

Install mongdb tutorial and redis tutorial under Windows

Postman environment variable settings

PyCharm中无法调用numpy,报错ModuleNotFoundError: No module named ‘numpy‘

Solution: log4j:warn please initialize the log4j system properly

A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon

Introduction and use of automatic machine learning framework (flaml, H2O)

Windows下安装MongDB教程、Redis教程

基于apache-jena的知识问答

Some problems in the development of unity3d upgraded 2020 VR

【博主推荐】asp.net WebService 后台数据API JSON(附源码)
随机推荐
QT creator create button
01项目需求分析 (点餐系统)
JDBC principle
[recommended by bloggers] C # generate a good-looking QR code (with source code)
Number game
记一次某公司面试题:合并有序数组
LeetCode #461 汉明距离
Armv8-a programming guide MMU (2)
neo4j安装教程
一键提取pdf中的表格
Install MySQL for Ubuntu 20.04
[BMZCTF-pwn] 12-csaw-ctf-2016-quals hungman
SSM整合笔记通俗易懂版
02 staff information management after the actual project
解决安装Failed building wheel for pillow
Julia 1.6 1.7 common problem solving
Django running error: error loading mysqldb module solution
Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'
Basic use of redis
引入了junit为什么还是用不了@Test注解