#include <bits/stdc++.h>
using namespace std;
struct countGreater
{
vector<vector<int>> seg;
vector<int> deb, fin;
vector<int> valeurs;
countGreater(const vector<int> &v) : valeurs(v)
{
int nbElem(1);
while (nbElem < (int)v.size())
nbElem *= 2;
seg.resize(2 * nbElem), deb.resize(2*nbElem), fin.resize(2*nbElem);
build(1, 0, (int)v.size()-1);
}
void build(int iNoeud, int iDeb, int iFin)
{
deb[iNoeud] = iDeb, fin[iNoeud] = iFin;
if (iDeb == iFin)
{
seg[iNoeud] = {valeurs[iDeb]};
return ;
}
int iMid = (iDeb + iFin) / 2;
build(2*iNoeud, iDeb, iMid);
build(2*iNoeud+1, iMid+1, iFin);
seg[iNoeud].resize(seg[2*iNoeud].size() + seg[2*iNoeud+1].size());
merge(seg[2*iNoeud].begin(), seg[2*iNoeud].end(),
seg[2*iNoeud+1].begin(), seg[2*iNoeud+1].end(),
seg[iNoeud].begin());
}
bool query(int iNoeud, int debR, int finR, int borne)
{
if (debR > fin[iNoeud] or finR < deb[iNoeud])
return 0;
if (debR <= deb[iNoeud] and fin[iNoeud] <= finR)
{
int iPos = lower_bound(seg[iNoeud].begin(), seg[iNoeud].end(), borne)
- seg[iNoeud].begin();
return (seg[iNoeud].size() - iPos)%2;
}
return query(2*iNoeud, debR, finR, borne)
!= query(2*iNoeud+1, debR, finR, borne);
}
};
// Si Ai = Bi osef
// WLoG : Ai < Bi
// Si Tj >= Bi : swap. Si Tj < Ai : osef, sinon : on a toujours Bi apres
// On peut donc trouver le dernier j tq Ai <= Tj < Bi puis compter le nombre de k > j tq Tk >= Bi
signed main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int nbCartes, nbOperations;
cin >> nbCartes >> nbOperations;
vector<pair<int, int>> cartes(nbCartes);
for (auto &[A, B] : cartes)
cin >> A >> B;
vector<int> operations(nbOperations);
for (auto &v : operations)
cin >> v;
sort(cartes.begin(), cartes.end(),
[](pair<int, int> a, pair<int, int> b)
{
return max(a.first, a.second) < max(b.first, b.second);
});
vector<pair<int, int>> potentielsOperations;
vector<int> ordreOp(nbOperations);
for (int i(0); i < nbOperations; ++i)
ordreOp[i] = i;
sort(ordreOp.begin(), ordreOp.end(),
[&](int i, int j) { return operations[i] < operations[j]; });
countGreater seg(operations);
int curOp(0);
long long sol(0);
for (auto [A, B] : cartes)
{
if (A == B)
{
sol += A;
continue;
}
int grand = max(A, B);
int petit = min(A, B);
while (curOp < nbOperations and operations[ordreOp[curOp]] < grand)
{
while (!potentielsOperations.empty() and
potentielsOperations.back().second < ordreOp[curOp])
potentielsOperations.pop_back();
auto aAjouter = make_pair(operations[ordreOp[curOp]], ordreOp[curOp]);
if (potentielsOperations.empty()
or potentielsOperations.back().first != aAjouter.first)
potentielsOperations.emplace_back(aAjouter);
curOp++;
}
int iDeb = -1;
if (!potentielsOperations.empty() and potentielsOperations.back().first >= petit)
{
int iPos = lower_bound(potentielsOperations.begin(), potentielsOperations.end(), make_pair(petit, 0)) - potentielsOperations.begin();
iDeb = potentielsOperations[iPos].second;
}
bool nbInv = seg.query(1, iDeb+1, nbOperations-1, grand);
int choisie = A;
if (A > B)
{
if (nbInv)
choisie = B;
}
else
{
if (iDeb == -1)
{
if (nbInv)
choisie = B;
}
else
{
if (!nbInv)
choisie = B;
}
}
sol += choisie;
}
cout << sol << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
11 ms |
2540 KB |
Output is correct |
15 |
Correct |
22 ms |
4844 KB |
Output is correct |
16 |
Correct |
38 ms |
6380 KB |
Output is correct |
17 |
Correct |
49 ms |
9580 KB |
Output is correct |
18 |
Correct |
42 ms |
9580 KB |
Output is correct |
19 |
Correct |
44 ms |
9580 KB |
Output is correct |
20 |
Correct |
47 ms |
9580 KB |
Output is correct |
21 |
Correct |
40 ms |
9580 KB |
Output is correct |
22 |
Correct |
36 ms |
9580 KB |
Output is correct |
23 |
Correct |
36 ms |
9580 KB |
Output is correct |
24 |
Correct |
38 ms |
9580 KB |
Output is correct |
25 |
Correct |
35 ms |
9580 KB |
Output is correct |
26 |
Correct |
41 ms |
9580 KB |
Output is correct |
27 |
Correct |
48 ms |
9672 KB |
Output is correct |
28 |
Correct |
45 ms |
9580 KB |
Output is correct |
29 |
Correct |
50 ms |
9580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
11 ms |
2540 KB |
Output is correct |
15 |
Correct |
22 ms |
4844 KB |
Output is correct |
16 |
Correct |
38 ms |
6380 KB |
Output is correct |
17 |
Correct |
49 ms |
9580 KB |
Output is correct |
18 |
Correct |
42 ms |
9580 KB |
Output is correct |
19 |
Correct |
44 ms |
9580 KB |
Output is correct |
20 |
Correct |
47 ms |
9580 KB |
Output is correct |
21 |
Correct |
40 ms |
9580 KB |
Output is correct |
22 |
Correct |
36 ms |
9580 KB |
Output is correct |
23 |
Correct |
36 ms |
9580 KB |
Output is correct |
24 |
Correct |
38 ms |
9580 KB |
Output is correct |
25 |
Correct |
35 ms |
9580 KB |
Output is correct |
26 |
Correct |
41 ms |
9580 KB |
Output is correct |
27 |
Correct |
48 ms |
9672 KB |
Output is correct |
28 |
Correct |
45 ms |
9580 KB |
Output is correct |
29 |
Correct |
50 ms |
9580 KB |
Output is correct |
30 |
Correct |
152 ms |
42988 KB |
Output is correct |
31 |
Correct |
158 ms |
43372 KB |
Output is correct |
32 |
Correct |
186 ms |
43756 KB |
Output is correct |
33 |
Correct |
230 ms |
44524 KB |
Output is correct |
34 |
Correct |
137 ms |
42860 KB |
Output is correct |
35 |
Correct |
239 ms |
44524 KB |
Output is correct |
36 |
Correct |
226 ms |
44524 KB |
Output is correct |
37 |
Correct |
237 ms |
44652 KB |
Output is correct |
38 |
Correct |
234 ms |
44652 KB |
Output is correct |
39 |
Correct |
245 ms |
44544 KB |
Output is correct |
40 |
Correct |
202 ms |
44524 KB |
Output is correct |
41 |
Correct |
237 ms |
44424 KB |
Output is correct |
42 |
Correct |
242 ms |
44396 KB |
Output is correct |
43 |
Correct |
182 ms |
44396 KB |
Output is correct |
44 |
Correct |
196 ms |
44588 KB |
Output is correct |
45 |
Correct |
187 ms |
44652 KB |
Output is correct |
46 |
Correct |
196 ms |
44672 KB |
Output is correct |
47 |
Correct |
220 ms |
44396 KB |
Output is correct |
48 |
Correct |
242 ms |
44524 KB |
Output is correct |
49 |
Correct |
244 ms |
44652 KB |
Output is correct |