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

链接: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]内的元素之和

输入例子:
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

输入

复制

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

说明

 #include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack> using namespace std; typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
const int MOD = 1e9 + ; #define MemI(x) memset(x, -1, sizeof(x))
#define Mem0(x) memset(x, 0, sizeof(x))
#define MemM(x) memset(x, 0x3f, sizeof(x))
int a[MAXN];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = ;i <= n;++i)
scanf("%d", &a[i]);
//这里为下面的累加进行修正
for(int i = n;i > ;--i)
a[i] = a[i] - a[i - ];
int flag, l, r, num;
//求出修改区间的前缀和
for(int i = ;i <= m;++i)
{
scanf("%d%d%d%d", &flag, &l, &r, &num);
//这里注意题意
if(flag == )
a[l] -= num, a[r + ] += num;
else
a[l] += num, a[r + ] -= num;
}
for(int i = ;i <= n;++i)
a[i] = a[i] + a[i - ];
int x, y;
LL ans = ;
scanf("%d%d", &x, &y);
for(int i = x;i <= y;++i)
ans += a[i];
printf("%lld\n", ans);
return ;
}

最新文章

  1. slave IO流程之一:mysql登陆过程(mysql_real_connect)
  2. 配置log4j
  3. jQuery Sidebar 侧边栏
  4. zabbix 安装
  5. Fatal error: Cannot redeclare tran() (previously declared in
  6. 解决Win10默认占用80端口
  7. 微信开发第6章 通过accesstoken获取用户粉丝列表
  8. vue父子组件之间互相获取data值&amp;调用方法(非props)
  9. ThinkPHP5.0 开发手册
  10. oracle 相除后保留指定位数小数round()
  11. C++二进制字符串转十六进制字符串 十六进制字符串转二进制字符串
  12. spring boot 集成 shiro
  13. Python解析Wav文件并绘制波形的方法
  14. Markdown预览功能不可用解决方案
  15. c++ c++ 与 Java
  16. LintCode2016年8月8日算法比赛----中序遍历和后序遍历构造二叉树
  17. 简明 Python 教程--学习记录
  18. iOS:CALayer(17-12-06更)
  19. css控制文本单行或者多行溢出显示为省略号...
  20. Visual Studio各版本工程文件之间的转换 [转载]

热门文章

  1. 使用UpdatePanel时FileUpload失效的问题!【FileUpload上传文件失败】
  2. App常用性能测试工具清单
  3. cmake笔记
  4. Codeforces 900C. Remove Extra One(暴力)
  5. C++小学生四则运算_余伟淇_郑鉴
  6. QuotedStr函数
  7. DB2 函数快速构造测试数据
  8. SOA IN Real World
  9. 转载:ResultMap和ResultType在使用中的区别
  10. day8学python 各种简单模板