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 ...
随机推荐
- 编程艺术第十六~第二十章:全排列/跳台阶/奇偶调序,及一致性Hash算法
目录(?)[+] 第十六~第二十章:全排列,跳台阶,奇偶排序,第一个只出现一次等问题 作者:July.2011.10.16.出处:http://blog.csdn.net/v_JULY_v. 引言 ...
- CAS简介
概念 CAS(Compare And Swap 比较并交换),是 乐观锁 的一种典型实现机制. 乐观锁主要的两个步骤:冲突检测.数据更新. 当多个线程尝试使用CAS同时更新通过一个变量的时候,只有一个 ...
- MapReduce04 框架原理Shuffle
目录 2 MapReduce工作流程 3 Shuffle机制(重点) 3.1 Shuffle机制 3.2 Partition分区 默认Partitioner分区 自定义Partitioner分区 自定 ...
- 零基础学习java------day5------do....while循环、嵌套、方法(函数)
1 do...while循环 格式 初始化语句; do { 循环体语句; 控制条件语句; }while(判断条件语句); 流程: 先执行初始化语句 再执行循环体语句 再执行条件控制语句 再做条件的判 ...
- linux允许直接以root身份ssh登录
1. sudo su - 2. vim /etc/ssh/sshd_config 3. let "PermitRootLogin" equal yes 4. :wq 5. serv ...
- [项目总结]Android 手动显示和隐藏软键盘
1.方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) 1 InputMethodManager imm = (InputMethodManager) getSystemService(Cont ...
- SpringBoot(4):整合Mybatis
1. 导入mybatis所需要的依赖 1 <!-- 引入 myBatis,这是 MyBatis官方提供的适配 Spring Boot的--> 2 <dependency> 3 ...
- feign中开启熔断的书写步骤
/** 1.在pom.xml中引入依赖 2.在application.yaml中开启hystrix 3.在方法上配置熔断类 4.书写接口的实现类 **/ //1.在pom.xml中引 ...
- PL/SQL实例1
declare --定义游标 cursor cemp is select to_char(hiredate,'yyyy') from emp; phiredate varchar2(4); ...
- 【JAVA今法修真】 第一章 今法有万象 百家欲争鸣
大家好,我是南橘,因为这段时间很忙,忙着家里的事情,忙着工作的事情,忙着考试的事情,很多时候没有那么多经历去写新的东西,同时,也是看了网上一些比较新颖的文章输出方式,自己也就在想,我是不是也可以这样写 ...