注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

n+e

NewWeb:http://trinkle.is-programmer.com/

 
 
 

日志

 
 

[BZOJ3212]Pku3468 A Simple Problem with Integers  

2014-12-01 19:44:14|  分类: BZOJ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

3212: Pku3468 A Simple Problem with Integers

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 868  Solved: 383
[Submit][Status]

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.

Solution

参见zkw《统计的力量——线段树全接触》

好像读入有问题还是什么

Code

/**************************************************************
    Problem: 3212
    User: wjy1998
    Language: C++
    Result: Accepted
    Time:24 ms
    Memory:24244 kb
****************************************************************/
 
#include<cstdio>
char ch,c;int aa,bb;int F(){
    while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-')if(ch<=0)return aa;
    ch=='-'?bb=1,ch=getchar():bb=0,aa=ch-'0';
    while(ch=getchar(),ch>='0'&&ch<='9')aa=aa*10+ch-'0';return bb?aa=-aa:1,aa;
}
typedef long long ll;
int n,m,d,l,r,v,i;ll sum[1000010],t0[1000010],ans0,t1[1000010],ans1;
ll query(int t){
    int r=t+1;for(ans1=ans0=0;t;t-=t&-t)ans0+=t0[t],ans1+=t1[t];
    return ans0*r-ans1;
}
void add(int t,int x){ll xx=x*t;for(;t<=n;t+=t&-t)t0[t]+=x,t1[t]+=xx;}
int main(){
    for(n=F(),m=F(),i=1;i<=n;i++)sum[i]=sum[i-1]+F();
    for(;m--;){while(c=getchar(),c!='Q'&&c!='C');
l=F(),r=F(),c=='Q'?printf("%lld\n",query(r)-query(l-1)+sum[r]-sum[l-1]):(add(l,v=F()),add(r+1,-v),1);}
}
  评论这张
 
阅读(39)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018