线段树维护最后一个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序列。
思路:
首先我们要想到,最后一个0的位置一定就是当前剩余还没用确定的数里的最小值,所以从1,2,3 。。。一直下去就行了。
选过了就在线段树上改为INF,同时pushup维护的最右边为0的位置。
一开始单点修改忘记down下去了,改了好久,学到了学到了。
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
int abss(int a);
int lowbit(int n);
int Del_bit_1(int n);
int maxx(int a,int b);
int minn(int a,int b);
double fabss(double a);
void swapp(int &a,int &b);
clock_t __STRAT,__END;
double __TOTALTIME;
void _MS(){__STRAT=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef long long ll;
const double E=2.718281828;
const double PI=acos(-1.0);
const ll INF=(1LL<<);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)2e5+; ll a[N];
ll add[N<<],min_[N<<];
int POS[N<<]; void up(int rt,int l,int r)
{
min_[rt]=min(min_[rt<<],min_[rt<<|]);
if(min_[ls]<min_[rs]) POS[rt]=POS[ls];
else POS[rt]=POS[rs];
} void dn(int rt)
{
if(add[rt]!=)
{
add[ls]+=add[rt];
add[rs]+=add[rt];
min_[rt<<]+=add[rt];
min_[rt<<|]+=add[rt];
add[rt]=;
}
} void Build(int l,int r,int rt)
{
if(l==r)
{
min_[rt]=a[l];
POS[rt]=l;
add[rt]=;
return;
}
int mid=(l+r)>>; Build(l,mid,rt<<);
Build(mid+,r,rt<<|);
up(rt,l,r);
} void update_dot(int pos,ll V,int l,int r,int rt)
{
if(l==r)
{
min_[rt]=V;
return;
} int mid=(l+r)>>;
dn(rt);
if(pos<=mid)
update_dot(pos,V,l,mid,rt<<);
else
update_dot(pos,V,mid+,r,rt<<|);
up(rt,l,r);
}
void update_qu(int L,int R,int V,int l,int r,int rt)
{
if(L>R)return;
if(L<=l&&r<=R)
{
min_[rt]+=V;
add[rt]+=V;
return;
} int mid=(l+r)>>;
dn(rt);
if(L<=mid)
update_qu(L,R,V,l,mid,rt<<);
if(R>mid)
update_qu(L,R,V,mid+,r,rt<<|);
up(rt,l,r);
}
void check(int pos,int l,int r,int rt)
{
if(l==r)
{
cout<<min_[rt]<<' ';
return ;
}
int mid=(l+r)>>; dn(rt);
if(pos<=mid)
check(pos,l,mid,rt<<);
else
check(pos,mid+,r,rt<<|);
}
int ans[N];
int main()
{
// fill(min_,min_+N-3,INF);
int n;
sc("%d",&n);
for(int i=;i<=n;++i)
sc("%lld",&a[i]);
Build(,n,);
for(int i=;i<=n;++i)
{
int p=POS[];
// pr("%d: %d\n",i,p);
ans[p]=i;
update_dot(p,INF,,n,);
// for(int j=1;j<=n;++j)check(j,1,n,1);cout<<endl;
update_qu(p+,n,-i,,n,);
// for(int j=1;j<=n;++j)check(j,1,n,1);cout<<endl;
}
for(int i=;i<=n;++i)
pr("%d ",ans[i]);
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}
线段树维护最后一个0的位置(Restore Permutation)Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)的更多相关文章
- 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)D(树状数组)
//树状数组中数组的特性,有更巧妙的方法.//我们知道在树状数组中,对于数组tree[i],它所维护的区间为[i−lowbit(i)+1,i]//所以对于tree[2^i],它所维护的区间就为[1,2 ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)E(多重集维护)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long ans[1000007]; ...
- MemSQL Start[c]UP 2.0 - Round 1 F - Permutation 思维+线段树维护hash值
F - Permutation 思路:对于当前的值x, 只需要知道x + k, x - k这两个值是否出现在其左右两侧,又因为每个值只有一个, 所以可以转换成,x+k, x-k在到x所在位置的时候是否 ...
- 2016shenyang-1002-HDU5893-List wants to travel-树链剖分+线段树维护不同区间段个数
肯定先无脑树链剖分,然后线段树维护一段区间不同个数,再维护一个左右端点的费用. 线段树更新,pushDown,pushUp的时候要注意考虑链接位置的费用是否相同 还有就是树链剖分操作的时候,维护上一个 ...
- HDU3564 --- Another LIS (线段树维护最值问题)
Another LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces 834D The Bakery【dp+线段树维护+lazy】
D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...
- [Codeforces]817F. MEX Queries 离散化+线段树维护
[Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...
随机推荐
- poj1275
Cashier Employment POJ - 1275 A supermarket in Tehran is open 24 hours a day every day and needs a n ...
- python2.X与Python3.X区别
__future__模块 [回到目录] Python 3.x引入了一些与Python 2不兼容的关键字和特性,在Python 2中,可以通过内置的__future__模块导入这些新内容.如果你希望在P ...
- MySQL优化相关参数--先做个记录,以后可能用得到
innodb_io_capacity:可设置的磁盘IO性能参数,越高代表当前mysql的IO性能更好,可用做决策刷脏页速度的参数: innodb_flush_neighbors:刷脏页是否开启连坐机制 ...
- 2018-2019-2 20165330《网络对抗技术》Exp9 Web安全基础
目录 基础问题 实验目的 实验内容 实验步骤 实验总结与体会 实验目的 本实践的目标理解常用网络攻击技术的基本原理. 返回目录 实验内容 WebGoat准备工作 SQL注入攻击 命令注入(Comman ...
- ndarray的axis问题
始终记不住np中axis是对应到哪个,还没系统地去学习下 先暂记两个常用的结果 1.[:,np.newaxis] 与 [np.newaxis, :] 注:这是ndarray才有的分片方法(np重写了[ ...
- CORS和jsonp实现跨域请求
同源策略:所谓同源是指,域名,协议,端口相同,它是由Netscape提出的一个著名的安全策略,现在所有支持JavaScript 的浏览器都会使用这个策略.当浏览器同时打开两个tab页面(两个不同服务器 ...
- python 3.6闭包+循环获取出字典中所有的值并保存在list中
def list_test(): list1=[] def list_all_dict(a): #检测字段类型 if isinstance(a,dict): for x in range(len(a) ...
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-2.使用Mybatis注解开发视频列表增删改查
笔记 2.使用Mybatis注解开发视频列表增删改查 讲解:使用Mybatis3.x注解方式 增删改查实操, 控制台打印sql语句 1.控制台打印sql语句 ...
- Centos7.4 下cobbler安装及配置
1.背景介绍 作为运维,在公司经常遇到一些机械性重复工作要做,例如:为新机器装系统,一台两台机器装系统,可以用光盘.U盘等介质安装,1小时也完成了,但是如果有成百台的服务器还要用光盘.U盘去安装,就显 ...
- lua学习笔记1--基础语法
print("打印日志");--单行注释 --[[ 多行注释 --]] a = --变量的类型,是由变量储存的数据决定 数据类型: number:数值类型,可以存储整数和小数 bo ...