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 ...
随机推荐
- jquery时间轴tab切换效果实现结合swiper实现滑动显示效果
需求:根据时间轴进行tab页面内容切换(时间轴需要滑动查看并选择) 实现思路: 结合swiper插件实现滑动显示效果 根据transform: translateX进行左侧切换效果的实现(具体实现cs ...
- Spring的事务传播机制(通俗易懂)
概述 Spring的事务传播机制有7种,在枚举Propagation中有定义. 1.REQUIRED PROPAGATION_REQUIRED:如果当前没有事务,就创建一个新事务,如果当前存在事务,就 ...
- Linux学习 - 系统命令sudo权限
1 功能 root把超级用执行的命令赋予普通用户执行 2 使用 visudo 或 vim /etc/sudoers 说明: root 用户名 ALL=(ALL) 被管理主机的地址=(可使用的身份) A ...
- Mysql 常见报错和疑问汇总
1.初始化数据库的时候报错 error while loading shared libraries: libstdc++.so.5: cannot open shared object file: ...
- Spring Boot事务支持
一.创建项目 二.添加依赖 <dependencies> <dependency> <groupId>org.projectlombok</groupId&g ...
- Spring Boot中注解@ConfigurationProperties
在Spring Boot中注解@ConfigurationProperties有三种使用场景,而通常情况下我们使用的最多的只是其中的一种场景.本篇文章带大家了解一下三种场景的使用情况. 场景一 使用@ ...
- JS 的三种定义变量 var let const
Let 只在 let 命令所在的代码块内有效,在外就会报错 Let 是块级作用域,函数内部使用let定义后,对函数外部无影响 Let/const 不存在变量提升,使用前一定要声明后,在使用,否则会报错 ...
- 使用JSP实现输出
一.在JSP页面添加java代码,实现输出,java代码写在<% %>中. 代码示例1: <body> <!-- HTML注释 --> <%-- JSP注释 ...
- C++内存管理:new / delete 和 cookie
new 和 delete C++的内存申请和释放是通过 new 和 delete 实现的, 而new 和 delete 其实就是通过 malloc 和 free 实现的. new 申请内存分为三个步骤 ...
- Fiddler抓包ios设备
Fiddler绝对称得上是"抓包神器", Fiddler不但能截获各种浏览器发出的HTTP请求, 也可以截获各种智能手机发出的HTTP/HTTPS请求. Fiddler能捕获ISO ...