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:
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.
思路:
找规律,计算每个位置的贡献。
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;
}
标签:int,sum,Answering,LightOJ,long,++,Queries,include,LL 来源: https://www.cnblogs.com/YDDDD/p/11841948.html