poj 3468 (区间修改 区间查询)
2024-08-26 01:45:17
A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions:147133 | Accepted: 45718 | |
Case Time Limit: 2000MS |
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
The sums may exceed the range of 32-bit integers.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring> using namespace std;
int n,m;
const int N=1e5+;
long long s1[N],s2[N];
int lowbit(int x)
{
return x&(-x);
} void updata(int p,long long x)
{
for(int i=p;i<=n;i+=lowbit(i)){
s1[i]+=x;
s2[i]+=x*p;
}
}
long long sum(int p)
{
long long ans=;
for(int i=p;i>;i-=lowbit(i)){
ans+=s1[i]*(p+)-s2[i];
}
return ans;
}
int main()
{
while(scanf("%d %d",&n,&m)==){
memset(s1,,sizeof(s1));
memset(s2,,sizeof(s2));
for(int i=;i<=n;i++){
long long x;
scanf("%lld",&x);
updata(i,x);
updata(i+,-x);
}
while(m--){
char s[];
scanf("%s",s);
if(s[]=='C'){
int a,b;
long long c;
scanf("%d %d %lld",&a,&b,&c);
updata(a,c);
updata(b+,-c);
}
else{
int a,b;
scanf("%d %d",&a,&b);
printf("%lld\n",sum(b)-sum(a-));
}
}
}
return ;
}
最新文章
- sql server 查询表结构
- poj1062昂贵的聘礼(Dijkstra**)
- JavaScript-闭包注意事项
- Android-ImageView.ScaleType
- Python超简单的HTTP服务器
- OpenGL入门学习(转)
- DreamWeaver文件保存时,提示";发生共享违例";问题的解决方法
- C#枚举器接口IEnumerator的实现
- 动态可视化库Vis.js:社交关系谱
- (iOS)Storyboard/xib小技巧
- MicroStrategy笔试
- Linux学习 -- 用户和用户组管理
- P3197 [HNOI2008]越狱
- DFS算法(——模板习题与总结)
- CSS-技巧
- web环境中微信JS-SDK配置
- java实现文件的断点续传的下载
- <;转>;python字典排序 关于sort()、reversed()、sorted()
- spark 与 Hadoop 融合后 Neither spark.yarn.jars nor spark.yarn.archive is set
- CodeM资格赛5