This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
using namespace std;
#define F first
#define S second
typedef long long ll;
#define pii pair <int, int>
#define pll pair <ll, ll>
typedef long double ld;
const ll N = 3 * 1e5 + 10;
const ll mod = 1e9 + 7;
ll um(ll a, ll b){
return ((1LL * a * b) % mod + mod) % mod;
}
ll subr(ll a, ll b){
return ((1LL * a - b) % mod + mod) % mod;
}
ll add(ll a, ll b){
return (1LL * a + b) % mod;
}
ll binpow(ll x, ll step){
ll res = 1LL;
while(step){
if(step & 1) res = um(res, x);
x = um(x, x);
step /= 2;
}
return res;
}
int arr[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
ll n, q;
cin >> n >> q;
for(ll i = 1; i <= n; i++){
cin >> arr[i];
}
if(n <= 11){
for(int i = 0, l, r; i < q; i++){
cin >> l >> r;
vector <int> vec;
for(int j = l; j <= r; j++){
vec.pb(arr[j]);
}
sort(vec.begin(), vec.end());
ll ans = 0, cur = 0, cnt = 0;
for(int j = 0; j < (int)vec.size(); j++){
if(j == 0) cnt++;
else{
if(vec[j] != vec[j - 1]) cnt++;
}
cur += cnt;
}
for(int j = 0; j < (int)vec.size(); j++){
ans += cur;
cur--;
if((int)vec.size() - 1 > j){
if(vec[j + 1] != vec[j]) cur -= ((int)vec.size() - j - 1);
}
}
cout << ans << endl;
}
return 0;
}
sort(arr + 1, arr + n + 1);
ll ans = 0, cnt= 0, cur = 0;
for(ll i = 1; i <= n; i++){
if(arr[i] != arr[i - 1]) cnt++;
cur += cnt;
}
for(ll i = 1; i <= n; i++){
ans += cur;
cur--;
if(arr[i] != arr[i + 1]) cur -= (n - i);
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |