题目链接
https://www.acwing.com/problem/content/841/
思路纯纯模拟堆的操作,后面细说,可以先看代码
代码#include
using namespace std;
//----------------自定义部分----------------
#define ll long long
#define mod 1000000009
#define endl "\n"
#define PII pair
int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
ll ksm(ll a,ll b) {
ll ans = 1;
for(;b;b>>=1LL) {
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
ll lowbit(ll x){return -x & x;}
const int N = 2e6+10;
//----------------自定义部分----------------
int n,m,cnt,h[N],hp[N],ph[N];
//ph记录的是第i次存入的下标
//hp记录的是下标为i是第几次存入的
//ph和hp是一个互逆的数组
void heap_swap(int a,int b){//a、b都是下标
swap(ph[hp[a]],ph[hp[b]]);
swap(hp[a],hp[b]);
swap(h[a],h[b]);
}
void down(int u){//向下递归更新
int t = u;
if(u * 2 > 1);
u >>= 1;
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
string op;
cin>>n;
int k,x,kk=0;
for(int i = 1;i >op;
if(op == "I"){//在堆中插入元素
cin>>k;
cnt++;
kk++;//表示是第kk次插入的
h[cnt] = k;
ph[kk] = cnt,hp[cnt] = kk;
up(cnt);
}
else if(op == "PM"){//第一个元素就是最小的
coutk>>x;//更改第k次插入的数
k = ph[k];//还是先找到第k个插入的下标
h[k] = x;//然后将改下标的值更改为x
down(k),up(k);//这个更改的值要么大于等于原来的h[k],要么小于等于,所以我们直接down和up就好了
}
}
return 0;
}