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 ...
随机推荐
- Sql性能优化梳理
前言 先简单梳理下Mysql的基本概念,然后分创建时和查询时这两个阶段的优化展开. 1.0 基本概念简述 1.1 逻辑架构 第一层:客户端通过连接服务,将要执行的sql指令传输过来 第二层:服务器解析 ...
- 日期格式在ios中的兼容性
在IOS中支持 2017/3/2 这种格式的日期 不支持2017-3-2日期 /** * 返回兼容ios.android的日期时间格式 * @param dateTime String * @retu ...
- 介绍Mobility Group
Mobility或Roaming是无线客户端能够安全地从一个AP无缝关联到另一个AP的能力,并且延迟尽可能的短. 当无线客户端和AP关联并通过AP进行身份验证时,注册AP的WLC会将客户端条目放在自己 ...
- jQuery中$.fn
$.fn是指jquery的命名空间,加上fn上的方法及属性,会对jquery实例每一个有效. 如扩展$.fn.abc(),即$.fn.abc()是对jquery扩展了一个abc方法,那么后面你的每一个 ...
- php中判断shell_exec执行结果
$shell = "wget -O despath sourcepath && echo 'success' "; $shellExec = shell_exec( ...
- 命令行选项解析函数getopt()
1.定义: int getopt(int argc, char * const argv[], const char *optstring); 2.描述: getopt是用来解析命令行选项参数的,但是 ...
- 一个基础的问题 多个$(function(){})里面的函数 为什么在下一个$(function(){})里没法执行。
先看下例子 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...
- CSS3实现魔方动画
本文将借助css3实现魔方动画效果,设计思路如下: HTML方面采用六个div容器形成六个立方面: CSS方面采用transform-style: preserve-3d;形成三维场景:transfo ...
- 02-Docker认识与原理
目录 02-Docker认识与原理 参考 Docker认识 Docker特性 Docker对比VM Docker社区版本 Docker原理 Docker engine Docker architect ...
- Unity2018编辑器脚本趟坑记录
解除预制体问题:(这个例子是解除游戏中的Canvas与Asset中的预制体的关系) if( PrefabUtility.IsAnyPrefabInstanceRoot(GameObject.Find( ...