#include <bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define forn(i, n) for(int (i) = 0; (i) < n; ++(i))
#define fornr(i, from, to) for(int (i) = from; (i) <= to; ++(i))
#define endl '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair < int, int >;
using pll = pair < ll, ll >;
using vi = vector < int >;
using vii = vector < vi >;
template < typename T >
std::ostream& operator<<(std::ostream& os, const std::vector < T > &vec)
{
forn(i, vec.size())
os << vec[i] << ' ';
return os;
}
///////////
//// ////
///////////
int main()
{
ios_base::sync_with_stdio(false);
int64_t n;
cin >> n;
vector < pair < int, int > > seq;
seq.reserve(n);
int64_t ans = 0;
int64_t cnt = 0;
for(int i = 0; i < n; ++i)
{
int q;
cin >> q;
vector < int > v(q);
for(int &i : v)
cin >> i;
int mn = 0;
int mx = 0;
for(int j = 1; j < q; ++j)
{
if(v[j] < v[mn])
mn = j;
if(v[j] > v[mx])
mx = j;
}
bool ok = 1;
for(int j = 1; j < q; ++j)
if(!(v[j-1] >= v[j]))
{
ok = 0;
break;
}
if(ok)
seq.push_back(make_pair(v[mn], v[mx]));
else
++cnt;
}
ans += cnt*cnt;
ans += seq.size()*2*cnt;
sort(all(seq), [](pii &l, pii &r){
return l.second < r.second;
});
for(int i = 0; i < seq.size(); ++i)
{
int l = -1, r = seq.size()-1;
while(r - l > 1)
{
int mid = l + r >> 1;
if(seq[i].first < seq[mid].second)
r = mid;
else
l = mid;
}
int to_add = seq.size()-r;
bool b = 0;
if(i >= r && seq[i].first == seq[i].second) --to_add, b = 1;
ans += to_add;
}
cout << ans;
return 0;
}
ICAgICNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgogICAgIAogICAgI2RlZmluZSBmYXN0X2lvIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpLCBjaW4udGllKDApL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U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICAgICAgaW50NjRfdCBuOwogICAgICAgIGNpbiA+PiBuOwogICAgIAogICAgICAgIHZlY3RvciA8IHBhaXIgPCBpbnQsIGludCA+ID4gc2VxOwogICAgICAgIHNlcS5yZXNlcnZlKG4pOwogICAgIAogICAgICAgIGludDY0X3QgYW5zID0gMDsKICAgICAgICBpbnQ2NF90IGNudCA9IDA7CiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IG47ICsraSkKICAgICAgICB7CiAgICAgICAgICAgIGludCBxOwogICAgICAgICAgICBjaW4gPj4gcTsKICAgICAKICAgICAgICAgICAgdmVjdG9yIDwgaW50ID4gdihxKTsKICAgICAgICAgICAgZm9yKGludCAmaSA6IHYpCiAgICAgICAgICAgICAgICBjaW4gPj4gaTsKICAgICAKICAgICAgICAgICAgaW50IG1uID0gMDsKICAgICAgICAgICAgaW50IG14ID0gMDsKICAgICAKICAgICAgICAgICAgZm9yKGludCBqID0gMTsgaiA8IHE7ICsraikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYodltqXSA8IHZbbW5dKQogICAgICAgICAgICAgICAgICAgIG1uID0gajsKICAgICAgICAgICAgICAgIGlmKHZbal0gPiB2W214XSkKICAgICAgICAgICAgICAgICAgICBteCA9IGo7CiAgICAgICAgICAgIH0KICAgICAKICAgICAgICAgICAgYm9vbCBvayA9IDE7CiAgICAgICAgICAgIGZvcihpbnQgaiA9IDE7IGogPCBxOyArK2opCiAgICAgICAgICAgICAgICBpZighKHZbai0xXSA+PSB2W2pdKSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBvayA9IDA7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgCiAgICAgICAgICAgIGlmKG9rKQogICAgICAgICAgICAgICAgc2VxLnB1c2hfYmFjayhtYWtlX3BhaXIodlttbl0sIHZbbXhdKSk7CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICsrY250OwogICAgIAogICAgICAgIH0KICAgICAgICBhbnMgKz0gY250KmNudDsKICAgICAgICBhbnMgKz0gc2VxLnNpemUoKSoyKmNudDsKICAgICAKICAgICAgICBzb3J0KGFsbChzZXEpLCBbXShwaWkgJmwsIHBpaSAmcil7CiAgICAgICAgICAgIHJldHVybiBsLnNlY29uZCA8IHIuc2Vjb25kOwogICAgICAgIH0pOwogICAgIAogICAgIAogICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBzZXEuc2l6ZSgpOyArK2kpCiAgICAgICAgewogICAgICAgICAgICBpbnQgbCA9IC0xLCByID0gc2VxLnNpemUoKS0xOwogICAgICAgICAgICB3aGlsZShyIC0gbCA+IDEpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGludCBtaWQgPSBsICsgciA+PiAxOwogICAgIAogICAgICAgICAgICAgICAgaWYoc2VxW2ldLmZpcnN0IDwgc2VxW21pZF0uc2Vjb25kKQogICAgICAgICAgICAgICAgICAgIHIgPSBtaWQ7CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgbCA9IG1pZDsKICAgICAgICAgICAgfQogICAgICAgICAgICBpbnQgdG9fYWRkID0gc2VxLnNpemUoKS1yOwogICAgICAgICAgICBib29sIGIgPSAwOwogICAgICAgICAgICBpZihpID49IHIgJiYgc2VxW2ldLmZpcnN0ID09IHNlcVtpXS5zZWNvbmQpIC0tdG9fYWRkLCBiID0gMTsKICAgICAgICAgICAgYW5zICs9IHRvX2FkZDsKICAgICAgICB9CiAgICAgICAgY291dCA8PCBhbnM7CiAgICAgICAgcmV0dXJuIDA7CiAgICB9
Main.java:1: error: illegal character: '#'
#include <bits/stdc++.h>
^
Main.java:1: error: class, interface, or enum expected
#include <bits/stdc++.h>
^
Main.java:3: error: illegal character: '#'
#define fast_io ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
^
Main.java:4: error: illegal character: '#'
#define pb push_back
^
Main.java:5: error: illegal character: '#'
#define mp make_pair
^
Main.java:6: error: illegal character: '#'
#define all(v) (v).begin(), (v).end()
^
Main.java:7: error: illegal character: '#'
#define rall(v) (v).rbegin(), (v).rend()
^
Main.java:8: error: illegal character: '#'
#define forn(i, n) for(int (i) = 0; (i) < n; ++(i))
^
Main.java:8: error: class, interface, or enum expected
#define forn(i, n) for(int (i) = 0; (i) < n; ++(i))
^
Main.java:8: error: class, interface, or enum expected
#define forn(i, n) for(int (i) = 0; (i) < n; ++(i))
^
Main.java:9: error: illegal character: '#'
#define fornr(i, from, to) for(int (i) = from; (i) <= to; ++(i))
^
Main.java:9: error: class, interface, or enum expected
#define fornr(i, from, to) for(int (i) = from; (i) <= to; ++(i))
^
Main.java:9: error: class, interface, or enum expected
#define fornr(i, from, to) for(int (i) = from; (i) <= to; ++(i))
^
Main.java:10: error: illegal character: '#'
#define endl '\n'
^
Main.java:13: error: class, interface, or enum expected
using ll = long long;
^
Main.java:14: error: class, interface, or enum expected
using ull = unsigned long long;
^
Main.java:15: error: class, interface, or enum expected
using pii = pair < int, int >;
^
Main.java:16: error: class, interface, or enum expected
using pll = pair < ll, ll >;
^
Main.java:17: error: class, interface, or enum expected
using vi = vector < int >;
^
Main.java:18: error: class, interface, or enum expected
using vii = vector < vi >;
^
Main.java:20: error: class, interface, or enum expected
template < typename T >
^
Main.java:25: error: class, interface, or enum expected
return os;
^
Main.java:26: error: class, interface, or enum expected
}
^
Main.java:36: error: class, interface, or enum expected
int64_t n;
^
Main.java:37: error: class, interface, or enum expected
cin >> n;
^
Main.java:39: error: class, interface, or enum expected
vector < pair < int, int > > seq;
^
Main.java:40: error: class, interface, or enum expected
seq.reserve(n);
^
Main.java:42: error: class, interface, or enum expected
int64_t ans = 0;
^
Main.java:43: error: class, interface, or enum expected
int64_t cnt = 0;
^
Main.java:44: error: class, interface, or enum expected
for(int i = 0; i < n; ++i)
^
Main.java:44: error: class, interface, or enum expected
for(int i = 0; i < n; ++i)
^
Main.java:44: error: class, interface, or enum expected
for(int i = 0; i < n; ++i)
^
Main.java:47: error: class, interface, or enum expected
cin >> q;
^
Main.java:49: error: class, interface, or enum expected
vector < int > v(q);
^
Main.java:50: error: class, interface, or enum expected
for(int &i : v)
^
Main.java:53: error: class, interface, or enum expected
int mn = 0;
^
Main.java:54: error: class, interface, or enum expected
int mx = 0;
^
Main.java:56: error: class, interface, or enum expected
for(int j = 1; j < q; ++j)
^
Main.java:56: error: class, interface, or enum expected
for(int j = 1; j < q; ++j)
^
Main.java:56: error: class, interface, or enum expected
for(int j = 1; j < q; ++j)
^
Main.java:60: error: class, interface, or enum expected
if(v[j] > v[mx])
^
Main.java:62: error: class, interface, or enum expected
}
^
Main.java:65: error: class, interface, or enum expected
for(int j = 1; j < q; ++j)
^
Main.java:65: error: class, interface, or enum expected
for(int j = 1; j < q; ++j)
^
Main.java:65: error: class, interface, or enum expected
for(int j = 1; j < q; ++j)
^
Main.java:69: error: class, interface, or enum expected
break;
^
Main.java:70: error: class, interface, or enum expected
}
^
Main.java:74: error: class, interface, or enum expected
else
^
Main.java:77: error: class, interface, or enum expected
}
^
Main.java:79: error: class, interface, or enum expected
ans += seq.size()*2*cnt;
^
Main.java:81: error: class, interface, or enum expected
sort(all(seq), [](pii &l, pii &r){
^
Main.java:83: error: class, interface, or enum expected
});
^
Main.java:86: error: class, interface, or enum expected
for(int i = 0; i < seq.size(); ++i)
^
Main.java:86: error: class, interface, or enum expected
for(int i = 0; i < seq.size(); ++i)
^
Main.java:86: error: class, interface, or enum expected
for(int i = 0; i < seq.size(); ++i)
^
Main.java:89: error: class, interface, or enum expected
while(r - l > 1)
^
Main.java:93: error: class, interface, or enum expected
if(seq[i].first < seq[mid].second)
^
Main.java:95: error: class, interface, or enum expected
else
^
Main.java:97: error: class, interface, or enum expected
}
^
Main.java:99: error: class, interface, or enum expected
bool b = 0;
^
Main.java:100: error: class, interface, or enum expected
if(i >= r && seq[i].first == seq[i].second) --to_add, b = 1;
^
Main.java:101: error: class, interface, or enum expected
ans += to_add;
^
Main.java:102: error: class, interface, or enum expected
}
^
Main.java:104: error: class, interface, or enum expected
return 0;
^
Main.java:105: error: class, interface, or enum expected
}
^
65 errors