LightOJ - 1369 - Answering Queries(规律)
链接:
https://vjudge.net/problem/LightOJ-1369
题意:
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:
0 x v (0 ≤ x < n, 0 ≤ v ≤ 106), meaning that you have to change the value of A[x] to v.
1, meaning that you have to find f as described above.
思路:
找规律,计算每个位置的贡献。
a[i]的贡献 = (n-1-i)a[i]-ia[i];
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int MAXN = 1e5+10;
const int MOD = 1e9+7;
LL A[MAXN];
int n, q;
LL f(LL A[], int n)
{
LL sum = 0;
for (int i = 0;i < n;i++)
{
for (int j = i+1;j < n;j++)
sum += A[i]-A[j];
}
return sum;
}
int main()
{
int t, cnt = 0;
scanf("%d", &t);
while(t--)
{
printf("Case %d:", ++cnt);
scanf("%d%d", &n, &q);
for (int i = 0;i < n;i++)
scanf("%lld", &A[i]);
LL sum = 0;
for (int i = 0;i < n;i++)
{
sum += (n-1-i)*A[i];
sum -= i*A[i];
}
int op, x, v;
puts("");
while(q--)
{
scanf("%d", &op);
if (op == 0)
{
scanf("%d%d", &x, &v);
sum -= (n-1-x)*A[x];
sum += x*A[x];
A[x] = v;
sum += (n-1-x)*A[x];
sum -= x*A[x];
}
else
{
printf("%lld\n", sum);
}
}
}
return 0;
}
LightOJ - 1369 - Answering Queries(规律)的更多相关文章
- LightOJ 1369 Answering Queries(找规律)
题目链接:https://vjudge.net/contest/28079#problem/P 题目大意:给你数组A[]以及如下所示的函数f: long long f( int A[], int n ...
- 1369 - Answering Queries(规律)
1369 - Answering Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 M ...
- 1369 - Answering Queries
1369 - Answering Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 ...
- LightOJ 1188 Fast Queries(简单莫队)
1188 - Fast Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Gi ...
- lightoj Again Array Queries
1100 - Again Array Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 ...
- Fibsieve`s Fantabulous Birthday LightOJ - 1008(找规律。。)
Description 某只同学在生日宴上得到了一个N×N玻璃棋盘,每个单元格都有灯.每一秒钟棋盘会有一个单元格被点亮然后熄灭.棋盘中的单元格将以图中所示的顺序点亮.每个单元格上标记的是它在第几秒被点 ...
- Harmonic Number (II) LightOJ - 1245 (找规律?。。。)
题意: 求前n项的n/i 的和 只取整数部分 暴力肯定超时...然后 ...现在的人真聪明...我真蠢 觉得还是别人的题意比较清晰 比如n=100的话,i=4时n/i等于25,i=5时n/i等于20 ...
- 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,. ...
- LightOJ - 1410 - Consistent Verdicts(规律)
链接: https://vjudge.net/problem/LightOJ-1410 题意: In a 2D plane N persons are standing and each of the ...
随机推荐
- Python基础 — eval 函数的作用
eval函数就是实现list.dict.tuple与str之间的相互转化str函数把list,dict,tuple转为为字符串 # 字符串转换成列表a = "[[1,2], [3,4], [ ...
- kafka服务端实验记录
kafka单机实验: 环境准备: 1.下载kafka,zookeeper,并解压 wget http://mirror.bit.edu.cn/apache/kafka/2.3.0/kafka_2.11 ...
- mvn: command not found in Jenkins slave
在Jenkins上添加了一个slave node, 并绑定了一个团队项目,通过shell来执行后续操作,结果卡在了 mvn: command not found 其实这个node上是配置了maven的 ...
- redis 安装使用 & SpringBoot Redis配置
1.安装 https://www.cnblogs.com/dingguofeng/p/8709476.html https://www.runoob.com/redis/redis-keys.html ...
- Mycat分布式数据库架构解决方案--schema.xml详解
echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 该文件 ...
- BUAA_OO第三单元总结性博客作业——JML
一.JML 在第三单元的面向对象课程中我们第一次接触了JML语言以及基于JML规范的规格化设计.在之前一系列关于面向对象思想的学习认识中,我们知道了Java是一种面向对象的语言,面向对象思想的一个重要 ...
- Lua for - ipairs,pairs,# 遍历的运行时间
- css盒模型。边框和内外边距
css盒模型: 外边距 边框 内填充 内容 盒模型分为两种: 标准盒模型: 怪异盒模型(IE盒模型): 边框:border border: 10px solid blue;表示设置10像素蓝色实线条的 ...
- Android studio来开发移动App--SQA计划和系统测试规程
概述 团队分工 产品需求 团队合作 每日例会 思维导图 UML 产品代码 团队分工 成员:刘鹏芝,罗樟,王小莉,沈兴艳,徐棒,彭康明,胡广键 产品用户:王小莉 需求规约:彭康明,罗樟 UML:刘鹏芝, ...
- Vivado debug异常现象
前言 bit文件和ltx文件的信号位宽不匹配问题.用了dont_touch等属性没用... WARNING: [Labtools 27-1972] Mismatch between the desig ...