1316. The number of columns Procedure B
★★ Input file: shulieb.in
Output file: shulieb.out
a simple comparison of
the time limit: 1 s memory limit: 128 MB
【Problem Description】
Suppose there is a size of n- ( n- ≤ 100000 ) integer sequence A , supports the following two operations:
1. A I , A I + . 1 , ... , A J values were increased d
2. Query A I value
The operational requirements for proper operation, and outputs the result.
[Input Format]
A first line of the input file integer n- ,
The second line n integer representing the number of columns A initial value items.
A third row integer m , operands. Then the m rows, each row describes an operation, the following two situations:
ADD ijd (the A I , A I + . 1 , ... , A J ( . 1 ≤ I , J ≤ n- ) values were increased an integer D )
QUERY s (representing a query A S value)
[Output format]
For each inquiry, the results of the query output.
[Sample input]
4 1 4 2 3 3 QUERY 1 ADD 2 2 50 QUERY 2
[Sample output]
. 1 54 is
the practice of a: Difference Array storage arrays in a tree
code is as follows
#include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #define maxn 100005 using namespace std; int n,m; int a[maxn],sum[maxn]; int lowbit(int x){ return x&(-x); } #define ll long long void Add(int x,int d){//存的是差分数组 while(x<=n){sum[x]+=d;x+=lowbit(x); } } long long Sum(int x){ long long ret=0; while(x>0){ ret+=sum[x];x-=lowbit(x);} return ret; } int main() { freopen("shulieb.in","r",stdin);freopen("shulieb.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); scanf("%d",&m); for(int i=1;i<=m;i++){ string s;cin>>s; if(s[0]=='Q'){ int x;scanf("%d",&x); printf("%lld\n",Sum(x)+a[x]); } else{ int l,r,x;scanf("%d%d%d",&l,&r,&x); Add(l,x);Add(r+1,-x); } } return 0; }