树状数组求第k大

辣鸡南瓜并不能在考场上打出平衡树,就只能退而求其次,离线用树状数组来解决啦

板子题传送门:https://www.luogu.org/problemnew/show/P3369

code:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define maxn 105000
using namespace std;

int n,tot,sz;
int opt[maxn],val[maxn],num[maxn],c[maxn];

int read()
{
	int xx=0,kk=1;char ch=' ';
	while(!isdigit(ch)){ch=getchar();if(ch=='-')kk=-1;}
	while(isdigit(ch)){xx=xx*10+ch-'0';ch=getchar();}
	return kk*xx;
}

int pos(int x)
{
	return lower_bound(num+1,num+1+sz,x)-num;
}

int lowbit(int x)
{
	return x&-x;
}

void add(int x,int k)
{
	for(x;x<=sz;x+=lowbit(x)) c[x]+=k;
}

int query(int x)
{
	int tmp=0;
	for(x;x;x-=lowbit(x)) tmp+=c[x];
	return tmp;
}

int kth(int x)
{
	int rs=0;
	for(int i=17;i>=0;--i)
	{
		rs+=(1<sz||c[rs]>=x) rs-=(1<

 

你可能感兴趣的:(树状数组求第k大)