JZ junior OJ 1567. [GDKOI] simple question

Title Description

    Here you are N integers A1, A2, ..., AN, you can perform two operations: the one kind of each of a given number interval is increased by a value; the other is calculated for a given range of numbers of with.
 

Entry

    The first line contains two integers N and Q.1 <= N, Q <= 100000

The second line contains N integers, is A1, A 2 , ...., The initial value of AN. -1000000000 <= Ai <= 1000000000

Next Q lines, described operation.

"C abc" represents the Aa, Aa + 1, ... .Ab have increased c, -10000 <= c <= 10000.

"Q ab" represents ask Aa, Aa + 1, .... , Ab and.

Export

For each interrogation and the corresponding output, one per 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
 

Data range limit

Limitations [

30% data N, Q <= 10000
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 long long n,q,d,s,c1[100001],sum1[100001],c2[100001],a[100001];
 4 char w;
 5 long long lowbit(long long x)
 6 {
 7     return x&(-x);
 8 }
 9 long long sum(long long c[],long long x)
10 {
11     long long ret=0;
12     while(x>0)
13     {
14         ret+=c[x];
15         x-=lowbit(x);
16     }
17     return ret;
18 }
19 void add(long long c[],long long x,long long y)
20 {
21     while(x<=n)
22     {
23         c[x]+=y;
24         x+=lowbit(x);
25     }
26 }
27 int main()
28 {
29     freopen("simple.in","r",stdin);
30     freopen("simple.out","w",stdout);
31     cin>>n>>q;
32     for(int i=1;i<=n;i++)
33     {
34         cin>>a[i];
35         sum1[i]=sum1[i-1]+a[i];
36     }
37     for(int j=1;j<=q;j++)
38     {
39         cin>>w;
40         cin>>s>>d;
41         if(int(w)==int('Q'))
42         {
43             long long suml=sum1[s-1]+s*sum(c1,s-1)-sum(c2,s-1);
44             long long sumd=sum1[d]+(d+1)*sum(c1,d)-sum(c2,d);
45             cout<<sumd-suml<<endl; 
46         }
47         else 
48         {
49             long long cost;
50             cin>>cost;
51             add(c1,s,cost);
52             add(c1,d+1,-cost);
53             add(c2,s,s*cost);
54             add(c2,d+1,-(d+1)*cost);
55         }
56     }
57     return 0;
58 }

Guess you like

Origin www.cnblogs.com/anbujingying/p/11305911.html