ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LightOJ - 1369 - Answering Queries(规律)

2019-11-12 14:51:07  阅读:408  来源: 互联网

标签:int sum Answering LightOJ long ++ Queries include LL


链接:

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有