昨天晚上做完4题还有30分钟,感觉太晚了就没继续写,不过看了下E题感觉是一个线段树题目,今天中午看了看发现就是一个线段树上递归的询问问题,不过我之前没写过但是靠着日益强大的乱写能力竟然水出来了~~

E. Greedy Shopping

不难知道操作1并不改变原数组不升序的性质即非严格单调递减的性质永远存在。

操作一:在线段树上二分第一个小于y的数的位置pos,然后区间修改即可[pos→x][pos\to x][pos→x]
操作二:维护一个区间最小值和区间和,然后递归乱搞,由于每次能买则买先往左子树递归,然后记录一下左子树的花费,再往右子树递归这时候剩余的钱要减去左子树的花费,全局变量记录答案。

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=400010;
ll a[N];
int n,q;
struct node
{int l,r;ll sum,mn;ll lazy;
}tree[N*4];
void pushup(int u)
{tree[u].sum=tree[u<<1].sum+tree[u<<1|1].sum;tree[u].mn=min(tree[u<<1].mn,tree[u<<1|1].mn);
}
void pushdown(int u)
{if(!tree[u].lazy)return;tree[u<<1].sum=(tree[u<<1].r-tree[u<<1].l+1)*tree[u].lazy;tree[u<<1|1].sum=(tree[u<<1|1].r-tree[u<<1|1].l+1)*tree[u].lazy;tree[u<<1].mn=tree[u<<1|1].mn=tree[u].lazy;tree[u<<1].lazy=tree[u<<1|1].lazy=tree[u].lazy;tree[u].lazy=0;
}
void build(int u,int l,int r)
{tree[u]={l,r};if(l==r)  {tree[u].sum=tree[u].mn=a[l];return;}int mid=l+r>>1;build(u<<1,l,mid),build(u<<1|1,mid+1,r);pushup(u);
}
void modify(int u,int l,int r,ll val)
{if(tree[u].l>=l&&tree[u].r<=r){tree[u].lazy=tree[u].mn=val;tree[u].sum=(tree[u].r-tree[u].l+1)*val;return;}pushdown(u);int mid=tree[u].l+tree[u].r>>1;if(l<=mid) modify(u<<1,l,r,val);if(r>mid)  modify(u<<1|1,l,r,val);pushup(u);
}
int findmn(int u,ll val)
{if(tree[u].l==tree[u].r) return tree[u].l;pushdown(u);if(tree[u<<1].mn>=val) return findmn(u<<1|1,val);else return findmn(u<<1,val);
}
int ans;
int calc(int u,int l,int r,ll now)
{if(tree[u].r<l||tree[u].l>r||!now) return 0;if(tree[u].l>=l&&tree[u].r<=r){if(tree[u].sum<=now){ans+=tree[u].r-tree[u].l+1;return tree[u].sum;}}ll w=0;pushdown(u);int mid=tree[u].l+tree[u].r>>1;if(l<=mid&&tree[u<<1].mn<=now) w+=calc(u<<1,l,r,now);if(r>mid) w+=calc(u<<1|1,l,r,now-w);return w;
}
int main()
{IO;int T=1;//cin>>T;while(T--){cin>>n>>q;for(int i=1;i<=n;i++) cin>>a[i];build(1,1,n+1);while(q--){int op,x,y;cin>>op>>x>>y;if(op==1){int pos=findmn(1,y);if(pos<=x) modify(1,pos,x,y); }else{ans=0;calc(1,x,n,y);cout<<ans<<'\n';}}}return 0;
}

此代码必须在多开一倍空间,要不然calc函数越界?我也不知道为啥很迷
要加哟哦~

