-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB.cpp
76 lines (74 loc) · 1.42 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
long long che[100005],l[100005],h[100005],u[100005];
int main()
{
fastio;
long long n,le,i,j,p,sum=0,x,low,high,mid,ch=0,c=0;
cin>>n;
for(i=0;i<n;i++)
{
p=0;
l[i]=INT_MAX;
h[i]=INT_MIN;
cin>>le;
for(j=0;j<le;j++)
{
cin>>x;
if(j>0 && x>l[i])
che[i]=1;
l[i]=min(l[i],x);
h[i]=max(h[i],x);
}
}
for(i=0;i<n;i++)
{
if(che[i]==1)
ch++;
else
{
u[c]=h[i];
c++;
}
}
sort(u,u+c,greater<long long>());
//cout<<ch;
for(i=0;i<n;i++)
{
//cout<<sum<<"\n";
if(che[i]==1)
{
sum+=n;
continue;
}
if(u[c-1]>l[i])
{
sum+=n;
continue;
}
if(u[0]<=l[i])
{
sum+=ch;
continue;
}
low=0;
high=c-1;
while(low<=high)
{
mid=(low+high)/2;
if(u[mid]>l[i] && u[mid+1]<=l[i])
{
sum+=mid+1;
break;
}
else if(u[mid]>l[i])
low=mid+1;
else
high=mid-1;
}
sum+=ch;
}
cout<<sum<<"\n";
return 0;
}