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 < n; i++ )

for( int j = i + 1; j < n; 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<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#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<n;i++)scanf("%lld",a+i);
sum=;
for(int i=n-;i>=;i--)sum+=a[i],b[i-]=sum;
//for(int i=0;i<n;i++)printf("%d ",b[i]);puts("");
b[n-]=;
sum=;
for(int i=;i<n-;i++)sum+=(a[i]*(n-i-)-b[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 ;
}

1369 - Answering Queries(规律)的更多相关文章

  1. 1369 - Answering Queries

    1369 - Answering Queries    PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 ...

  2. LightOJ - 1369 - Answering Queries(规律)

    链接: https://vjudge.net/problem/LightOJ-1369 题意: The problem you need to solve here is pretty simple. ...

  3. LightOJ 1369 Answering Queries(找规律)

    题目链接:https://vjudge.net/contest/28079#problem/P 题目大意:给你数组A[]以及如下所示的函数f: long long f( int A[], int n  ...

  4. 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,. ...

  5. Hive 表数据的存储和压缩格式

    SerDe * 按行存储 * 按列存储 file_format: : | SEQUENCEFILE 序列化(行存储) | TEXTFILE 文本格式(行存储)- (Default, depending ...

  6. DNS主从服务器+mysql

    1 .背景 BIND从文本文件中获取数据,这样容易因为编辑错误出现问题. BIND需要将数据加载到内存中,如果域或者记录较多,会消耗大量的内存. BIND启动时解析Zone文件,对于一个记录较多的DN ...

  7. 可以通过shadowserver来查看开放的mdns(用以反射放大攻击)——中国的在 https://mdns.shadowserver.org/workstation/index.html

    Open mDNS Scanning Project 来自:https://mdns.shadowserver.org/ If you are looking at this page, then m ...

  8. HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)

    题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...

  9. 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 ...

随机推荐

  1. Sublime Text3快捷方式总结

    Ctrl+P快速查找 Ctrl+D多行游标//同时选中多个地方进行编辑 Ctrl+F查找替换 Ctrl+H查找替换 Ctrl+G快速跳到某一行 Ctrl+shift+P命令模式 在命令模式下设置语法: ...

  2. 2015.8.3 Java

    今天继续学习Java  用的是eclipse IDE 这个ide怪怪的,但是有一个很方便的功能 就是通过右键选择source 可以点击Generate Getters and Setters生成属性的 ...

  3. 用js捕捉鼠标连续点击三次事件怎么实现啊

    var count = 0, timer; document.onclick = function(){ if(count < 2){ if(timer){ clearTimeout(timer ...

  4. Android ListView 删除动画

    Android 的ListView在删除条目时,被删除的条目直接消失,比较生硬,在此实现一下删除动画,大家一起探讨:主要实现原理即是通过Animator来实现被删除条目的动画效果,然后在动画结束时通过 ...

  5. C++的常量折叠(三)

    背景知识 在开始之前先说一下符号表,这个编译器中的东西.下面看一下百度百科中的描述: 符号表是一种用于语言翻译器中的数据结构.在符号表中,程序源代码中的每个标识符都和它的声明或使用信息绑定在一起,比如 ...

  6. Maven POM配置释疑

    1.  对于有父子关系的Project, 父POM中依赖使用dependencies 和 dependencyManagement 的区别: dependencies: 即使子项目中不写该依赖项,仍然 ...

  7. C# 读书笔记之访问虚方法、重写方法和隐藏方法

    C#允许派生类中的方法与基类中方法具有相同的签名:基类中使用关键字virtual定义虚方法:然后派生类中使用关键字override来重写方法,或使用关键字new来覆盖方法(隐藏方法). 重写方法用相同 ...

  8. C#手机充值

    C#手机充值系统开发(基于聚合数据) 说是手机充值系统有点装了,其实就是调用了聚合数据的支付接口,其实挺简单的事 但是我发现博客园竟然没有类似文章,我就个出头鸟把我的代码贡献出来吧 首先说准备工作: ...

  9. 常见排序算法C++总结

    看了总结图,我这里就总结一下 直接插入排序,冒泡排序,快速排序,堆排序和归并排序,使用C++实现 重新画了总结图 直接插入排序 整个序列分为有序区和无序区,取第一个元素作为初始有序区,然后第二个开始, ...

  10. ie的selectNodes函数和firefox的document.evaluate

    selectNodes() 方法用一个 XPath 查询选择节点. nodeObject.selectNodes(query)query  XPath 查询串. 包含了匹配查询的节点的一个 NodeL ...