Greg and Array CodeForces 296C 差分数组
Greg and Array CodeForces 296C 差分数组
题意
是说有n个数,m种操作,这m种操作就是让一段区间内的数增加或则减少,然后有k种控制,这k种控制是说让m种操作中的一段区域内的操作来实际进行,问进行完k种控制后,这n个数变成了啥。
解题思路
我开始使用了最简单的差分,就是把m种操作存到结构体数组中,然后在读取k中控制时,按照要求执行之前结构体数组中的一段区间内的操作,但是这样超时了。后来一想,如果直接知道m种操作每种操作的次数不就行了,于是我们需要两个数组,一个是用来记录m种操作它们需要进行的次数,另一个就是原来数组中的值。
详细看代码实现
代码实现
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
struct node{
ll l, r, x;
}op[maxn];
ll num[maxn], ca[maxn];//这个存储原数组值的差分
ll qujian[maxn]; //存储每种操作的次数
int n, m, k;
int main()
{
cin>>n>>m>>k;//注意这里一定要用cin和cout来进行输入输出,题目要求
for(int i=1; i<=n; i++)
{
cin>>num[i];
ca[i]=num[i]-num[i-1];
}
for(int i=1; i<=m; i++)
{
cin>>op[i].l>>op[i].r>>op[i].x;//把m中操作按照顺序存到结构体数组中
}
ll x, y;
for(int i=1; i<=k; i++)
{
cin>>x>>y;
qujian[x]++;
qujian[y+1]--;
}
ll tmp=0;
for(int i=1; i<=m; i++)
{
tmp+=qujian[i];
ca[op[i].l]+=tmp*op[i].x;
ca[op[i].r+1]-=tmp*op[i].x;
}
tmp=0;
for(int i=1; i<=n; i++)
{
tmp+=ca[i];
cout<<tmp<<" ";
}
cout<<endl;
return 0;
}
Greg and Array CodeForces 296C 差分数组的更多相关文章
- G - Greg and Array CodeForces - 296C 差分+线段树
题目大意:输入n,m,k.n个数,m个区间更新标记为1~m.n次操作,每次操作有两个数x,y表示执行第x~y个区间更新. 题解:通过差分来表示某个区间更新操作执行的次数.然后用线段树来更新区间. #i ...
- Codeforces Round #179 (Div. 1) A. Greg and Array 离线区间修改
A. Greg and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/295/pro ...
- Educational Codeforces Round 54 E. Vasya and a Tree(树上差分数组)
https://codeforces.com/contest/1076/problem/E 题意 给一棵树(n<=3e5),m(3e5)次查询,每次查询u,d,x,表示在u的子树中,给距离u&l ...
- CF296C Greg and Array 查分数组
题目链接:http://codeforces.com/problemset/problem/296/C 题意:给你n.m.k,表示n个数a[i],m个对数的操作,k个对操作的操作.m个操作:数a[l] ...
- Codeforces 295A Greg and Array
传送门 A. Greg and Array time limit per test 1.5 seconds memory limit per test 256 megabytes input stan ...
- Karen and Coffee CodeForces - 816B (差分数组+预处理前缀和)
To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...
- Codeforces Round #419 (Div. 2)(B)差分数组
传送门:Problem B https://www.cnblogs.com/violet-acmer/p/9721160.html 题意: Karen有n个关于煮咖啡的食谱,每个食谱都有个煮咖啡的最适 ...
- Codeforces Round #426 (Div. 2) B题【差分数组搞一搞】
B. The Festive Evening It's the end of July – the time when a festive evening is held at Jelly Castl ...
- [差分数组] LeetCode789 得分最高的最小轮调
LeetCode 得分最高的最小轮调 今天当然CV了因为今天比较忙,所以直接走算法,因为什么都不做的话并不符合社会主义核心价值观,今天小学一手查分数组. 题目:并不存在CV了还写什么题解 算法背景: ...
随机推荐
- axios中put和patch的区别(都是update , put是需要提交整个对象资源,patch是可以修改局部)
patch方法用来更新局部资源,这句话我们该如何理解? 假设我们有一个UserInfo,里面有userId, userName, userGender等10个字段.可你的编辑功能因为需求,在某个特别的 ...
- 【Heaven Cow与God Bull】题解
题目 Description __int64 ago,there's a heaven cow called sjy... A god bull named wzc fell in love with ...
- Vue 基于node npm & vue-cli & element UI创建vue单页应用
基于node npm & vue-cli & element UI创建vue单页应用 开发环境 Win 10 node-v10.15.3-x64.msi 下载地址: https ...
- Gparted for partition of Linux on graphic interface
You can change the partition table on Linux by a group of tools, which is tool comprehansive for a n ...
- PHP高手干货分享:不能不看的50个细节!【PHP培训教程】
兄弟连分享PHP高手干货:不能不看的50个细节 1.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以 ...
- makefile 打印
$(warning ----------$(abc)) $(info -----------------$(abc))
- Codeforces Round #351(Div 2)
熬到半夜打结果前一个小时提交代码的页面根本加载不进去,D题写完还因为小点没判被hack掉了... rating什么的都是身外之物了,于是乎喜闻乐见地又掉回绿名了. A题: *题目描述: Bear看一场 ...
- ubantu apt-get install安装php及开展
.安装php apt-get install libapache2-mod-php5 php5 报错 E: Package 'libapache2-mod-php5' has no installat ...
- Nginx动静分离-tomcat
一.动静分离 1.通过中间件将动态请求和静态请求分离. 2.为什么? 分离资源,减少不必要的请求消耗,减少请求延时. 3.场景 还可以利用php,fastcgi,python 等方式 处理动态请求 # ...
- GPSMap程序源代码
1. 界面 第一次打开时选择工程文件 MainActivity.java Tools.OpenDialog(new ICallback(){ public void OnClick(String pa ...