codeforces1440 E. Greedy Shopping相关推荐

  1. 线段树 ---- 线段树上区间二分 或者单点二分 codeforces C. Greedy Shopping

    题目大意 题目大意: 给你一个非增的区间现在你有两次操作 1 x y : 把[a1,...ax][a_1,...a_x][a1​,...ax​]里面的数对yyy取maxmaxmax 2 x y : 你 ...

  2. CodeForces - 1440E Greedy Shopping(线段树)

    题目链接:点击查看 题目大意:给出一个非严格递减的子序列,需要完成 m 次操作,分为下列两种类型: 1 x y:将区间 [ 1 , x ] 中的数进行 a[ i ] = max( a[ i ] , y ...

  3. Codeforces Round #684 (Div. 2)

    A - Buy the String 要么全变成1要么全变成0要么一个都不改变,三种情况取最小. #define IO ios::sync_with_stdio(false);cin.tie();co ...

  4. Greedy is Good

    作者:supernova 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=greedyAlg Joh ...

  5. Arithmetic_Thinking -- greedy algorithm

    贪心算法--就是一种寻找局部最优解的情况,然后整合成整体最优解的情况 简单的例子:买菜的找钱,现在有1元,5角,1角的硬币,要找给别人2元7角,现在是怎么才能以最少的硬币量找给别人,肯定是先来两个1元 ...

  6. 指南:如何运用谷歌Google Shopping和Product Listing Ads

    指南:如何运用谷歌Google Shopping和Product Listing Ads 如果你在网上卖东西给外国人,那么你就没有理由不使用Google Shopping. 图片来源:123rf.co ...

  7. 数据结构与算法(C++)– 贪婪算法(Greedy algorithm)

    贪婪算法(Greedy algorithm) 1.基础 定义:贪婪算法分阶段地工作,在每一阶段,选择在当前最好的决策,不考虑将来的后果.所以一般只能得到局部最优而不是全局最优. 贪婪算法: Dijks ...

  8. 【题意分析】1044 Shopping in Mars (25 分)【滑动窗口】

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Shopping in Mars is quite a different experience. The Mars people ...

  9. 职业高中计算机英语教案,职高英语shopping教学设计.doc

    职高英语shopping教学设计 PAGE PAGE 1 职高英语"shopping"的教学设计 深圳市沙井职业高级中学 李慧莉 一 教学设计理念: 1.教材简析:<商务英语 ...

最新文章

  1. 怎样处理重命名系列案例代码
  2. Linux sendmail 服务器
  3. [转]Open Data Protocol (OData) Basic Tutorial
  4. react学习(38)----react是什么
  5. kafka mysql事务_【干货】Kafka 事务特性分析
  6. 单机android游戏排行榜,安卓单机手机游戏推荐_十大必玩单机手机游戏
  7. AO*算法详解,附例子和算法详细步骤
  8. perl删除Windows下的图片缓存缩略图(Thumbs.db)
  9. JAVA九宫格拼图游戏怎么计时_九宫格拼图怎么拼 如何玩转九宫格拼图游戏
  10. QML入门教程(1): Qt Quick与QML介绍
  11. 李宏毅老师《机器学习》课程笔记-1深度学习简介
  12. 基于微信小程序 校园跑腿小程序毕业设计毕设开题报告参考功能
  13. Blender烘焙光照贴图
  14. IK分词器原理研磨及源码改造,更适用生产
  15. 群晖服务器显示灯,【群晖 DS119j 单盘位NAS 网络存储服务器使用总结】功能|配置|机身|指示灯_摘要频道_什么值得买...
  16. 清橙OJ 1082 查找第K小元素 -- 快速排序
  17. 计算机网络学习笔记(持续更新)
  18. asp.net 实现word在线阅读
  19. 剑指 Offer II 076. 数组中的第 k 大的数字
  20. 任务管理系统算法-Kahn’s algorithm for Topological Sorting(一)

热门文章

  1. linux mysql帮助文档,在 Linux 上安装 MySQL
  2. java反射sethaha_Java反射深度测试
  3. python爬取网页数据软件_python爬虫入门10分钟爬取一个网站
  4. linux如何使用vnc远程登录,如何使用Xmanager及VNC登录远程桌面
  5. Pandas中的 transform() 结合 groupby() 用法示例
  6. 紧跟月影大佬的步伐,一起来学习如何写好JS(上)
  7. moore 数据集_警报数据集(alarm dataset)_机器学习_科研数据集
  8. [Java基础]Stream流终结操作之forEachcount
  9. [Java基础]Math类的常用方法
  10. 根据后序和中序求二叉树的先序