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 ...
随机推荐
- 巩固javaweb第十七天
巩固内容: 文本域 文本域主要用于输入多行文字,如果输入的文字比较多,则可以采用文本域. 文本域的基本格式如下: <textarea rows="行数" name=" ...
- 深入理解mysql锁与事务隔离级别
一.锁 1.锁的定义 锁即是一种用来协调多线程或进程并发使用同一共享资源的机制 2.锁的分类 从性能上分类:乐观锁和悲观锁 从数据库操作类型上分类:读锁和写锁 从操作粒度上分类:表锁和行锁 2 ...
- Flink(六)【ParameterTool类】
ParameterTool 工具类 object ParameterToolTest { def main(args: Array[String]): Unit = { val params: Par ...
- 【leetcode】834. Sum of Distances in Tree(图算法)
There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are ...
- 100个Shell脚本—【脚本6】拷贝目录
[脚本6]拷贝目录 编写shell脚本,把/root/目录下的所有目录(只需要一级)拷贝到/tmp/目录下: 一.脚本 #!/bin/bash cd /root list=(`ls`) for i i ...
- go recover让崩溃的程序继续执行
package main import ( "fmt" "log" ) func de() { //recover() //可以打印panic的错误信息 //f ...
- CentOS 6.5下安装Python+Django+Nginx+uWSGI
1.安装Python31.1先安装zlib库及其他三方库安装uWSGI时需要使用zlib,否则执行python uwsgiconfig.py --build时会报ImportError,就是因为在安装 ...
- SpringBoot(1):初始SpringBoot
一. SpringBoot 简介 1. SpringBoot介绍 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特 ...
- Consumer方法结合Lambda表达式的应用
package com.itheima.demo05.Consumer;import java.util.function.Consumer;/** * @author newcityman * @d ...
- 记一次SQL Server insert触发器操作
需求:在河道水情表(ST_RIVER_R )新增插入数据时,更新实时数据表(SS_data) 中关联字段的值. 需求概括下:当A表中新增数据时,同时更新B表中的某字段 代码如下: USE [DBCNB ...