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 |
Note
Dataset is huge, use faster I/O methods.
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<math.h>
6 #include<stdlib.h>
7 typedef long long LL;
8 LL bns[200000];
9 int main(void)
10 {
11 int k;
12 int i,j;
13 scanf("%d",&k);
14 int s;
15 int p,q;
16 LL ans=0;
17 for(s=1; s<=k; s++)
18 {
19 scanf("%d %d",&p,&q);
20 for(i=1; i<=p; i++)
21 {
22 scanf("%lld",&bns[i]);
23 }
24 ans=0;
25 for(i=1; i<=p; i++)
26 {
27 ans+=(LL)(p-2*i+1)*(LL)bns[i];
28 }
29 printf("Case %d:\n",s);
30 while(q--)
31 {
32 int ask;
33 int n,m;
34 scanf("%d",&ask);
35 if(ask==1)
36 {
37 printf("%lld\n",ans);
38 }
39 else
40 {
41 scanf("%d %d",&n,&m);
42
43 {
44 ans-=(LL)(p-2*(n+1)+1)*bns[n+1];
45 ans+=(LL)(p-2*(n+1)+1)*(LL)m;
46 bns[n+1]=m;
47 }
48 }
49 }
50 }
51 return 0;
52 }
1369 - Answering Queries的更多相关文章
- 1369 - Answering Queries(规律)
1369 - Answering Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 M ...
- 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 ...
- 可以通过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 ...
- CodeForces Round #515 Div.3 C. Books Queries
http://codeforces.com/contest/1066/problem/C You have got a shelf and want to put some books on it. ...
- 实践 HTML5 的 CSS3 Media Queries
先来介绍下 media,确切的说应该是 CSS media queries(CSS 媒体查询),媒体查询包含了一个媒体类型和至少一个使用如宽度.高度和颜色等媒体属性来限制样式表范围的表达式.CSS3 ...
随机推荐
- APP工程师接入Telink Mesh流程 -3
加密是为了使网络更加的安全.健壮,若由于login.加密等流程 严重影响了 开发进程,也可以通过 修改SDK 固件 将login.加密 环节取消 1.发送数据.接受数据加密,解密去掉 mesh_sec ...
- python基础实战
字符串的互相转换 字典的排序 字典的排序可以直接把,key值或者,values拿出来排序 也可以用dict.items拿出所有的key,value的值再加key=lambda x:x[1] 来排序. ...
- 第一个基础框架 — mybatis框架 — 更新完毕
1.Mybatis是什么? 百度百科一手 提取一下重点: MyBatis 本是apache的一个开源项目iBatis.即:mybatis的原名为:ibatis 2010年迁移到google code, ...
- 日常Java测试第一段 2021/11/12
课堂测试一 package word_show;import java.io.BufferedReader;import java.io.FileNotFoundException;import ja ...
- 日常Java 2021/9/27
题目: 在某个比赛中,有6个评委为参赛的选手打分,分数为1-100的随机整数.选手的最后得分为:除去最高分和最低分后的4个评委分值的平均值(不考虑小数部分). package m; import ja ...
- Spark集群环境搭建——部署Spark集群
在前面我们已经准备了三台服务器,并做好初始化,配置好jdk与免密登录等.并且已经安装好了hadoop集群. 如果还没有配置好的,参考我前面两篇博客: Spark集群环境搭建--服务器环境初始化:htt ...
- 【STM8】添加头文件、加入库函数
下面顺便放上STM8L15x-16x-05x的固件库,以及固件库里没有的<stm8l15x_conf.h> 链接打开后,还会发现另外两个文件夹,<src><inc> ...
- 【Linux】【Basis】块存储,文件存储,对象存储
1. 块存储: 定义:这种接口通常以QEMU Driver或者Kernel Module的方式存在,这种接口需要实现Linux的Block Device的接口或者QEMU提供的Block Driver ...
- API测试最佳实践 - 身份验证
适用等级:高级 1. 概况 身份验证通常被定义为是对某个资源的身份的确认的活动,这里面资源的身份指代的是API的消费者(或者说是调用者).一旦一个用户的身份验证通过了,他将被授权访问那些期待访问的资源 ...
- vue-cli4脚手架搭建三
组件传值 <script> import LunBo from "./LunBo"; export default { name: 'Home', components ...