这题没什么话说。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 200500 using namespace std; int n,k,p,pos[51][maxn],cnt[maxn],r[maxn],col[maxn],val[maxn],pre[maxn],regis=0; long long ans=0; int ask(int col,int poss) { int l=1,r=cnt[col],ans=0; while (l<=r) { int mid=(l+r)>>1; if (pos[col][mid]<=poss) {ans=mid;l=mid+1;} else r=mid-1; } return pos[col][ans]; } int main() { scanf("%d%d%d",&n,&k,&p); for (int i=1;i<=n;i++) scanf("%d%d",&col[i],&val[i]); for (int i=1;i<=n;i++) { pos[col[i]][++cnt[col[i]]]=i; r[i]=cnt[col[i]]; if (val[i]<=p) regis=i; pre[i]=regis; } for (int i=2;i<=n;i++) { if (r[i]==1) continue; if (pre[i]==i) ans+=r[i]-1; else ans+=r[ask(col[i],pre[i])]; } printf("%lld ",ans); return 0; }