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 ...
随机推荐
- [转帖]Linux教程(12)- linux输入输出重定向
Linux教程(12)- linux输入输出重定向 2018-08-21 22:57:02 钱婷婷 阅读数 49更多 分类专栏: Linux教程与操作 Linux教程与使用 版权声明:本文为博主原 ...
- PHP提取中英文首字母的方法(首字母索引)
function Getzimu($str) { $str= iconv("UTF-8","gb2312", $str);//如果程序是gbk的,此行就要注释掉 ...
- P-R曲线深入理解
P-R曲线就是精确率precision vs 召回率recall 曲线,以recall作为横坐标轴,precision作为纵坐标轴.首先解释一下精确率和召回率. 解释精确率和召回率之前,先来看下混淆矩 ...
- nRF24L01P数据传输速率
项目要用nRF24L01P做语音的传输,数据量可想而知. 发送端按照8KHz/s采样率,每次采样双声道,16位深度(2Bytes). 数据量算一下就是8000x2x2=32000(Bytes) nRF ...
- 小程序的组件插槽使用slot===以及小程序多个插槽使用方法 三步骤
===================== 小程序多个插槽使用方法 三步骤 小程序多个插槽第一步 小程序组件内使用多个插槽第二部 小程序使用多个插槽第三部
- 微软.NET CORE 3.0 预览版 7 发布:大幅减少 SDK 空间大小
据悉,这个预览版是 .Net Core 3 中重要的版本,可以视为原计划在 7 月发布的 RC 版本 (引自微软 .NET Core 首席 Program Manager Richard 先生原话), ...
- java之hibernate之单向的多对多关联映射
这篇 单向的多对多关联映射 1.如何在权限管理中,角色和权限之间的关系就是多对多的关系,表结构为: 2.类结构 Permission.java public class Permission impl ...
- 有关mysql的utf8和utf8mb4,以及Illegal mix of collations for operation 'like'
参考以下几个帖子: https://www.cnblogs.com/install/p/4417527.html https://blog.csdn.net/Yetmoon/article/detai ...
- 浅谈Vue.js2.0某些概念
Vue.js2.0是一套构建用户界面的渐进式框架,目标是实现数据驱动和组件系统. A 渐进式框架 Vue.js是一个提供MVVM数据双向绑定的库,只专注于UI层面,这是它的核心.它本身没有解决SP ...
- 使用HBuilderX打包成app之后点击返回按钮让它返回上一个页面
首先:下载引入mui.min.js文件,MUI框架mui-min.js文件github地址 https://github.com/dcloudio/mui 下载之后并在index.html文件中引入如 ...