先自己想了一个沙茶贪心怒WA,看了题解以后发现只要维护一个单调栈就行了,说明自己还是太弱了…
AC code:
#include <cstdio>
#include <stack>
using namespace std;
const int N=50010;
const int M=500010;
int n,tmp,ans;
int y[N];
bool ins[M];
stack<int> stk;
int main(){
scanf("%d%d",&n,&tmp);
for(int i=1;i<=n;i++) scanf("%d%d",&tmp,&y[i]);
for(int i=1;i<=n;i++){
if(!ins[y[i]]&&y[i]) ans++;
while((!stk.empty())&&stk.top()>=y[i]){
ins[stk.top()]=0;
stk.pop();
}
ins[y[i]]=1;
stk.push(y[i]);
}
printf("%d\n",ans);
return 0;
}