Submission #860297

#TimeUsernameProblemLanguageResultExecution timeMemory
860297annabeth9680Pilot (NOI19_pilot)C++17
100 / 100
445 ms86552 KiB
#include <bits/stdc++.h> #define f first #define s second #define int long long using namespace std; int res = 0; struct dsu{ vector<int> D, sz; void init(int N){ D.resize(N+1); sz.resize(N+1); for(int i = 0;i<=N;++i){ D[i] = i; sz[i] = 1; } } int finden(int x){ return D[x] == x ? x : finden(D[x]); } void unite(int a, int b){ a = finden(a); b = finden(b); if(a == b) return; res += sz[a]*sz[b]; D[a] = b; sz[b] += sz[a]; } }DSU; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N,Q; cin >> N >> Q; vector<int> heights(N),ans(Q); DSU.init(N); for(int i = 0;i<N;++i) cin >> heights[i]; 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({heights[i],i}); sort(queries.begin(),queries.end()); sort(edges.begin(),edges.end()); int j = 0; for(int i = 0;i<Q;++i){ while(j < N && edges[j].f <= queries[i].f){ DSU.unite(edges[j].s,edges[j].s+1); j++; } ans[queries[i].s] = res; } for(int i = 0;i<Q;++i) cout << ans[i] << "\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...