1369 - Answering Queries
Time Limit: 3 second(s)
Memory Limit: 32 MB
The problem you need to solve here is pretty simple. You are give a function f(A, n), where A is an array of integers and n is the number of elements in the array. f(A, n) is defined as follows:
long long f( int A[], int n ) { // n = size of A
long long sum = 0;
for( int i = 0; i
for( int j = i + 1; j
sum += A[i] - A[j];
return sum;
}
Given the array A and an integer n, and some queries of the form:
1) 0 x v (0 ≤ x < n, 0 ≤ v ≤ 106), meaning that you have to change the value of A[x] to v.
2) 1, meaning that you have to find f as described above.
Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
Each case starts with a line containing two integers: n and q (1≤ n, q ≤ 105). The next line contains n space separated integers between 0 and 106 denoting the array A as described above.
Each of the next q lines contains one query as described above.
Output
For each case, print the case number in a single line first. Then for each query-type "1" print one single line containing the value of f(A, n).
Sample Input
Output for Sample Input
1
3 5
1 2 3
1
0 0 3
1
0 2 1
1
Case 1:
-4
0
4
题解:我的思路本来是针对每次的修改,都在询问里面找值,不出意外肯定超时了,出来看了大神的题解,是针对每次修改再修改sum就妥了,比赛的时候就没想到。。。
代码:
#include
#include
#include
#include
#include
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=1e5+;
typedef long long LL;
LL a[MAXN],b[MAXN];
int main(){
int T,n,q,cnt=;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&q);
LL sum=;
for(int i=;i
sum=;
for(int i=n-;i>=;i--)sum+=a[i],b[i-]=sum;
//for(int i=0;i
b[n-]=;
sum=;
for(int i=;i
mem(b,);
printf("Case %d:\n",++cnt);
while(q--){
int t,x,v;
scanf("%d",&t);
if(t){
printf("%lld\n",sum);
}
else{
scanf("%d%d",&x,&v);
sum=sum+(v-a[x])*(n-x-)-x*(v-a[x]);
a[x]=v;
}
}
}
return ;
}
LightOJ - 1369 - Answering Queries(规律)
链接: https://vjudge.net/problem/LightOJ-1369 题意: The problem you need to solve here is pretty simple. ...
LightOJ 1369 Answering Queries(找规律)
题目链接:https://vjudge.net/contest/28079#problem/P 题目大意:给你数组A[]以及如下所示的函数f: long long f( int A[], int n ...
UVA - 12424 Answering Queries on a Tree(十棵线段树的树链剖分)
You are given a tree with N nodes. The tree nodes are numbered from 1 to N and have colors C1, C2,. ...
Hive 表数据的存储和压缩格式
SerDe * 按行存储 * 按列存储 file_format: : | SEQUENCEFILE 序列化(行存储) | TEXTFILE 文本格式(行存储)- (Default, depending ...
DNS主从服务器+mysql
1 .背景 BIND从文本文件中获取数据,这样容易因为编辑错误出现问题. BIND需要将数据加载到内存中,如果域或者记录较多,会消耗大量的内存. BIND启动时解析Zone文件,对于一个记录较多的DN ...
spoj gss2 : Can you answer these queries II 离线&;&;线段树
1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...
Find the maximum(规律,大数)
Find the maximum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
随机推荐
opencv MAT数据操作
1.存取单个像素值 最通常的方法就是 img.at(i,j) = 255; img.at(i,j)[0] = 255; 2.用指针扫描一幅图像 对于 ...
C/C++将一个整型数组拼接成一个字符串
参考: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include #include int ma ...
python数据分析工具包(1)——Numpy(一)
在本科阶段,我们常用的科学计算工具是MATLAB.下面介绍python的一个非常好用而且功能强大的科学计算库--Numpy. a powerful N-dimensional array object ...
WPF---Xaml中改变ViewModel的值
在开发中遇到实现如下需求的情景:一个输入框,旁边一个清空输入的按钮,当输入框中有内容时显示清空按钮,点击该按钮可以清空输入框内容,当输入框中无内容时隐藏按钮 当然这个需求使用wpf的绑定功能很容易实现 ...
设计模式 -->; (6)原型模式
原型(Prototype)模式 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 原型模式是一种创建型设计模式,Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知 ...
ERROR internal error: process exited while connecting to monitor
centos7.4 创建kvm虚拟机时报错 问题: [root@oldboy ~]# virt-install --virt-type kvm --os-type=linux --os-variant ...
Vue.directive添加全局指令详解
自定义指令创建: Vue.directive( 'mycolor(指令名称:推荐全部小写,驼峰命名会出现问题,看最后面)' , { bind:function(){}, //本例只介绍inserted ...
利用Clang(Python接口)来解析C++
1 背景说明 最近希望利用开源库来解析C++头文件,并做一些自动翻译.自动注释之类的工作.经过两天的调研,发现clang最有希望满足需求.clang提供了三套接口来共外部使用,liblang最适合作为 ...
Oracle date-time
Name Description ADDDATE() Add time values (intervals) to a date value ADDTIME() Add time CONVERT_TZ ...