Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)D(树状数组)
//树状数组中数组的特性,有更巧妙的方法。
//我们知道在树状数组中,对于数组tree[i],它所维护的区间为[i−lowbit(i)+1,i]
//所以对于tree[2^i],它所维护的区间就为[1,2^i]。
//所以就可以利用此特性加上树状数组的操作,维护一个类似倍增方法,并且支持在线修改操作。
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
long long a[200007],tree[200007],ans[200007];
long long lowbit(long long x){//找到最低位的1
return x&(-x);
}
void update(long long x,long long val){//更新前缀和
for(long long i=x;i<=200007;i+=lowbit(i))
tree[i]+=val;
}
/*
long long query(int x){//树状数组求前缀和
long long ans=0;
for(long long i=x;i;i-=lowbit(x))
ans+=tree[i];
return ans;
}
*/
long long solve(long long k,long long n){//找到前缀和(sum1~x)为k的x
long long num=0,sum=0;
for(long long i=21;i>=0;--i){
if(num+(1<<i)<=n&&sum+tree[num+(1<<i)]<=k){
num+=1<<i;
sum+=tree[num];
}
}
return num+1;//返回x+1
}
int main(){
long long n;
cin>>n;
for(long long i=1;i<=n;++i){
update(i,i);//预处理前缀和
cin>>a[i];
}
for(long long i=n;i;--i){//从后向前扫
ans[i]=solve(a[i],n);//找到1+2+3+...+x==a[i],ans[i]=x+1
update(ans[i],-ans[i]);//将比ans[i]大的数的前缀和中都减去ans[i],因为ans[i]这个数字已经在它们的后面了
}
for(long long i=1;i<=n;++i)
cout<<ans[i]<<" ";
return 0;
}
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)D(树状数组)的更多相关文章
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组 [Pro ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-C. Magic Grid-构造 [Problem Descripti ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-E. Let Them Slide-思维+数据结构
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-E. Let Them Slide-思维+数据结构 [Problem ...
- Codeforces Round #381 (Div. 2) D dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) F. Bits And Pieces sosdp
F. Bits And Pieces 题面 You are given an array
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) G. Polygons 数论
G. Polygons Description You are given two integers
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) (1208F,1208G,1208H)
1208 F 大意: 给定序列$a$, 求$\text{$a_i$|$a_j$&$a_k$}(i<j<k)$的最大值 枚举$i$, 从高位到低位贪心, 那么问题就转化为给定$x$ ...
- RMQ+差分处理(Let Them Slide)Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)
题意:https://codeforc.es/contest/1208/problem/E 现有n行w列的墙,每行有一排连续方块,一排方块可以左右连续滑动,且每个方块都有一个价值,第i 列的价值定义为 ...
- 线段树维护最后一个0的位置(Restore Permutation)Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)
题意:https://codeforc.es/contest/1208/problem/D 给你长度为n的序列,s[i]的值为p[1]到p[i-1]中比p[i]小的数的和,让你求出p序列. 思路: 首 ...
- Codeforces Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)
传送门 A. XORinacci 手玩三四项发现序列就是 $a,b,a\ xor\ b,a,b,...$,直接输出即可 #include<iostream> #include<cst ...
随机推荐
- tomcat6w.exe启动tomcat
在使用tomcat中,我们可能经常点击startup.bat来启动tomcat,但也不少通过tomcat6w.exe来启动的. 但是当我们点击tomcat6w.exe的时候会报错,信息如下:提示 指定 ...
- idea2019.2激活至2089年!
上图! 激活到2089年8月,绝对够用! 注意:在激活之前,无需改动 host 文件. 资料自取:链接:https://pan.baidu.com/s/1MzX5ewt6lbzHYuggP5sGE ...
- git密码相关问题
一.解决:每次都需要输入账号密码 git config --global credential.helper store 二.后期git密码更改后,重置密码操作 git config --system ...
- 创建Maven project 提示pom.xml 首行错误
背景 使用eclipse创建Maven SpringBoot 2.2.0 项目时报错,更换springboot 版本也不行,排除框架依赖原因.然后别人的eclipse创建的同样2.2.2 maven项 ...
- java知识树
https://blog.csdn.net/aitaozi11/article/details/79652943 (学习Java的9张思维导图) 文章目录 针对技术栈学习 1. java基础 1.1 ...
- 史上最全CentOS6离线安装部署Cloudera Manager5.9.3
史上最全CentOS6离线安装部署Cloudera Manager5.9.3
- nginx 解决 connect() failed (111: Connection refused) while connecting to upstream,
嗯哼,刚装了个ubuntu的lnmp,我的天啊,踩的坑比我脂肪还多了 比如刚装完的时候访问显示502, 也不知道什么问题,就去看了一下nginx日志 /var/log/nginx/error.log ...
- go get下载包失败问题
文章引用自 解决go get下载包失败问题 从github克隆 golang在github上建立了一个镜像库,如https://github.com/golang/net就对应是 https://go ...
- Go标准库之Log
文章引用自 Go语言标准库log介绍 无论是软件开发的调试阶段还是软件上线之后的运行阶段,日志一直都是非常重要的一个环节,我们也应该养成在程序中记录日志的好习惯. log Go语言内置的log包实 ...
- ARM相关概念(学习目标、分类、商业模式及半导体公司、嵌入式处理器)
1.学习ARM的目标 (1)对比X86,8051汇编,从更底层的角度去理解相关知识 (2)为后续嵌入式课程做准备 (3)了解ARM的体系结构,能够看懂ARM汇编 2.ARM分类 (1)按照版本号分类: ...