Luo Gu P1908 reverse for explanations

Daily questions day43 punch

Analysis

Because the size of the data, so we need to be discrete, create a new array to a place inside our initial sequence number in this position is the first of several still use a little trick here sort, about discretization the specific skills we look at the code inside

We are only a discrete array operation after like, every time they meet a number put him into the Fenwick tree to go inside

Double experience Luo Gu P1774 closest to God's people _NOI Guide 2010 increase (02)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define int long long
 6 #define rep(i,s,e) for(register int i=s;i<=e;++i)
 7 #define dwn(i,s,e) for(fegister int i=s;i>+e;--i)
 8 #define maxn 5000000+10
 9 using namespace std;
10 inline int read()
11 {
12     int x=0;
13     bool f=1;
14     char c=getchar();
15     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
16     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
17     if(f) return x;
18     return 0-x;
19 }
20 inline void write(int x)
21 {
22     if(x<0){putchar('-');x=-x;}
23     if(x>9)write(x/10);
24     putchar(x%10+'0');
25 }
26 int n,ans;
27 int a[maxn],re[maxn];
28 int tree[maxn];
29 inline int lowbit(int x)
30 {
31     return x&-x;
32 } 
33 inline void build(int x,int k)
34 {
35     for(int i=x;i<=n;i+=lowbit(i)) tree[i]+=k;
36 }
37 inline int ask(int x)
38 {
39     int res=0;
40     for(int i=x;i>=1;i-=lowbit(i)) res+=tree[i];
41     return res;
42 }
43 signed main()
44 {
45     n=read();
46     rep(i,1,n) a[i]=read(),re[i]=a[i];
47     sort(a+1,a+n+1);
48     int m=unique(a+1,a+n+1)-a-1;
49     rep(i,1,n)
50     {
51         int now=lower_bound(a+1,a+m+1,re[i])-a;
52         ans+=ask(m)-ask(now);
53         build(now,1);
54     }
55     write(ans);
56     return 0;
57 }

Please Gangster treatise(Anyway, I do not know what that means treatise)

Guess you like

Origin www.cnblogs.com/handsome-zyc/p/11862765.html