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;
using ll = long long;
#define MAXN (1000005)
ll parent[MAXN];
ll SIZE[MAXN];
long long find_set(ll a){
if(parent[a] == a) return a;
parent[a] = find_set(parent[a]);
return parent[a];
}
bool same_set(ll x,ll y){
return find_set(x) == find_set(y);
}
void merge_set(ll x,ll y){
parent[find_set(x)] = find_set(y);
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
ll N,Q;
cin>>N>>Q;
ll sum = 0;
ll arr[N];
for(ll i = 0;i < N;i++){
cin>>arr[i];
}
pair<ll,ll> planes[Q];
for(ll i = 0;i < Q;i++){
cin>>planes[i].first;
planes[i].second = i;
}
ll prefix[N + 1];
ll cur = 0;
for(ll i = 0;i <= N;i++){
cur += i;
prefix[i] = cur;
}
deque<pair<ll,ll> > dq;
for(ll i = 0;i < N;i++){
dq.push_back(make_pair(arr[i],i));
}
ll ans[Q];
ll can[N];
memset(can,0,sizeof(can));
memset(ans,0,sizeof(ans));
for(ll i = 0;i < N;i++){
parent[i] = i;
}
for(ll i = 0;i < N;i++){
SIZE[i] = 1;
}
sort(planes,planes + Q);
sort(dq.begin(),dq.end());
queue<ll> q;
for(ll i = 0;i < Q;i++){
while(dq.size() != 0 && dq.front().first <= planes[i].first){
ll a = dq.front().first;
ll index = dq.front().second;
can[index] = i + 1;
q.push(index);
dq.pop_front();
if(N == 1){
}else if(index == 0){
ll b = 0;
ll c = 0;
if(can[index + 1] >= 1){
b = find_set(index + 1);
sum -= prefix[SIZE[b]];
c += SIZE[b];
merge_set(index,index + 1);
b = find_set(index + 1);
SIZE[b] = c + 1;
sum += prefix[SIZE[b]];
}else{
sum++;
}
}else if(index == N - 1){
ll b = 0;
ll c = 0;
if(can[index - 1] >= 1){
b = find_set(index - 1);
sum -= prefix[SIZE[b]];
c += SIZE[b];
merge_set(index,index - 1);
b = find_set(index - 1);
SIZE[b] = c + 1;
sum += prefix[SIZE[b]];
}else{
sum++;
}
}else{
ll b = 0;
ll c = 0;
if(can[index - 1] >= 1){
b = find_set(index - 1);
sum -= prefix[SIZE[b]];
c += SIZE[b];
}
if(can[index + 1] >= 1){
b = find_set(index + 1);
sum -= prefix[SIZE[b]];
c += SIZE[b];
}
if(can[index - 1] >= 1) merge_set(index,index - 1);
if(can[index + 1] >= 1) merge_set(index,index + 1);
b = find_set(index);
SIZE[b] = c + 1;
sum += prefix[SIZE[b]];
}
}
ans[planes[i].second] = sum;
}
for(ll i = 0;i < Q;i++){
cout<<ans[i]<<'\n';
}
}
Compilation message (stderr)
pilot.cpp: In function 'int main()':
pilot.cpp:57:7: warning: unused variable 'a' [-Wunused-variable]
57 | ll a = dq.front().first;
| ^
# | 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... |