***题目在这里(这是洛谷上的一道入门模板题 ):***https://www.luogu.org/problem/P3834
**代码在下面,解释都写在注释上了。然后我也是第一次接触这个知识,注释中有什么不妥的,欢迎大家帮我指正。
**
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+5;
int n,m,a[maxn];
vector<int> v;
inline int getid(int x)
{
return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
//利用lower_bound函数(下面有链接)来给每个数编号,注意是从1开始的。
// 这是离散化的部分组成
}
typedef struct Node//主席树结点的结构
{
int l,r,sum;
}no;
no hjt[maxn*40];
int cnt,root[maxn];
void insert(int l,int r,int pre,int &now,int p)//递归的方式来插入新结点
{
hjt[++cnt]=hjt[pre];
now=cnt;
hjt[now].sum++;
if(l==r) return;
int m=(l+r)>>1;
if(p<=m) insert(l,m,hjt[pre].l,hjt[now].l,p);
else insert(m+1,r,hjt[pre].r,hjt[now].r,p);
}
int