NC15175. Tree Recovery
描述
输入描述
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.
输出描述
You need to answer all Q commands in order. One answer in a line.
示例1
输入:
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
输出:
4 55 9 15
C++11(clang++ 3.9) 解法, 执行用时: 48ms, 内存消耗: 1168K, 提交时间: 2020-02-29 16:57:16
#include<iostream> using namespace std; int main() { int a[100001]; int n,t,i; cin>>n>>t; for(i=1;i<=n;i++) cin>>a[i]; while(t--) { char c; int p,q,r,sum=0; cin>>c; if(c=='C') { cin>>p>>q>>r; for(i=p;i<=q;i++) a[i]+=r; } else { cin>>p>>q; for(i=p;i<=q;i++) sum+=a[i]; cout<<sum<<endl; } } return 0; }
pypy3 解法, 执行用时: 723ms, 内存消耗: 34628K, 提交时间: 2022-03-29 12:50:03
n,q = map(int,input().split()) a=[0]+list(map(int,input().split())) for _ in range(q): s = input().split() if s[0]=='Q': print(sum(a[_] for _ in range(int(s[1]),int(s[2])+1))) else: for i in range(int(s[1]),int(s[2])+1): a[i]+=int(s[3])