# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
753114 | minhnhatnoe | Pilot (NOI19_pilot) | C++14 | 539 ms | 48344 KiB |
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;
typedef long long ll;
ll cpair(ll s){
return s * (s+1) / 2;
}
struct dsu{
ll c = 0;
vector<int> p;
vector<bool> s;
dsu(int n): p(n, -1), s(n) {}
int find(int v){
if (p[v] < 0) return v;
return p[v] = find(p[v]);
}
void merge(int a, int b){
a = find(a), b = find(b);
if (a == b) return;
if (-p[a] < -p[b]) swap(a, b);
c -= cpair(-p[a]) + cpair(-p[b]);
p[a] += p[b];
c += cpair(-p[a]);
p[b] = a;
}
void activate(int pos){
c += 1;
s[pos] = true;
if (pos && s[pos-1]){
merge(pos, pos-1);
}
if (pos != s.size()-1 && s[pos+1]){
merge(pos, pos+1);
}
}
};
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n, q; cin >> n >> q;
dsu g(n);
vector<pair<int, int>> a(n), b(q);
for (int i=0; i<n; i++){
cin >> a[i].first;
a[i].second = i;
}
for (int i=0; i<q; i++){
cin >> b[i].first;
b[i].second = i;
}
vector<ll> result(q);
sort(a.begin(), a.end()), sort(b.begin(), b.end());
int aptr = 0;
for (int i=0; i<b.size(); i++){
while (aptr < a.size() && a[aptr].first <= b[i].first){
g.activate(a[aptr].second);
aptr++;
}
result[b[i].second] = g.c;
}
for (int i=0; i<result.size(); i++){
cout << result[i] << "\n";
}
}
Compilation message (stderr)
# | 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... |