题解

尝试做一下,感觉是每次取一段前缀和,这样就相当于让我们证明在 \(a_i\le 10^{12}\) 时,不可能构造出隔一个取一个的情况(\(n=10^5\))。

a[i]:  1, 2, 3, 5, 6,11,12,23,24...
s[i]: 1, 1, 4, 4,10,10,22,22,46...

可以发现他是成指数级增长的,所以必定不能构造出这样的数据,呕吼,好像可以做了。

我们就动态维护一下区间和,每次在这个东西上二分,按照我们上面的策略就可以了吧。

好像还需要吉老师线段树,骚啊~


好像不需要吉老师线段树了,各种线段树上二分才能保证复杂度不超过两只 \(\log_2\) 。。。感觉对于二分和线段树的理解更上一层楼了。

#include<bits/stdc++.h>
using namespace std;
#define Lint long long
const int N=2e5+5;
int n,m;Lint a[N];
struct Seg_Tree
{
struct Node{Lint data,tag,L,R;}tr[N<<2];
void up(int u)
{
tr[u].data=tr[u<<1].data+tr[u<<1|1].data;
tr[u].L=tr[u<<1|1].L,tr[u].R=tr[u<<1].R;
}
void update(int u,int l,int r,Lint z)
{
tr[u].data=(r-l+1)*z;
tr[u].L=tr[u].R=tr[u].tag=z;
}
void down(int u,int l,int r)
{
if(!tr[u].tag) return ;
int mid=(l+r)>>1;
update(u<<1,l,mid,tr[u].tag);
update(u<<1|1,mid+1,r,tr[u].tag);
tr[u].tag=0;
}
void build(int u,int l,int r,Lint a[])
{
if(l==r) return (void)(tr[u].data=tr[u].L=tr[u].R=a[l]);
int mid=(l+r)>>1;
build(u<<1,l,mid,a);
build(u<<1|1,mid+1,r,a);
up(u);
}
void chg(int u,int l,int r,int x,int y,Lint z)
{
if(x>y) return ;
if(x<=l&&r<=y) return update(u,l,r,z);
down(u,l,r);
int mid=(l+r)>>1;
if(x<=mid) chg(u<<1,l,mid,x,y,z);
if(y>mid) chg(u<<1|1,mid+1,r,x,y,z);
up(u);
}
int find(int u,int l,int r,int x)
{
if(tr[u].L>x) return n+1;
if(l==r) return l;
down(u,l,r);
int mid=(l+r)>>1;
if(tr[u<<1].L<=x) return find(u<<1,l,mid,x);
else return find(u<<1|1,mid+1,r,x);
}
Lint sum(int u,int l,int r,int x,int y)
{
if(x>y) return 0;
if(x<=l&&r<=y) return tr[u].data;
down(u,l,r);
int mid=(l+r)>>1;Lint res=0;
if(x<=mid) res+=sum(u<<1,l,mid,x,y);
if(y>mid) res+=sum(u<<1|1,mid+1,r,x,y);
return res;
}
int query(int u,int l,int r,Lint k)
{
if(l==r) return l;
down(u,l,r);
int mid=(l+r)>>1;Lint tmp=tr[u<<1].data;
if(k<=tmp) return query(u<<1,l,mid,k);
else return query(u<<1|1,mid+1,r,k-tmp);
}
}t;
int main()
{
cin>>n>>m;
for(int i=1;i<=n;++i) scanf("%lld",&a[i]);
t.build(1,1,n,a);
while(m--)
{
int opt,x,y;
scanf("%d%d%d",&opt,&x,&y);
if(opt==1) t.chg(1,1,n,t.find(1,1,n,y),x,y);
else
{
int cnt=0;
while(x<=n)
{
int tmp=t.query(1,1,n,t.sum(1,1,n,1,x-1)+y);
if(t.sum(1,1,n,x,tmp)>y) tmp--;
// printf("%d %d %d\n",x,y,tmp);
cnt+=tmp-x+1,y-=t.sum(1,1,n,x,tmp),x=t.find(1,1,n,y);
if(x<=tmp) x=tmp+1;
}
printf("%d\n",cnt);
}
}
return 0;
}

