POJ a simple problem of Integers
来源:互联网 发布:npt内螺纹软件 编辑:程序博客网 时间:2024/05/22 12:30
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<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cmath>
#include<stdio.h>
using namespace std;
typedef long long ll;
ll n,q;
ll a[100005],c1[100005],c2[100005];
ll x,y,z,sum1,sum2;
ll lowbit(ll i)
{
return i&(-i);
}
void add(ll *c,ll i,ll data)
{
while(i<=n)
{
c[i]+=data;
i+=lowbit(i);
}
}
long long getsum(ll *c,ll i)
{
long long ans=0;
while(i>0)
{
ans+=c[i];
i-=lowbit(i);
}
return ans;
}
int main()
{
int i,j;
scanf("%lld%lld",&n,&q);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
add(c1,i,a[i]-a[i-1]);
add(c2,i,(i-1)*(a[i]-a[i-1]));
}
while(q--)
{
char ch;
cin>>ch;
if(ch=='Q')
{
scanf("%lld%lld",&x,&y);
sum1=(x-1)*getsum(c1,x-1)-getsum(c2,x-1);
sum2=y*getsum(c1,y)-getsum(c2,y);
printf("%lld\n",sum2-sum1);
}
if(ch=='C')
{
scanf("%lld%lld%lld",&x,&y,&z);
add(c1,x,z);
add(c1,y+1,-z);
add(c2,x,z*(x-1));
add(c2,y+1,-z*y);
}
}
return 0;
}
- POJ a simple problem of Integers
- POJ A Simple Problem with Integers
- POJ 3468 A Simple Problem With Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ-3468-A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 - A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- WebSocket介绍以及netty对于WebSocket的支持
- 培养优秀思维
- Hadoop基础教程-第12章 Hive:进阶(12.1 内置函数)(草稿)
- MySQL 死锁与日志二三事
- 程序员面试金典(2):原串翻转(python)
- POJ a simple problem of Integers
- Spring Data Jpa+SpringMVC+Jquery.pagination.js实现分页
- poj 1149 PIGS 最大流模型
- 插入排序之希尔排序
- 图解快速排序
- 腾讯安全部门-大数据挑战赛-learner队伍(进入复赛)
- RILD
- keras中文文档笔记15——面向小数据集构建图像分类模型
- Servlet回顾总结