F. Greedy Sequence(主席树区间k的后继)(The Preliminary Contest for ICPC Asia Nanjing 2019)
题意:
查找区间k的后继。
思路:
直接主席树。
#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
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int 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;
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
//const long long INF=(1LL<<60);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e5+; int tot;
int root[N];//forest_root;
struct node
{
int l,r,sz;
}tr[N*];//Log(n) //=========================================
void Init()
{
tot=;
}
int Build(int l,int r)
{
int now=++tot;
tr[now].sz=;
int mid=(l+r)>>;
if(l<r)
{
tr[now].l=Build(l,mid);
tr[now].r=Build(mid+,r);
}
return now;
}
int update(int pre,int l,int r,int x)
{
int now=++tot;
tr[now].sz=tr[pre].sz+;
tr[now].l=tr[pre].l;
tr[now].r=tr[pre].r;
if(l<r)
{
int mid=(l+r)>>;
if(x>mid)
tr[now].r=update(tr[pre].r,mid+,r,x);
else
tr[now].l=update(tr[pre].l,l,mid,x);
}
return now;
}
int Query_min(int u,int v,int l,int r,int k)
{
if(tr[v].sz-tr[u].sz==)return ;
if(l==r)return l<k?l:;
int mid=(l+r)>>;
if(k<=mid+||tr[tr[v].r].sz-tr[tr[u].r].sz==)
return Query_min(tr[u].l,tr[v].l,l,mid,k);
int t=Query_min(tr[u].r,tr[v].r,mid+,r,k);
if(t)return t;
return Query_min(tr[u].l,tr[v].l,l,mid,k);
}
int Q(int u,int v,int l,int r,int k)
{
if(l==r)return tr[v].sz-tr[u].sz;
int mid=(l+r)>>;
if(k<=mid)
return Q(tr[u].l,tr[v].l,l,mid,k);
else
return Q(tr[u].r,tr[v].r,mid+,r,k);
}
void check(int u,int v,int n)
{
for(int i=;i<=n;++i)
pr("%d ",Q(u,v,,n,i));
pr("\n");
} int a[N];
int pos[N];
int ans[N];
int main()
{
int T;
sc("%d",&T);
while(T--)
{
int n,k;
sc("%d%d",&n,&k);
Init();
root[]=Build(,n);
for(int i=;i<=n;++i)
{
sc("%d",&a[i]);ans[i]=;pos[a[i]]=i;
root[i]=update(root[i-],,n,a[i]);
}
for(int i=;i<=n;++i)
{
int t1=-;
if(pos[i]!=)
{
// pr("%d %d\n",max(pos[i]-k-1,0),pos[i]-1);
// check(root[max(pos[i]-k-1,0)],root[pos[i]-1],n);
int tt=Query_min(root[max(pos[i]-k-,)],root[pos[i]-],,n,i);
ans[i]=ans[tt]+;
t1=tt;
}
if(pos[i]!=n)
{
// pr("%d %d\n",pos[i],min(pos[i]+k,n));
// check(root[pos[i]],root[min(pos[i]+k,n)],n);
int tt=Query_min(root[pos[i]],root[min(pos[i]+k,n)],,n,i);
if(tt>t1)
ans[i]=ans[tt]+;
}
pr("%d%c",ans[i],i==n?'\n':' ');
}
}
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;
}
F. Greedy Sequence(主席树区间k的后继)(The Preliminary Contest for ICPC Asia Nanjing 2019)的更多相关文章
- The Preliminary Contest for ICPC Asia Nanjing 2019( B H F)
B. super_log 题意:研究一下就是求幂塔函数 %m的值. 思路:扩展欧拉降幂. AC代码: #include<bits/stdc++.h> using namespace std ...
- 树状数组+二维前缀和(A.The beautiful values of the palace)--The Preliminary Contest for ICPC Asia Nanjing 2019
题意: 给你螺旋型的矩阵,告诉你那几个点有值,问你某一个矩阵区间的和是多少. 思路: 以后记住:二维前缀和sort+树状数组就行了!!!. #define IOS ios_base::sync_wit ...
- The Preliminary Contest for ICPC Asia Nanjing 2019 A The beautiful values of the palace(树状数组+思维)
Here is a square matrix of n * nn∗n, each lattice has its value (nn must be odd), and the center val ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- 计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛
XKC's basketball team XKC , the captain of the basketball team , is directing a train of nn team mem ...
- The Preliminary Contest for ICPC Asia Shanghai 2019 F. Rhyme scheme(dp)
题意:给你一个n和k 要你找到长度为n 字典序第k小的字符串 定义一个字符串合法:第i的字符的范围只能是前i-1个字符中的最大值+1 思路:我们dp[n][i][j]表示长度为n 在第i位 最大值为 ...
- The Preliminary Contest for ICPC Asia Shenyang 2019 F. Honk's pool
题目链接:https://nanti.jisuanke.com/t/41406 思路:如果k的天数足够大,那么所有水池一定会趋于两种情况: ① 所有水池都是一样的水位,即平均水位 ② 最高水位的水池和 ...
- 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)
query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树
签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...
随机推荐
- 'findstr' 不是内部或外部命令,也不是可运行的程序或批处理文件
今天通过windows cmd客户端输入: solr.cmd start ,启动solr时,提示"findstr' 不是内部或外部命令,也不是可运行的程序或批处理文件" 这是PAT ...
- font属性
font属性 font属性设置css字体
- DRL Hands-on book
代码:https://github.com/PacktPublishing/Deep-Reinforcement-Learning-Hands-On Chapter 1 What is Reinfor ...
- django xadmin安装
安装方式一: 下载xadmin源码文件,下载之后,解压缩,将解压目录中的xadmin文件夹拷贝到项目项目文件中.下载地址:https://codeload.github.com/sshwsfc/xad ...
- springboot加载bean过程探索
springboot一般通过以下main方法来启动项目 @SpringBootApplication public class DemoApplication { public static void ...
- springboot 2.2.0 SNAPSHOT 解决 repositories.repository.id must be unique 的问题
如果打包 jar 也报错了 ,那也是这个的原因,注释掉即可 <!-- 这个仓库必须注释掉 否则打包 war 的时候 , 会报错 'repositories.repository.id' must ...
- 【免费电子书】这可能是全网最齐的程序员编程电子书PDF合集了!
[toc] 最近博主
- Node.js自学完全总结
零.什么是Node.js? 引用Node.js官方网站的解释如下: Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript e ...
- Java-GC 垃圾收集器(HotSpot)
垃圾收集器为垃圾收集算法的具体实现,是执行垃圾收集算法的,是守护线程. HotSpot 虚拟机采用分代收集(JVM 规范并未对堆区进行划分),将堆分为年轻代和老年代,垃圾收集器也按照这样区分.不过已有 ...
- EDM数据营销之电商篇| 六大事务性邮件,环环相扣打造极致用户体验!
“以用户为中心”的时代,电商们致力于打造极致的用户体验,想尽各式新颖营销办法,但难免还是会出现营销断层,以至于和用户间无法达到完整的交互. 本次Focussend以邮件营销为例,聚焦用户从浏览到支付等 ...