题解

尝试做一下,感觉是每次取一段前缀和,这样就相当于让我们证明在 \(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. sock skbuf 结构:

    /** * struct sock - network layer representation of sockets * @__sk_common: shared layout with inet_ ...

  2. JAVA SE——集合框架

    1.首先根据业务场景选择哪种集合类型. set(无序,并且不包含重复元素),list(有序,并且允许重复元素),map(key-value,)

  3. 在线动态修改ulimit

    前言 系统中有些地方会进行资源的限制,其中的一个就是open file的限制,操作系统默认限制的是1024,这个值可以通过各种方式修改,本篇主要讲的是如何在线修改,生产上是不可能随便重启进程的 实践 ...

  4. (一)廖师兄springboot微信点餐SQL建表脚本

      数据库设计 数据库表之间的关系 类目表(product_category) 商品表(product_info) 订单主表(order_master) 订单详情表(order_detail) 卖家信 ...

  5. spring boot和spring cloud版本选择

    SpringBoot版本介绍 官网链接:https://spring.io/projects/spring-boot#learn Spring Boot的版本以数字表示.例如:Spring Boot ...

  6. JPA、Hibernate、Spring-Data-Jpa的本质区别

    什么是JPA? 全称Java Persistence API,可以通过注解或者XML描述[对象-关系表]之间的映射关系,并将实体对象持久化到数据库中. 为我们提供了: 1)ORM映射元数据:JPA支持 ...

  7. NVM、NPM、Node.js的安装选择

    在安装和使用这三种工具时,我们有很多方式可以选择,这些方法各有优劣,每个人都有自己用起来比较习惯的配置,所以我在这里记录下自己比较习惯的一种安装方式与其他一些可能的选项. NVM.NPM.Node.j ...

  8. php在线预览pdf文件

    话不多说,直接上代码 <?php public function read_pdf($file) { if(strtolower(substr(strrchr($file,'.'),1)) != ...

  9. Word中如何调整MathType公式的间距

    作为一名理工科的学生,经常会面对一大堆公式,那么就要掌握在Word中编辑公式的技能,那么怎样才能在Word中编辑美观的公式呢?为了方便大家的使用,下面就详细介绍在Word中调整MathType公式间距 ...

  10. 合并2个数组为1个无重复元素的有序数组--Go对比Python

    Go实现: 1 package main 2 3 import ( 4 "fmt" 5 "sort" 6 ) 7 8 func main() { 9 var a ...