-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB.cpp
45 lines (44 loc) · 782 Bytes
/
B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector <int> mn, mx;
long long ans = 0;
int cnt = 0;
for(int i = 1; i <= n; i++) {
int l;
scanf("%d", &l);
int p = INT_MIN;
int q = INT_MAX;
bool good = false;
while(l--) {
int x;
scanf("%d", &x);
if(q < x) {
good = true;
}
p = max(p, x);
q = min(q, x);
}
if(!good) {
mn.push_back(q);
mx.push_back(p);
} else {
++cnt;
}
}
sort(mn.begin(), mn.end());
sort(mx.begin(), mx.end());
int l = 0;
for(int i = 0; i < mx.size(); i++) {
while(l < mn.size() && mn[l] < mx[i]) {
++l;
}
ans += l;
}
ans += 2LL * cnt * (n - cnt);
ans += 1LL * cnt * cnt;
printf("%lld\n", ans);
return 0;
}