Magic cat

题意:Thanks to everyone’s help last week, TT finally got a cute cat. But what TT didn’t expect is that this is a magic cat.
One day, the magic cat decided to investigate TT’s ability by giving a problem to him. That is select n cities from the world map, and a[i]represents the asset value owned by the i-th city.
Then the magic cat will perform several operations. Each turn is to choose the city in the interval [l,r] and increase their asset value by cc. And finally, it is required to give the asset value of each city after qq operations.
Could you help TT find the answer?
大概就是n个点,每次给个[l,r],这个区间中所有元素加一个v;q次操作之后,输出所有的元素。

输入:
The first line contains two integers n,q (1≤n,q≤2⋅105) — the number of cities and operations.
The second line contains elements of the sequence aa: integer numbers a1,a2,…,an (−106≤ai≤106)
Then q lines follow, each line represents an operation. The i-th line contains three integers l,r and c (1≤l≤r≤n,−105≤c≤105) for the i-th operation.

输出:
Print n integers a1,a2,…,an one per line, and ai should be equal to the final asset value of the i-th city.

Example:

Here Insert Picture Description

Problem-solving ideas: differential line, multi-segment tree Intuit, note here use longlong, otherwise int to white.

Code:

#include<cstdio>
#include<iostream>
using namespace std;
int a[200005],b[200005];
int main()
{
    int n,q,x;
    scanf("%d%d",&n,&q);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(i==0)b[i]=a[i];
        else b[i]=a[i]-a[i-1];
    }
    while(q--)
    {
        int l,r,v;
        scanf("%d%d%d",&l,&r,&v);
        b[l-1]+=v;//差分
        b[r]-=v;
    }
    long long total=0;//用longlong
    for(int i=0;i<n;i++)
    {
        total+=b[i];
        if(i)printf(" ");
        printf("%lld",total);
    }
}
Published 15 original articles · won praise 0 · Views 222

Guess you like

Origin blog.csdn.net/qq_43653717/article/details/104972842