#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mxN = 1e6 + 5;
struct DATA
{
int idx;
ll val;
};
int n;
ll a[mxN];
DATA b[mxN];
ll p;
int fw[mxN];
vector<DATA> tmp;
int mp[mxN];
bool cmp(DATA x , DATA y){
if(x.val == y.val) return x.idx < y.idx;
return x.val < y.val;
}
void update(int idx , int val){
for(int i=idx;i<mxN;i += (i & -i)) fw[i] += val;
}
ll query(int idx){
ll res = 0;
for(int i=idx;i>0;i -= (i & -i)) res += fw[i];
return res;
}
int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin>> n;
for(int i=1;i<=n;i++) cin>> a[i] , a[i] += a[i-1];
// for(int i=1;i<=n;i++) cout<< a[i] << " ";
// cout<< "\n";
cin>> p;
tmp.push_back({0,0});
for(int i=1;i<=n;i++) b[i].val = a[i] - p*i , b[i].idx = i , tmp.push_back(b[i]);
// for(int i=1;i<=n;i++) cout<< b[i] << " ";
// cout<< "\n\n";
sort(tmp.begin() , tmp.end() , cmp);
// for(auto elem : tmp) cout<< elem.idx << " " << elem.val << " , ";
// cout<< "\n";
int id = 1;
for(auto elem : tmp){
mp[elem.idx] = id;
id++;
}
ll ans = 0;
update(mp[0] , 1);
for(int i=1;i<=n;i++){
int cnt = query(mp[b[i].idx]);
// cout<< "\ncnt = " << cnt << " ";
// cout<< cnt << "\n\n";
ans += cnt;
update(mp[i] , 1);
}
cout<< ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8916 KB |
Output is correct |
3 |
Correct |
3 ms |
8792 KB |
Output is correct |
4 |
Correct |
220 ms |
46640 KB |
Output is correct |
5 |
Correct |
123 ms |
36676 KB |
Output is correct |
6 |
Correct |
190 ms |
45548 KB |
Output is correct |
7 |
Correct |
190 ms |
46512 KB |
Output is correct |
8 |
Correct |
163 ms |
42928 KB |
Output is correct |
9 |
Correct |
225 ms |
47648 KB |
Output is correct |
10 |
Correct |
185 ms |
45092 KB |
Output is correct |