#include<iostream>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
struct node
{
int v,id;
node(int a,int b){v=a;id=b;}
bool operator >(node b) const
{return v==b.v?id>b.id:v>b.v;}
};
tree<node,null_type,greater<node>,rb_tree_tag,tree_order_statistics_node_update> T,TE;
int main()
{
int n,m,k,s=0,ans=0;
char c;
scanf("%d%d",&n,&m);
while(n--)
{
cin>>c;
scanf("%d",&k);
if(c=='I')
{
k+=s;
if(k>=m) T.insert(node(k,n));
}
else if(c=='A') m-=k,s-=k;
else if(c=='S')
{
m+=k,s+=k;
T.split(node(m,-1),TE);
ans+=TE.size();
}
else if(c=='F')
{
if(k>T.size()) cout<<-1<<endl;
else cout<<T.find_by_order(k-1)->v-s<<endl;
}
}
printf("%d\n",ans);
return 0;
}
为什么while循环可以
#include<iostream>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
struct node
{
int v,id;
node(int a,int b){v=a;id=b;}
bool operator >(node b) const
{return v==b.v?id>b.id:v>b.v;}
};
tree<node,null_type,greater<node>,rb_tree_tag,tree_order_statistics_node_update> T,TE;
int main()
{
int n,m,k,s=0,ans=0;
char c;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
cin>>c;
scanf("%d",&k);
if(c=='I')
{
k+=s;
if(k>=m) T.insert(node(k,n));
}
else if(c=='A') m-=k,s-=k;
else if(c=='S')
{
m+=k,s+=k;
T.split(node(m,-1),TE);
ans+=TE.size();
}
else if(c=='F')
{
if(k>T.size()) cout<<-1<<endl;
else cout<<T.find_by_order(k-1)->v-s<<endl;
}
}
printf("%d\n",ans);
return 0;
}
但for循环不行,难绷