zoukankan      html  css  js  c++  java
  • Alyona and flowers

    Alyona and flowers
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th flower is ai. The mood can be positive, zero or negative.

    Let's define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl's happiness its mood multiplied by the number of chosen subarrays the flower is in.

    For example, consider the case when the mother has 5 flowers, and their moods are equal to 1,  - 2, 1, 3,  - 4. Suppose the mother suggested subarrays (1,  - 2), (3,  - 4), (1, 3), (1,  - 2, 1, 3). Then if the girl chooses the third and the fourth subarrays then:

    • the first flower adds 1·1 = 1 to the girl's happiness, because he is in one of chosen subarrays,
    • the second flower adds ( - 2)·1 =  - 2, because he is in one of chosen subarrays,
    • the third flower adds 1·2 = 2, because he is in two of chosen subarrays,
    • the fourth flower adds 3·2 = 6, because he is in two of chosen subarrays,
    • the fifth flower adds ( - 4)·0 = 0, because he is in no chosen subarrays.

    Thus, in total 1 + ( - 2) + 2 + 6 + 0 = 7 is added to the girl's happiness. Alyona wants to choose such subarrays from those suggested by the mother that the value added to her happiness would be as large as possible. Help her do this!

    Alyona can choose any number of the subarrays, even 0 or all suggested by her mother.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of flowers and the number of subarrays suggested by the mother.

    The second line contains the flowers moods — n integers a1, a2, ..., an ( - 100 ≤ ai ≤ 100).

    The next m lines contain the description of the subarrays suggested by the mother. The i-th of these lines contain two integers li and ri(1 ≤ li ≤ ri ≤ n) denoting the subarray a[li], a[li + 1], ..., a[ri].

    Each subarray can encounter more than once.

    Output

    Print single integer — the maximum possible value added to the Alyona's happiness.

    Examples
    input
    5 4
    1 -2 1 3 -4
    1 2
    4 5
    3 4
    1 4
    output
    7
    input
    4 3
    1 2 3 4
    1 3
    2 4
    1 1
    output
    16
    input
    2 2
    -1 -2
    1 1
    1 2
    output
    0
    Note

    The first example is the situation described in the statements.

    In the second example Alyona should choose all subarrays.

    The third example has answer 0 because Alyona can choose none of the subarrays.

    分析:维护前缀和,若询问区间和>0,则对答案有贡献;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <unordered_map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    #define intxt freopen("in.txt","r",stdin)
    const int maxn=1e5+10;
    using namespace std;
    int gcd(int p,int q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,a[maxn],sum[maxn];
    int main()
    {
        int i,j;
        scanf("%d%d",&n,&m);
        rep(i,1,n)scanf("%d",&a[i]),sum[i]=sum[i-1]+a[i];
        ll ans=0;
        rep(i,1,m)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            if(sum[r]-sum[l-1]>0)ans+=sum[r]-sum[l-1];
        }
        printf("%lld
    ",ans);
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    Windows下安装tesserocr
    Python中的那些“坑”
    【Python3爬虫】用Python中的队列来写爬虫
    【Python3爬虫】常见反爬虫措施及解决办法(三)
    【Python3爬虫】常见反爬虫措施及解决办法(二)
    【Python3爬虫】常见反爬虫措施及解决办法(一)
    【Python3爬虫】教你怎么利用免费代理搭建代理池
    【Python3爬虫】自动查询天气并实现语音播报
    【Python3爬虫】百度一下,坑死你?
    【Python3爬虫】为什么你的博客没人看呢?
  • 原文地址:https://www.cnblogs.com/dyzll/p/6102910.html
Copyright © 2011-2022 走看看