CF1439C Greedy Shopping
题解
尝试做一下,感觉是每次取一段前缀和,这样就相当于让我们证明在 \(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的更多相关文章
- Greedy is Good
作者:supernova 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=greedyAlg Joh ...
- USACO . Greedy Gift Givers
Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...
- Shopping(山东省第一届ACM省赛)
Shopping Time Limit: 1000MS Memory limit: 65536K 题目描述 Saya and Kudo go shopping together.You can ass ...
- hdu4976 A simple greedy problem. (贪心+DP)
http://acm.hdu.edu.cn/showproblem.php?pid=4976 2014 Multi-University Training Contest 10 1006 A simp ...
- ACM Greedy Mouse
Greedy Mouse 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 A fat mouse prepared M pounds of cat food,read ...
- 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 ...
- sdutoj 2154 Shopping
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2154 Shopping Time Limit: ...
- Shopping(SPFA+DFS HDU3768)
Shopping Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- eclipse中 起动tomcat时报Multiple Contexts have a path of "/shopping"
eclipse中 启动tomcat时报Multiple Contexts have a path of "/shopping". 这个是由于你的server服务器中的server. ...
随机推荐
- ubutun下安装phantomjs配置chromedriver
我们在进行动态html页面获取时候,需要用到selenium,phantomjs,chromedriver selenium可以通过命令python的pip进行安装, phantomjs与chrome ...
- conda / cuda / screen 常用命令总结
记录一些常用的 conda / cuda / screen 相关的命令,这些在跑深度学习代码时经常用到. conda 下面的命令在 Ubuntu 下的 4.8.3 版本 conda 均正确工作. 查看 ...
- JVM中的常量池详解
在Java的内存分配中,总共3种常量池: 转发链接:https://blog.csdn.net/zm13007310400/article/details/77534349 1.字符串常量池(Stri ...
- ISITDTU CTF 2020 部分Web题目Writeup
周末,跟着m3w师傅打ISITDTUCTF,m3w师傅带弟弟上分,Tql! Web1 给了源码: <?php class Read{ public $flag; public function ...
- 工作三年终于社招进字节跳动!字节跳动,阿里,腾讯Java岗面试经验汇总
前言 我大概我是从去年12月份开始看书学习,到今年的6月份,一直学到看大家的面经基本上百分之90以上都会,我就在5月份开始投简历,边面试边补充基础知识等.也是有些辛苦.终于是在前不久拿到了字节跳动的o ...
- 自学linux——8.firewall-cmd的命令使用
firewall-cmd使用方法 1.firewall-cmd的含义 firewall-cmd 是 firewalld的字符界面管理工具,firewalld是centos7之后版本的一大特性, 最大的 ...
- iPhone/iOS开启个人热点的相关位置调整小结
冬至已到,圣诞将近,最近公司项目实在太多,三四个项目反复的切换真的让人焦头烂额,趁今天有点空,把维护的三个项目顺利送出,刚好可以缕缕思路,记录一下最近遇到的问题.说不着急那是假的,客户一天天的催的确实 ...
- Mockito 结合 Springboot 进行应用测试
Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试:Spring Boot可以跟BDD(Behavier Driven ...
- java实验作业类的定义与描述
1 //1三角形的定义与描述 2 package test; 3 4 public class sjx { 5 private double a,b,c; 6 7 public sjx(double ...
- 数据库default null字段用基本类型映射,改成包装类型后缓存中旧数据反序列化失败
rt,spring Temp不知道用的什么反序列化,int不能反序列化为Integer,后实验hissing是可以的int->Integer Integer(不为null)->int均可