CodeForces - 295A(思维)

题目链接
Description:
Greg has an array a = a1, a2, …, an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, …, ri by value di.

Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, …, yi to the array.

Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.
Input:
The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, …, an (0 ≤ ai ≤ 105) — the initial array.

Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105).

Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m).

The numbers in the lines are separated by single spaces.
output:
On a single line print n integers a1, a2, …, an — the array after executing all the queries. Separate the printed numbers by spaces.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
思路:
感觉思路很巧妙,当要记录区间 [l,r],令数组p[l]++,p[r+1]- -,再计算前缀和,令p[i]+=p[i-1],就可以发现数组p在区间[l,r]的值都变为1了,对题中要记录的操作的下标的区间这样处理,再对操作所对应的数组元素下标这样处理,便能得到最终结果。
code:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#define ios ios::sync_with_stdio(false), cin.tie(0);
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int maxn = 2e6 + 10;

ll s[maxn];
ll n, m, k;
ll l[maxn], r[maxn], d[maxn];
ll p[maxn], ind[maxn];

int main()
{
    ios;
    cin >> n >> m >> k;
    for (int i = 1; i <= n;i++){
        cin >> s[i];
    }
    for (int i = 1; i <= m;i++){
        cin >> l[i] >> r[i] >> d[i];
    }
    for (int i = 1,a,b; i <= k;i++){
        cin >> a >> b;
        p[a]++, p[b + 1]--;
    }
    for (int i = 1; i <= m;i++){
        p[i] += p[i - 1];
    }
    for (int i = 1; i <= m;i++){
        ind[l[i]] += p[i] * d[i], ind[r[i] + 1] -= p[i] * d[i];
    }
    for (int i = 1; i <= n;i++){
        ind[i] += ind[i - 1];
        cout << s[i] + ind[i] << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值