【HDOJ6609】Find the answer(线段树)
题意:给定一个n个正整数的数列,第i项为w[i],对于每个i,你要从[1,i-1]中选择一些变成0,使得变化后[1,i]的总和小于m,每次询问最少要变几个
n<=2e5,m<=1e9,1<=w[i]<=m
思路:显然每次贪心删最大的,直接开权值线段树,每次询问就在直接树上二分
开始交了几发TLE+MLE,是没有离散化的锅,把w[i]离散化就行
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
#define N 210000
#define M 1100000
#define fi first
#define se second
#define MP make_pair
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1 const ll MOD=,inv2=(MOD+)/;
double eps=1e-;
ll INF=1e14; struct node
{
int num;
ll sum;
}t[N<<]; struct arr
{
int x,id;
}a[N]; bool cmp(arr a,arr b)
{
return a.x<b.x;
} int b[N],c[N],q[N]; int cnt,root,n,m; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} void build(int l,int r,int p)
{
t[p].num=t[p].sum=;
if(l==r) return;
int mid=(l+r)>>;
build(l,mid,ls);
build(mid+,r,rs);
} int query(int l,int r,ll s,int p)
{
if(p==) return ;
if(l==r)
{
if(s<=m) return ;
ll t=(s-m)/c[l];
if((s-m)%c[l]) t++;
return t;
}
int mid=(l+r)>>;
ll tmp=t[rs].sum;
if(s-tmp<=m) return query(mid+,r,s,rs);
else
{
return t[rs].num+query(l,mid,s-tmp,ls);
}
} void update(int l,int r,int x,int p)
{
t[p].num++;
t[p].sum+=c[x];
if(l==r) return;
int mid=(l+r)>>;
if(x<=mid) update(l,mid,x,ls);
else update(mid+,r,x,rs);
} int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int cas=read();
cnt=;
while(cas--)
{
n=read(),m=read();
rep(i,,n)
{
a[i].x=read();
a[i].id=i;
}
rep(i,,n) q[i]=a[i].x;
sort(a+,a+n+,cmp);
b[a[].id]=; c[]=a[].x;
int t=;
rep(i,,n)
if(a[i].x==a[i-].x) b[a[i].id]=t;
else
{
t++;
b[a[i].id]=t;
c[t]=a[i].x;
}
build(,t,);
ll s=;
rep(i,,n)
{
s+=q[i];
printf("%d ",query(,t,s,));
update(,t,b[i],);
}
//printf("cnt=%d\n",cnt);
printf("\n");
}
return ;
}
【HDOJ6609】Find the answer(线段树)的更多相关文章
- 2019杭电多校第三场hdu6609 Find the answer(线段树)
Find the answer 题目传送门 解题思路 要想变0的个数最少,显然是优先把大的变成0.所以离散化,建立一颗权值线段树,维护区间和与区间元素数量,假设至少减去k才能满足条件,查询大于等于k的 ...
- [2019杭电多校第三场][hdu6609]Find the answer(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 大致题意是求出每个位置i最小需要将几个位置j变为0(j<i),使得$\sum_{j=1}^ ...
- hdu多校第三场 1007 (hdu6609) Find the answer 线段树
题意: 给定一组数,共n个,第i次把第i个数扔进来,要求你删掉前i-1个数中的一些(不许删掉刚加进来这个数),使得前i个数相加的和小于m.问你对于每个i,最少需要删掉几个数字. 题解: 肯定是优先删大 ...
- snnu(1110) 传输网络 (并查集+路径压缩+离线操作 || 线段树)
1110: 传输网络 Time Limit: 3 Sec Memory Limit: 512 MBSubmit: 43 Solved: 18[Submit][Status][Web Board] ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- SPOJ 1557. Can you answer these queries II 线段树
Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...
- bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树
2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 145 ...
- spoj gss2 : Can you answer these queries II 离线&&线段树
1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
随机推荐
- React Hooks简单业务场景实战(非源码解读)
前言 React Hooks 是React 16.7.0-alpha 版本推出的新特性.从 16.8.0 开始,React更稳定的支持了这一新特性. 它可以让你在不编写 class 的情况下使用 st ...
- Looper,Handler, MessageQueue
Looper Looper是线程用来运行消息循环(message loop)的类.默认情况下,线程并没有与之关联的Looper,可以通过在线程中调用Looper.prepare() 方法来获取,并通过 ...
- MongoDB分片配置 优化 不错
简单注解:mongos 路由进程, 应用程序接入mongos再查询到具体分片,监听端口默认27017config server 路由表服务, 每一台都具有全部chunk的路由信息 shard为数据存储 ...
- Java IO(3)
字符流相关 字符流基本上可以类比字节流 只不过是将字节流的byte 换为char. 最根本的两个类是Reader以及Writer Reader的子类有:BufferedReader, CharArra ...
- 《STL源码剖析》——第一、二、三章
第一章:概论: 换句话说,STL所实现的,是依据泛型思维架设起来的一个概念结构.这个以抽象概念(abstract concepts)为主体而非以实际类(classes)为主体的结构,形成了一个严谨的 ...
- python的包
1. 无论是import形式还是from...import形式,凡是在导入语句中(而不是在使用时)遇到带点的,都要第一时间提高警觉:这是关于包才有的导入语法 2. 包是目录级的(文件夹级),文件夹是用 ...
- 《JAVA设计模式》之策略模式(Strategy)
在阎宏博士的<JAVA与模式>一书中开头是这样描述策略(Strategy)模式的: 策略模式属于对象的行为模式.其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它 ...
- 《JAVA设计模式》之代理模式(Proxy)
在阎宏博士的<JAVA与模式>一书中开头是这样描述代理(Proxy)模式的: 代理模式是对象的结构模式.代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用. 代理模式的结 ...
- IDEA 中常用插件
怎么找到安装插件位置 Mybatis plugin 可以在mapper接口中和mapper的xml文件中来回跳转,就想接口跳到实现类那样简单. Lombok plugin 开发神器,可以简化你的实体类 ...
- kmp(最长前缀与后缀)
http://acm.hdu.edu.cn/showproblem.php?pid=1358 Period Problem Description For each prefix of a given ...