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]表示回文树上节 ...
随机推荐
- 线性素数筛(欧拉筛)(超级好的MuBan)
Problem:找出小于等于n的所有素数的个数. #include <bits/stdc++.h> using namespace std; const int maxn = 1e6; i ...
- 用python进行服务器的监控
用python进行服务器的监控 在linux服务器中,一切皆为文件,就是说,服务器运行的个中信息,其实是可以从某些文件中查询得到的:百度后,你会知道,在Linux系统中,有一个/proc的虚拟文件系统 ...
- 十七、mysql数据库备份
使用java代码在window环境下实现定时执行Mysql备份与还原 //备份 public void doBackup() { Date currentTime = new Date(); Syst ...
- $\LaTeX$数学公式大全13
$13\ Text\ Mode:\ Accents\ and\ Symbols$ $\AA$ \AA $\S$ \S
- Nginx一个server配置多个location
在配置文件中增加多个location,每个location对应一个项目 比如使用8066端口,location / 访问官网: location /demo访问培训管理系统配置多个站点我选择了配置多个 ...
- bilibili番剧评分爬虫
python选修课学习中练手写的,主要就是查询bilibili提供得api # -*- coding:utf-8 -*- import requests import json import csv ...
- vue 的多页面应用
vue-cli3 中构建多页面的应用 第一步:先创建一个 vue-cli3 的项目:vue create app 然后运行项目:npm run serve 现在开始多页面的应用: 首先在 src 的目 ...
- java用Annotation注入到成员Bean对象
java用Annotation注入到成员Bean对象 在使用一些java框架的时候,经常看到一些注解,而且使用注解之后就可以免去一些xml的繁琐配置,本文记录如何通过注解获得成员Bean对象. 一.首 ...
- [windows菜鸟]Windows API函数大全(完整)
Windows API函数大全,从事软件开发的朋友可以参考下 1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一 ...
- LC 413. Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...