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 ...
随机推荐
- class——python编程从入门到实践
创建和使用类 1. 创建Dog类 class Dog: """一次模拟小狗的简单尝试""" def __init__(self, name, ...
- CMDB和自动化运维
CMDB和自动化运维 IT运维的分类 IT运维,指的是对已经搭建好的网络,软件,硬件进行维护.运维领域也是细分的,有硬件运维和软件运维 硬件运维主要包括对基础设施的运维,比如机房的设备,主机的硬盘,内 ...
- docker(四):集群swarm
docker使用入门(四):集群swarm swarm是一组位于同一集群且运行docker的机器,用户可以通过swarm manager向swarm输入命令,swarm中的机器可以是虚拟机也可以是物理 ...
- pythony--运算符
python运算符 运算符表达式 python运算符: 赋值运算符.算数运算符.关系运算符.逻辑运算符 python表达式: 是将不同的数据(包括变量.函数)用运算符号按一定规则连接起来的一种式子 ...
- redis客户端介绍及php客户端的下载安装
一.PHP客户端1.官方提供了几款PHP客户端,包括amphp/redis.phpredis.Predis.Rediska.推荐使用官方推荐的两款客户端,phpredis.Predis2.phpred ...
- Java JDK1.8源码学习之路 2 String
写在最前 String 作为我们最常使用的一个Java类,注意,它是一个引用类型,不是基本类型,并且是一个不可变对象,一旦定义 不再改变 经常会定义一段代码: String temp = " ...
- 前端开发 Angular
https://www.angularjs.net.cn/tutorial/18.html
- 8 search中的timeout参数
默认的search,是没有时间限制的.比如,一个search,可能要10分钟才能搜完,那么,es就会等10分钟,直到结果出来. 然而,在某些场景下,客户是等不了10分钟的.比如,电商网站,客户宁可 ...
- flutter 动画 practice
import 'package:flutter/material.dart'; import 'dart:io'; import 'dart:async'; main() => runApp(M ...
- 解决internal/modules/cjs/loader.js:638 throw err; ^ Error: Cannot find module 'resolve'
internal/modules/cjs/loader.js:638 throw err; ^ Error: Cannot find module 'resolve' 根据提示可以知道有依赖没有安装完 ...