当前位置:网站首页>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
- [BMZCTF-pwn] 12-csaw-ctf-2016-quals hungman
- Knowledge Q & A based on Apache Jena
- Tcp/ip protocol (UDP)
- Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
- CSDN blog summary (I) -- a simple first edition implementation
- Request object and response object analysis
- CSDN question and answer module Title Recommendation task (II) -- effect optimization
- MySQL的一些随笔记录
- 项目实战-后台员工信息管理(增删改查登录与退出)
猜你喜欢

Generate PDM file from Navicat export table

Invalid global search in idea/pychar, etc. (win10)

A trip to Macao - > see the world from a non line city to Macao

Why can't I use the @test annotation after introducing JUnit
![[recommended by bloggers] asp Net WebService background data API JSON (with source code)](/img/04/c721e6177b578b30cbbf334cb1b6c9.png)
[recommended by bloggers] asp Net WebService background data API JSON (with source code)

解决:log4j:WARN Please initialize the log4j system properly.

CSDN question and answer module Title Recommendation task (I) -- Construction of basic framework

02-项目实战之后台员工信息管理

【博主推荐】C# Winform定时发送邮箱(附源码)

MySQL master-slave replication, read-write separation
随机推荐
Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes
Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
Summary of numpy installation problems
02-项目实战之后台员工信息管理
Use dapr to shorten software development cycle and improve production efficiency
Development of C language standard
Antlr4 uses keywords as identifiers
Principes JDBC
Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
[recommended by bloggers] C # generate a good-looking QR code (with source code)
报错解决 —— io.UnsupportedOperation: can‘t do nonzero end-relative seeks
Ubuntu 20.04 安装 MySQL
MySQL完全卸载(Windows、Mac、Linux)
Some notes of MySQL
Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
Solution: log4j:warn please initialize the log4j system properly
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
Software testing and quality learning notes 3 -- white box testing