Submission #924243

#TimeUsernameProblemLanguageResultExecution timeMemory
924243raysh07Pilot (NOI19_pilot)C++17
89 / 100
1076 ms127336 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define INF (int)1e18 #define f first #define s second const int N=2e5+69; int ans=0; mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count()); class DisjointSets { private: vector<int> parents; vector<int> sizes; public: DisjointSets(int size) : parents(size), sizes(size, 1) { for (int i = 0; i < size; i++) { parents[i] = i; } } /** @return the "representative" node in x's component */ int find(int x) { return parents[x] == x ? x : (parents[x] = find(parents[x])); } /** @return whether the merge changed connectivity */ bool unite(int x, int y) { int x_root = find(x); int y_root = find(y); if (x_root == y_root) { return false; } ans+=sizes[x_root]*sizes[y_root]; if (sizes[x_root] < sizes[y_root]) { swap(x_root, y_root); } sizes[x_root] += sizes[y_root]; parents[y_root] = x_root; return true; } /** @return whether x and y are in the same connected component */ bool connected(int x, int y) { return find(x) == find(y); } }; void Solve(){ int n,q; cin>>n>>q; int H[n]; int A[q]; map <int,int> mp; for(int i=0;i<n;i++) cin>>H[i]; for(int i=0;i<q;i++) cin>>A[i]; vector <pair<int,int>> vec; for(int i=0;i<n;i++) vec.push_back({H[i],i}); DisjointSets ds(n); sort(vec.begin(),vec.end()); set <int> st; int cnt = 0; for(auto [x,i]: vec){ if(i!=0&&H[i-1]<=H[i]){ ds.unite(i-1,i); } if(i!=n-1 && H[i+1]<=H[i]) ds.unite(i,i+1); cnt++; mp[x]=ans + cnt; st.insert(x); } for(int i=0;i<q;i++){ // we need to find the largest number <= x auto it = st.upper_bound(A[i]); // this finds the first place with a number > x // --it if (it == st.begin()){ cout << 0 << "\n"; } else { --it; cout << mp[*it] << "\n"; } } } int32_t main() { auto begin = std::chrono::high_resolution_clock::now(); ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // freopen("in", "r", stdin); // freopen("out", "w", stdout); //cin >> t; for(int i = 1; i <= t; i++) { //cout << "Case #" << i << ": "; Solve(); } auto end = std::chrono::high_resolution_clock::now(); auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin); cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\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...