Submission #853405

#TimeUsernameProblemLanguageResultExecution timeMemory
853405annabeth9680Pilot (NOI19_pilot)C++17
40 / 100
30 ms5572 KiB
#include <bits/stdc++.h> using namespace std; int res = 0; struct dsu{ vector<int> D,dep; void init(int N){ D.resize(N+1); dep.resize(N+1); for(int i = 0; i<=N;++i){ D[i] = i; dep[i] = 1; } } int finden(int x){ return D[x] = (D[x] == x ? x : finden(D[x])); } void unite(int a, int b){ a = finden(a); b = finden(b); if(a == b) return; res += dep[a]*dep[b]; D[a] = b; dep[b] += dep[a]; } }DSU; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N,Q; cin >> N >> Q; vector<int> height(N),ans(Q); for(int i = 0;i<N;++i) cin >> height[i]; DSU.init(N); vector<pair<int,int>> queries, edges; for(int i = 0;i<Q;++i){ int x; cin >> x; queries.push_back({x,i}); } for(int i = 0;i<N;++i) edges.push_back({height[i],i}); sort(queries.begin(),queries.end()); sort(edges.begin(),edges.end()); int pos = 0; for(int i = 0;i<Q;++i){ while(pos < N && queries[i].first >= edges[pos].first){ DSU.unite(edges[pos].second,edges[pos].second+1); pos++; } ans[queries[i].second] = res; } for(int i = 0;i<Q;++i) cout << ans[i] << " "; cout << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...