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