CF1439C Greedy Shopping的更多相关文章

  1. Greedy is Good

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

  2. USACO . Greedy Gift Givers

    Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...

  3. Shopping(山东省第一届ACM省赛)

    Shopping Time Limit: 1000MS Memory limit: 65536K 题目描述 Saya and Kudo go shopping together.You can ass ...

  4. hdu4976 A simple greedy problem. (贪心+DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4976 2014 Multi-University Training Contest 10 1006 A simp ...

  5. ACM Greedy Mouse

    Greedy Mouse 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 A fat mouse prepared M pounds of cat food,read ...

  6. hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏

    huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...

  7. sdutoj 2154 Shopping

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2154 Shopping Time Limit: ...

  8. Shopping(SPFA+DFS HDU3768)

    Shopping Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  9. eclipse中 起动tomcat时报Multiple Contexts have a path of "/shopping"

    eclipse中 启动tomcat时报Multiple Contexts have a path of "/shopping". 这个是由于你的server服务器中的server. ...

随机推荐

  1. 《GNU_makefile》——第八章 内嵌函数

    函数可以带参数,函数的展开方式和变量展开一样,函数的返回结果替换调用函数的文本. 1.函数的调用 $(FUNCTION ARGUMENTS) 或者: ${FUNCTION ARGUMENTS} FUN ...

  2. git命令学习之clone指定分支代码

    今天要拉取一个项目,但是是一个指定分支,本来我以为直接git clone就行,但是发现好像不能,报错: Cloning into 'lecture'...fatal: unable to update ...

  3. TCP/IP模型简介和/etc/hosts文件说明

    软件=协议的实现. IP决定了主机的位置.端口号决定了进程的位置. 两台主机上的通讯实际是两台主机上两个具体进程的通讯. TCP/IP模型分四层: TCP/IP模型:应用层---传输层----网络层- ...

  4. centos6安装calamari

    安装操作系统 首先安装操作系统centos6,安装过程选择的是base server,这个不相同不要紧,出现缺少包的时候去iso找出来安装就可以了 calamari的简单介绍 首先简单的介绍下cala ...

  5. 贼厉害,手撸的 SpringBoot 缓存系统,性能杠杠的!

    一.通用缓存接口 二.本地缓存 三.分布式缓存 四.缓存"及时"过期问题 五.二级缓存 缓存是最直接有效提升系统性能的手段之一.个人认为用好用对缓存是优秀程序员的必备基本素质. 本 ...

  6. [LeetCode题解]21. 合并两个有序链表 | 递归

    解题思路 使用递归实现: 定义函数功能:合并两个有序链表,并返回链表的头 结束条件:两个链表其中一个为空,返回另一个链表 递推公式: l1.val < l2.val:l1.next = Merg ...

  7. IDEA 2019.3.3 + Pycharm 2020.2.1 安装包及破解步骤

    IDEA IDEA的破解流程就不用再说了,免费试用,添加VMOptions参数,选择破解jar的路径,重启IDEA. 下载地址:链接:https://pan.baidu.com/s/1aTRATVTL ...

  8. kali linux与虚拟机Vmware安装vmware tools(主机与虚拟机的文件拖拽)

    一.打开虚拟机任务栏"虚拟机"-----点击安装Vmware tools 二.回到开启的kali linux系统中,找到vmware tools CD文件夹,拖拽出文件中的压缩文件 ...

  9. 如何用CorelDRAW画箭头?

    CorelDRAW,简称为cdr,是一款专业的矢量绘图软件,在设计界也是常用的专业设计之一,在日常的设计工作中,我们常常需要绘制一些特殊的图形,比如箭头.很多对cdr不是特别熟练的小伙伴不知道如何用c ...

  10. word-流程图

    将 业务洽谈 签订合同 客户付费 发货 收获 信息反馈 制作成流程图: 插入-SmartArt-根据需要选择图形,如下图所示 点击红框选中部分,变成如下图所示,将要编辑的内容选中复制粘贴进去,一般最后 ...