이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct dsu{
vector<int>root;
vector<int>siz;
dsu(int n){
root=vector<int>(n);
iota(root.begin(),root.end(),0);
siz=vector<int>(n,1);
}
bool unite(int x, int y){
x=findRoot(x);
y=findRoot(y);
if(x==y)
return 0;
if(siz[x]<siz[y])
swap(x,y);
siz[x]+=siz[y];
root[y]=x;
return 1;
}
int findRoot(int x){
if(root[x]==x){
return x;
}
return root[x]=findRoot(root[x]);
}
int sizx(int x){
x=findRoot(x);
return siz[x];
}
};
long long lenans(int x){
return (1LL*x*(x+1))/2;
}
const int MAX_LEN=1e6+5;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,q;
cin >> n >> q;
int h[n];
vector<int> mp[MAX_LEN];
for(int i = 0;i<n;i++){
cin >> h[i];
mp[h[i]].push_back(i);
}
long long currans = 0;
bool val[n];
dsu ds(1e6+5);
fill(val,val+n,0);
long long ans[MAX_LEN];
for(int i = 0;i<MAX_LEN;i++){
for(int ind : mp[i]){
if(ind){
if(val[ind-1]){
currans-=lenans(ds.sizx(ind-1));
ds.unite(ind,ind-1);
}
}
if(ind<n-1){
if(val[ind+1]){
currans-=lenans(ds.sizx(ind+1));
ds.unite(ind,ind+1);
}
}
currans+=lenans(ds.sizx(ind));
val[ind]=1;
}
ans[i]=currans;
}
while(q--){
int x;
cin >> x;
cout << ans[x] << "\n";
}
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... |
# | 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... |