奶牛抗议

有点像 佳佳的手账 a[l]+a[l+1]+......+a[r]>=0

                              s[r]-s[l-1]>=0

                              s[l-1]<s[r]

                              求顺序对

                              用树状数组维护

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm> 
 4 using namespace std;
 5 const int maxn=1e5+7;
 6 const int mod=1000000009;
 7 int n;
 8 int ans;
 9 int a[maxn],tmp[maxn],c[maxn];
10 int s[maxn];
11 int lowbit(int x){return x&(-x);}
12 void add(int x,int y){
13     for(;x<=n;x+=lowbit(x)) {c[x]+=y;c[x]%=mod;}
14 }
15 int sum(int x){
16     int ret=0;
17     for(;x;x-=lowbit(x)) {ret+=c[x];ret%=mod;}
18     return ret%mod;
19 }
20 int main(){
21     cin>>n;
22     for(int i=1;i<=n;i++) {cin>>a[i];s[i]=s[i-1]+a[i];tmp[i]=s[i];}
23     sort(tmp+1,tmp+n+1);
24     for(int i=0;i<=n;i++){
25         s[i]=lower_bound(tmp+1,tmp+n+1,s[i])-tmp;
26     }
27     add(s[0],1);
28     for(int i=1;i<=n;i++){
29         ans=sum(s[i])%mod;
30         add(s[i],ans);
31     }
32     cout<<ans%mod<<endl;
33     return 0;
34 } 

 因为计算的是s[r]和s[l-1]所以要从s[0]开始计算,而s[0]是0要用离散化来处理

1.用f来计算每个s[i]的值,而最后答案是最后加入的s[i]对应的f值,我却记录f数组最后的值......

2.忘记了s[l-1]把s[1]设成1,应该是s[0]设成1

猜你喜欢

转载自www.cnblogs.com/lcan/p/9557974.html