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. 设计一个有getMin功能的栈

    [说明]: 本文是左程云老师所著的<程序员面试代码指南>第一章中“设计一个有getMin功能的栈”这一题目的C++复现. 本文只包含问题描述.C++代码的实现以及简单的思路,不包含解析说明 ...

  2. 关于playframework2.5

    加入了很多新东西: 1.用akka streams 替换了大部分 iteratee-based async io,当然还有一些模块在用iteratees 2.java 的一些API 做了调整升级,以及 ...

  3. PHP创建定义数组

    $array = array();      $array["key"] = "values";  ?> 在PHP中声明数组的方式主要有两种:1.用arr ...

  4. winform代码:关联窗体数据更新,删除dataGridview中选中的一行或多行

    一.关联窗体数据更新 关联窗体数据修改时,如果一个为总体数据显示窗体A,另一个为详细修改窗体B,从A进入B,在B中对数据进行修改,然后返回A,这时A窗体的数据需要更新. 我采用最简单的方法,首先保证每 ...

  5. [LeetCode]题解(python):080-Remove Duplicates from Sorted Array II

    题目来源: https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意分析: 跟定一个排好序的数组.修改这个数组使 ...

  6. 高质量程序设计指南C/C++语言——C++/C常量(2)

  7. spss

    编辑 SPSS(Statistical Product and Service Solutions),“统计产品与服务解决方案”软件.最初软件全称为“社会科学统计软件包” (SolutionsStat ...

  8. java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp

    http://stackoverflow.com/questions/13269564/java-lang-classcastexception-oracle-sql-timestamp-cannot ...

  9. Php 使用 fsockopen发送http请求

    <?php function HTTP_Post($URL,$data, $referrer="") { // parsing the given URL $URL_Info ...

  10. HDU 2852 KiKi's K-Number

    权值线段树 #include <cstdio> #include <cstring> const int N=200000,M=220000; int k,q,x,y,sum[ ...