https://ac.nowcoder.com/acm/contest/911/F
值域上维护右括号的个数,遇到左括号就查询前面有几个右括号
#include <iostream> #include <algorithm> #include <queue> using namespace std ; const int N =1e5+10; int n,c[N]; int L[N],R[N] ; int lowbit(int x){ return x&-x; } int qry(int x){ int res=0; for(;x;x-=lowbit(x)) res+=c[x]; return res; } void add(int x){ for(;x<=1e5;x+=lowbit(x)) c[x]++; } signed main(){ cin>>n; for(int i=1;i<=n;i++){ double x,r; cin>>x>>r; L[i] =x-r; R[i]=x+r; } sort(L+1,L+1+n);sort(R+1,R+1+n) ; int ans=0; for(int i=1;i<=n;i++){ int l=L[i],r=R[i]; ans+= qry(L[i]-1); add(R[i]); } cout<<ans; }
标签:contest,int,res,nowcoder,include,911 From: https://www.cnblogs.com/towboa/p/17266761.html