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>
using namespace std;
#define ll long long
#define MOD 1000000007
ll tests = 1 , curr = 0;
const int N = 1000001;
int parent[N],Size[N];
int get_parent(int u){
return ((u != parent[u]) ? parent[u] = get_parent(parent[u]) : u);
}
void union__(int u,int v){
if((u = get_parent(u)) == (v = get_parent(v))){
return ;
}
if (Size[u] < Size[v]){
swap(u , v);
}
curr += Size[u] * 1ll * Size[v];
parent[v] = u;
Size[u] += Size[v];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n , q , z;
cin >> n >> q;
vector<pair<int,int>> tot;
for(int i = 1 ;i <= n ;i++){
cin >> z;
tot.push_back({z , -i});
}
for(int i = 1 ;i <= q ;i++){
cin >> z;
tot.push_back({z , i});
}
sort(tot.begin(),tot.end());
ll ans[q];
for(int i = 0 ;i < n + q ;i ++){
ll a = tot[i].first , b = tot[i].second;
if(b > 0){
ans[b] = curr;
}
else{
b = -b;
curr++;
parent[b] = b;
Size[b] = 1;
if(parent[b + 1]){
union__(b , b + 1);
}
if(parent[b - 1]){
union__(b , b - 1);
}
}
}
for(int i = 1; i<= q; i++){
cout << ans[i] << endl;
}
}
Compilation message (stderr)
pilot.cpp: In function 'int main()':
pilot.cpp:40:12: warning: unused variable 'a' [-Wunused-variable]
40 | ll a = tot[i].first , b = tot[i].second;
| ^
# | 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... |
# | 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... |