【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 ...
随机推荐
- Not a Number (NaN)
NaN can be produced by: 1. 0/0 2. Inf - Inf 3. Inf/Inf 4. 0*Inf 5. rem(x,y), where y=0 or x=Inf
- windows下使用pycharm开发基于ansible api的python程序
Window下python安装ansible,基于ansible api开发python程序 在windows下使用pycharm开发基于ansible api的python程序时,发现ansible ...
- [转]Hook executed successfully but returned HTTP 403
原文地址:https://www.cnblogs.com/chenglc/p/11174530.html jenkins配置gitlab的webhook,完成配置,测试结果显示 Hook execut ...
- delphi中的idhttpserver如何才能收到idhttp发送来的exe\rar文件呢
http://zhidao.baidu.com/link?url=-q2oXqYCKBZ9OgFDEHAcQwQEY_NroHcqGvVfKW67X5sF9LdjAAB_HPXQo04VxStFVS7 ...
- poj3614Sunscreen
Description To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her ...
- RedHat可用的几处软件源
rpmforge仓库 http://repoforge.org/use/ http://rpms.famillecollet.com/
- [CF960G]Bandit Blues(第一类斯特林数+分治卷积)
Solution: 先考虑前缀,设 \(f(i, j)\) 为长度为 \(i\) 的排列中满足前缀最大值为自己的数有 \(j\) 个的排列数. 假设新加一个数 \(i+1\) 那么会有: \[ f ...
- android概念-android学习第二天
一:1G到4G(generation) 1G 大哥大 -- 电话 2G 小灵通 gsm标准 发短信 wap.baidu.com -- 电话和短信 3G 沃 www.baidu.com 7.2M/s - ...
- go 上下文context
go控制并发有两种经典的方式,一种是WaitGroup,另外一种就是Context WaitGroup这种方式是控制多个goroutine同时完成 func main() { var wg sync. ...
- sqlite查询语句
搜索距现在六个月前的月份第一天日期: SELECT date('now','start of month','-6 month','0 day'); 搜索距现在六个月前的日期: SELECT date ...