#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <long double, pii> muchie;
const ll NMAX = 1000001;
const ll VMAX = 1000001;
const ll INF = (1LL << 60);
const ll MOD = 1000000007;
const ll BLOCK = 447;
const ll nr_of_bits = 21;
ll s[NMAX];
int v[NMAX];
unordered_map <ll, ll> mp;
int aib[NMAX + 1];
void update(int node, int x){
for(; node <= NMAX; node += node&(-node))
aib[node] += x;
}
int query(int node){
int x = 0;
if(node > NMAX)
return 0;
for(; node > 0; node -= node&(-node))
x += aib[node];
return x;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, i;
cin >> n;
for(i = 1; i <= n; i++){
cin >> v[i];
}
int p;
cin >> p;
vector <ll> a;
for(i = 1; i <= n; i++){
v[i] = v[i] - p;
s[i] = s[i - 1] + 1LL * v[i];
a.push_back(s[i]);
}
a.push_back(0);
sort(a.begin(), a.end());
int cnt = 0;
for(int i = 0; i < a.size(); i++){
if(i == 0 || a[i] != a[i - 1])
cnt++;
mp[a[i]] = cnt;
}
ll sol = 0;
update(mp[0] + 1, 1);
for(i = 1; i <= n; i++){
s[i] = mp[s[i]] + 1; /// sa devina si 0 -> 1
sol += 1LL * query(s[i]);
update(s[i], 1);
}
cout << sol;
return 0;
}
Compilation message
vudu.cpp: In function 'int main()':
vudu.cpp:56:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int i = 0; i < a.size(); i++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
844 KB |
Output is correct |
2 |
Correct |
2 ms |
716 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
822 ms |
65536 KB |
Output is correct |
5 |
Correct |
408 ms |
35940 KB |
Output is correct |
6 |
Correct |
756 ms |
65536 KB |
Output is correct |
7 |
Correct |
775 ms |
65536 KB |
Output is correct |
8 |
Correct |
665 ms |
62012 KB |
Output is correct |
9 |
Runtime error |
662 ms |
65540 KB |
Execution killed with signal 9 |
10 |
Correct |
717 ms |
65536 KB |
Output is correct |