思路:
线段树水过;
代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 40005 #define ll long long inline void in(int &now) { char Cget=getchar();now=0; while(Cget>‘9‘||Cget<‘0‘) Cget=getchar(); while(Cget>=‘0‘&&Cget<=‘9‘) { now=now*10+Cget-‘0‘; Cget=getchar(); } } inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>‘9‘&&Cget<‘0‘) Cget=getchar(); while(Cget>=‘0‘&&Cget<=‘9‘) { now=now*10+Cget-‘0‘; Cget=getchar(); } } struct TreeNodeType { int l,r,mid,dis; TreeNodeType *lc,*rc; TreeNodeType() {lc=NULL,rc=NULL,dis=0;} }; class NiXuDuiWorkPType { private: int ai[maxn],bi[maxn],n,size; ll ans; TreeNodeType *root; void build(TreeNodeType *&now,int l,int r) { now=new TreeNodeType,now->l=l,now->r=r; if(l==r) return ;now->mid=l+r>>1; build(now->lc,l,now->mid),build(now->rc,now->mid+1,r); } void updata(TreeNodeType *now,int to) { if(now->l==now->r) {now->dis++;return;} if(to<=now->mid) updata(now->lc,to);else updata(now->rc,to); now->dis=now->lc->dis+now->rc->dis; } void query(TreeNodeType *now,int l,int r) { if(now->l>=l&&now->r<=r) {ans+=now->dis;return;} if(l<=now->mid) query(now->lc,l,min(now->mid,r)); if(r>now->mid) query(now->rc,max(now->mid+1,l),r); } public: NiXuDuiWorkPType() { in(n);for(int i=1;i<=n;i++) in(ai[i]),bi[i]=ai[i]; sort(bi+1,bi+n+1),size=unique(bi+1,bi+n+1)-bi-1;build(root,1,size); for(int i=1;i<=n;i++) { ai[i]=lower_bound(bi+1,bi+size+1,ai[i])-bi; if(ai[i]<size) query(root,ai[i]+1,size);updata(root,ai[i]); } cout<<ans; } }; class NiXuDuiWorkPType pos; int main() { return 0; }
时间: 2024-10-22 22:57:49