# 洛谷P1823 [COI2007] Patrik 音乐会的等待

https://www.luogu.org/problemnew/show/P1823

 1 #include<cstdio>
2 #include<algorithm>
3 #include<cstring>
4 #include<vector>
5 using namespace std;
6 #define fi first
7 #define se second
8 #define mp make_pair
9 #define pb push_back
10 typedef long long ll;
11 typedef unsigned long long ull;
12 ll n;
13 ll d[500100];
14 #define lowbit(x) ((x)&(-x))
16 {
17     for(;p<=n;p+=lowbit(p))
18         d[p]+=x;
19 }
20 ll sum(ll p)
21 {
22     ll an=0;
23     for(;p>0;p-=lowbit(p))
24         an+=d[p];
25     return an;
26 }
27 ll a[500100];
28 ll st[500010],tp;
29 ll pre[500010];
30 ll ans;
31 int main()
32 {
33     ll i;
34     scanf("%lld",&n);
35     for(i=1;i<=n;++i)
36         scanf("%lld",&a[i]);
37     for(i=n;i>=1;--i)
38     {
39         while(tp&&a[st[tp]]<a[i])    pre[st[tp--]]=i;
40         st[++tp]=i;
41     }
42     //for(i=1;i<=n;++i)
43     //    printf("1t%lld\n",pre[i]);
44     tp=0;
45     for(i=1;i<=n;++i)
46     {
47         ans+=sum(i-1);
48         if(pre[i])    ans-=sum(pre[i]-1);
49         while(tp&&a[st[tp]]<a[i])
50         {
52             --tp;
53         }
58 }