不会去重.jpg

P1637 三元上升子序列

map去重失败求大佬帮忙qwq ```cpp #include<bits/stdc++.h> #define N 100010 using namespace std; namespace program{ long long n,C[N],l[N],r[N],res=0,m=0; map<long long,long long>cnt; struct node{ long long h,num; }a[N]; inline long long lowbit(long long x){ return ((x)&(-x)); } template<class T> T read(){ T s=0; long long ch; while(!isdigit(ch=getchar())); do s=s*10+(ch^48); while(isdigit(ch=getchar())); return s; } inline bool cmp1(node x,node y){ return x.h<y.h; } inline bool cmp2(node x,node y){ return x.h>y.h; } inline void init(){ int x; n=read<long long>(); for(long long i=1;i<=n;i++){ x=read<long long>(); if(!cnt[x]){ m+=1; a[m].h=x; a[m].num=m; cnt[x]+=1; }else{ cnt[x]+=1; } } } inline void add1(long long pos,long long val){ while(pos<=m){ C[pos]+=val; pos+=lowbit(pos); } } inline long long query1(long long pos){ long long tot=0; while(pos>0){ tot+=C[pos]; pos-=lowbit(pos); } return tot; } inline void add2(long long pos,long long val){ while(pos>0){ C[pos]+=val; pos-=lowbit(pos); } } inline long long query2(long long pos){ long long tot=0; while(pos<=m){ tot+=C[pos]; pos+=lowbit(pos); } return tot; } inline void work(){ init(); sort(a+1,a+m+1,cmp1); for(long long i=1;i<=m;i++){ add1(a[i].num,cnt[a[i].h]); l[a[i].num]=query1(a[i].num-1); } memset(C,0,sizeof C); sort(a+1,a+m+1,cmp2); for(long long i=1;i<=m;i++){ add2(a[i].num,cnt[a[i].h]); r[a[i].num]=query2(a[i].num+1); } for(long long i=1;i<=m;i++){ res=res+l[i]*r[i]; } cout<<res<<'\n'; } } int main(){ program::work(); return 0; } ```
by 严佳楠 @ 2018-05-07 08:07:40


|