当前位置:网站首页>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;
}
边栏推荐
- 连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
- [recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
- 【博主推荐】SSM框架的后台管理系统(附源码)
- 虚拟机Ping通主机,主机Ping不通虚拟机
- Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
- La table d'exportation Navicat génère un fichier PDM
- 图像识别问题 — pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path
- JDBC principle
- [BMZCTF-pwn] 12-csaw-ctf-2016-quals hungman
- In the era of DFI dividends, can TGP become a new benchmark for future DFI?
猜你喜欢
MySQL master-slave replication, read-write separation
解决安装Failed building wheel for pillow
Idea import / export settings file
Basic use of redis
[recommended by bloggers] C WinForm regularly sends email (with source code)
Solution: log4j:warn please initialize the log4j system properly
Use dapr to shorten software development cycle and improve production efficiency
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application
Did you forget to register or load this tag 报错解决方法
[recommended by bloggers] C # generate a good-looking QR code (with source code)
随机推荐
Knowledge Q & A based on Apache Jena
Introduction to the easy copy module
frp内网穿透那些事
SSM整合笔记通俗易懂版
连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
Install mongdb tutorial and redis tutorial under Windows
Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
[recommended by bloggers] asp Net WebService background data API JSON (with source code)
Project practice - background employee information management (add, delete, modify, check, login and exit)
Django running error: error loading mysqldb module solution
csdn-Markdown编辑器
一键提取pdf中的表格
虚拟机Ping通主机,主机Ping不通虚拟机
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application
Idea import / export settings file
MySQL other hosts cannot connect to the local database
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks
Some problems in the development of unity3d upgraded 2020 VR
npm一个错误 npm ERR code ENOENT npm ERR syscall open