• 牛客小白月赛5 I


    看到一份不错的操作。。。。。

    链接:https://www.nowcoder.com/acm/contest/135/I

    来源:牛客网

      Apojacsleam喜欢数组。

        他现在有一个n个元素的数组a,而他要对a[L]-a[R]进行M次操作:

            操作一:将a[L]-a[R]内的元素都加上P

            操作二:将a[L]-a[R]内的元素都减去P

        最后询问a[l]-a[r]内的元素之和?
        请认真看题干及输入描述。

    输入描述:

    输入共M+3行:

    第一行两个数,n,M,意义如“题目描述”

    第二行n个数,描述数组。

    第3-M+2行,共M行,每行四个数,q,L,R,P,若q为1则表示执行操作2,否则为执行操作1

    第4行,两个正整数l,r

    输出描述:

    一个正整数,为a[l]-a[r]内的元素之和

    示例1

    输入

    复制
    10 5
    1 2 3 4 5 6 7 8 9 10
    1 1 5 5
    1 2 3 6
    0 2 5 5 
    0 2 5 8
    1 4 9 6
    2 7

    输出

    复制
    23

    说明

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <sstream>
     7 #include <algorithm>
     8 #include <set>
     9 #include <map>
    10 #include <vector>
    11 #include <queue>
    12 #include <iomanip>
    13 #include <stack>
    14 
    15 using namespace std;
    16 
    17 typedef long long LL;
    18 const int INF = 0x3f3f3f3f;
    19 const int MAXN = 1000005;
    20 const int MOD = 1e9 + 7;
    21 
    22 #define MemI(x) memset(x, -1, sizeof(x))
    23 #define Mem0(x) memset(x, 0, sizeof(x))
    24 #define MemM(x) memset(x, 0x3f, sizeof(x))
    25 int a[MAXN];
    26 int main()
    27 {
    28     int n, m;
    29     scanf("%d%d", &n, &m);
    30     for(int i = 1;i <= n;++i)
    31         scanf("%d", &a[i]);
    32     //这里为下面的累加进行修正
    33     for(int i = n;i > 1;--i)
    34         a[i] = a[i] - a[i - 1];
    35     int flag, l, r, num;
    36     //求出修改区间的前缀和
    37     for(int i = 1;i <= m;++i)
    38     {
    39         scanf("%d%d%d%d", &flag, &l, &r, &num);
    40         //这里注意题意
    41         if(flag == 1)
    42             a[l] -= num, a[r + 1] += num;
    43         else
    44             a[l] += num, a[r + 1] -= num;
    45     }
    46     for(int i = 2;i <= n;++i)
    47         a[i] = a[i] + a[i - 1];
    48     int x, y;
    49     LL ans = 0;
    50     scanf("%d%d", &x, &y);
    51     for(int i = x;i <= y;++i)
    52         ans += a[i];
    53     printf("%lld
    ", ans);
    54     return 0;
    55 }
    现在所有的不幸都是以前不努力造成的。。。
  • 相关阅读:
    tp5.1 查询自定义排序(按照查询结果顺序排序)
    Laravel Auth 用户认证
    Laravel 文件上传
    Laravel 缓存操作
    Laravel 验证码
    第46章:TEB
    第45章:TLS回调函数
    第4章:逆向分析技术--32位软件逆向技术
    第43章:内核6中的DLL注入
    第42章:内核6中的会话
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/9353025.html
Copyright © 2020-2023  润新知