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就妥了,比赛的时候就没想到。。。

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #define mem(x,y) memset(x,y,sizeof(x))
  7. using namespace std;
  8. const int INF=0x3f3f3f3f;
  9. const int MAXN=1e5+;
  10. typedef long long LL;
  11. LL a[MAXN],b[MAXN];
  12. int main(){
  13. int T,n,q,cnt=;
  14. scanf("%d",&T);
  15. while(T--){
  16. scanf("%d%d",&n,&q);
  17. LL sum=;
  18. for(int i=;i<n;i++)scanf("%lld",a+i);
  19. sum=;
  20. for(int i=n-;i>=;i--)sum+=a[i],b[i-]=sum;
  21. //for(int i=0;i<n;i++)printf("%d ",b[i]);puts("");
  22. b[n-]=;
  23. sum=;
  24. for(int i=;i<n-;i++)sum+=(a[i]*(n-i-)-b[i]);
  25. mem(b,);
  26. printf("Case %d:\n",++cnt);
  27. while(q--){
  28. int t,x,v;
  29. scanf("%d",&t);
  30. if(t){
  31. printf("%lld\n",sum);
  32. }
  33. else{
  34. scanf("%d%d",&x,&v);
  35. sum=sum+(v-a[x])*(n-x-)-x*(v-a[x]);
  36. a[x]=v;
  37. }
  38. }
  39. }
  40. return ;
  41. }

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. iOS工程上传AppStore时遇到的问题“ERROR ITMS-90046”解析

    在我们将代码写完整,测试没有bug之后,我们就可以将它上传到AppStore了,上传的过程只要操作正确并不会有太大的问题,但是打包的过程中会出现一些小问题,导致打的包不能上传或者上传的时候会出现错误. ...

  2. spring4.1.3+springmvc+mybatis3.2.1整合

    注意:这里使用了mybatis3.2.1版本,刚开始用了3.4.1的版本,会报一个很奇怪的错(java.lang.AbstractMethodError: org.mybatis.spring.tra ...

  3. TreeSet两种比较

    TreeSet底层数据结构是二叉树 判断对象是否一致是通过是对象自身有比较的方法,即使类实现Comparable接口,重写compareTo方法,自己定义比较规则, 若是不想用元素本身的比较方法,又不 ...

  4. BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏( floyd )

    直接floyd.. ---------------------------------------------------------------------------- #include<c ...

  5. kafka集群配置与测试

    刚接触一些Apache Kafka的内容,用了两天时间研究了一下,仅以此文做相关记录,以供学习交流.  概念: kafka依赖的项: 1. 硬件上,kafka利用线性存储来进行硬盘直接读写. 2. k ...

  6. python自学笔记(七)排序与多级排序

    一.sorted内置方法 a = [1,2,3,4] 从大到小(翻转) a = sorted(a,reverse = True) #生成新对象,不会原地修改,需要重新赋值 print a --> ...

  7. 循环-10. 求序列前N项和(15)

    #include<iostream>#include<iomanip>using namespace std;int main(){    double i,n,t,a,b;  ...

  8. Linux下nc命来实现文件传输

    发送端:cat test.txt | nc -l -p 6666或者nc -l -p 6666 < test.txt 有些版本不要在 -p[监听6666端口,等待连接](设发送端IP为10.20 ...

  9. QTableView另类打印解决方案(复用render函数去解决print问题)

    Qt QTableView另类打印解决方案     上回书说道Qt的model/view,我就做了个demo用于显示数据库中的内容.没想到tableview的打印竟然成了问题.我困惑了,难道Qt不应该 ...

  10. Sublime Text 2 - There are no packages available for installation

    解决Sublime Text 2 package Control 无法安装插件的问题 错误提示 here are no packages available for installation 问题解决 ...