backup
This commit is contained in:
parent
6a86ee71b6
commit
0d97e56d45
BIN
src/bin/a.out
BIN
src/bin/a.out
Binary file not shown.
|
@ -0,0 +1,427 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
res1 = (res1 + x * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + x * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n, m);
|
||||
readvec(int, a, n - 1);
|
||||
readvec(int, b, n);
|
||||
sort(a.begin(), a.end());
|
||||
sort(b.begin(), b.end());
|
||||
vector<int> mp(n - 1);
|
||||
vector<bool> mapped(n);
|
||||
int r = 0;
|
||||
int mx = -1;
|
||||
int def = 0;
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
while (r < n and a[i] >= b[r]) {
|
||||
++r;
|
||||
}
|
||||
mp[i] = r;
|
||||
if (r < n) {
|
||||
mx = r;
|
||||
def += 1;
|
||||
mapped[r] = 1;
|
||||
r += 1;
|
||||
}
|
||||
}
|
||||
def = n - def;
|
||||
vector<int> ss(n + 1);
|
||||
for (int i = n - 1; ~i; --i) {
|
||||
ss[i] = ss[i + 1] + mapped[i];
|
||||
}
|
||||
ll res = 0;
|
||||
int prev = 0;
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
int x = min(m, a[i]);
|
||||
if (prev == m or mp[i] == n) {
|
||||
break;
|
||||
}
|
||||
if (mp[i] == 0 or i and mp[i - 1] == mp[i] - 1) {
|
||||
if (ss[mp[i]] == n - mp[i]) {
|
||||
res += ll(1) * def * (x - prev);
|
||||
} else {
|
||||
res += ll(1) * (def - 1) * (x - prev);
|
||||
}
|
||||
} else {
|
||||
int prev_max = min(x, b[mp[i] - 1] - 1);
|
||||
res += ll(1) * (def - 1) * (prev_max - prev);
|
||||
if (ss[mp[i]] == n - mp[i]) {
|
||||
res += ll(1) * def * (x - prev_max);
|
||||
} else {
|
||||
res += ll(1) * (def - 1) * (x - prev_max);
|
||||
}
|
||||
}
|
||||
prev = x;
|
||||
}
|
||||
if (mapped[n - 1]) {
|
||||
res += ll(1) * def * (m - prev);
|
||||
} else {
|
||||
int mx = min(b[n - 1] - 1, m);
|
||||
res += ll(1) * (def - 1) * (mx - prev);
|
||||
res += ll(1) * def * (m - mx);
|
||||
}
|
||||
cout << res << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t < (DUMP_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,416 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
res1 = (res1 + x * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + x * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
|
||||
|
||||
void solve() {
|
||||
int left = 0, right = 0;
|
||||
read(int, n, m);
|
||||
vector<pii> rg;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
read(int, l, r);
|
||||
rg.emplace_back(l, r);
|
||||
}
|
||||
sort(rg.begin(), rg.end());
|
||||
min_heap<pii> pq;
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
while (pq.size() and pq.top().first < rg[i].first) {
|
||||
if (pq.top().second == 1) left -= 1;
|
||||
if (pq.top().first == m) right -= 1;
|
||||
pq.pop();
|
||||
}
|
||||
pq.emplace(rg[i].second, rg[i].first);
|
||||
if (rg[i].first == 1) left += 1;
|
||||
if (rg[i].second == m) right += 1;
|
||||
res = max(res, int(pq.size()) - min(left, right));
|
||||
}
|
||||
cout << res << '\n';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t < (DUMP_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
void solve() {
|
||||
int n, m; cin >> n >> m;
|
||||
vector<pair<int, int>> seg(n);
|
||||
|
@ -37,6 +39,16 @@ void solve() {
|
|||
cout << res << endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
int t; cin >> t;
|
||||
while (t--) solve();
|
||||
|
|
|
@ -0,0 +1,399 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
res1 = (res1 + x * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + x * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
pll intersect(ll l1, ll r1, ll l2, ll r2) {
|
||||
ll l = max(l1, l2), r = min(r1, r2);
|
||||
if (l > r) {
|
||||
return {-1, -1};
|
||||
}
|
||||
return {l, r};
|
||||
}
|
||||
|
||||
void solve() {
|
||||
using mll = MLL<MDL>;
|
||||
read(ll, a, b);
|
||||
// enumerate y
|
||||
mll res = 0;
|
||||
for (int y = 2; y < 61; ++y) {
|
||||
ll yz = 1;
|
||||
auto [l, r] = intersect(ll(1) << y, (ll(1) << y + 1) - 1, a, b);
|
||||
if (l == -1) {
|
||||
continue;
|
||||
}
|
||||
for (int z = 0; ; z += 1, yz *= y) {
|
||||
auto [p, q] = intersect(l, r, yz, yz > 4e18 / y ? LLONG_MAX : yz * y - 1);
|
||||
if (p != -1) {
|
||||
res += mll(1) * (q - p + 1) * z;
|
||||
}
|
||||
if (yz > 4e18 / y) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << res << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t < (DUMP_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,489 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
res1 = (res1 + x * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + x * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
struct pt {
|
||||
int i, x, y;
|
||||
};
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
vector<pt> a(n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
a[i].i = i;
|
||||
cin >> a[i].x;
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cin >> a[i].y;
|
||||
}
|
||||
read(int, m);
|
||||
vector<pt> b(m);
|
||||
for (int i = 0; i < m; ++i) {
|
||||
b[i].i = i;
|
||||
cin >> b[i].x;
|
||||
}
|
||||
for (int i = 0; i < m; ++i) {
|
||||
cin >> b[i].y;
|
||||
}
|
||||
int b_indicator = max(lg2(n), lg2(m)) + 1;
|
||||
// connect a to b
|
||||
int tot = (1 << b_indicator | max(n, m)) + 1;
|
||||
vector<int> ch(tot, -1);
|
||||
sort(b.begin(), b.end(), [] (const pt& a, const pt& b) { return a.x < b.x; });
|
||||
sort(a.begin(), a.end(), [] (const pt& a, const pt& b) { return a.x < b.x; });
|
||||
vector<int> ss_b(m + 1);
|
||||
ss_b[m] = m - 1;
|
||||
for (int i = m - 1; ~i; --i) {
|
||||
if (b[i].y > b[ss_b[i + 1]].y) {
|
||||
ss_b[i] = i;
|
||||
} else {
|
||||
ss_b[i] = ss_b[i + 1];
|
||||
}
|
||||
}
|
||||
// debug(ss_b);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
int l = 0, r = m - 1;
|
||||
while (l < r) {
|
||||
int mid = l + r >> 1;
|
||||
if (b[mid].x > a[i].y) {
|
||||
r = mid;
|
||||
} else {
|
||||
l = mid + 1;
|
||||
}
|
||||
}
|
||||
if (b[l].x > a[i].y) {
|
||||
ch[i] = 1 << b_indicator | ss_b[l];
|
||||
}
|
||||
}
|
||||
vector<int> ss_a(n + 1);
|
||||
ss_a[n] = n - 1;
|
||||
for (int i = n - 1; ~i; --i) {
|
||||
if (a[i].y > a[ss_a[i + 1]].y) {
|
||||
ss_a[i] = i;
|
||||
} else {
|
||||
ss_a[i] = ss_a[i + 1];
|
||||
}
|
||||
}
|
||||
// debug(ss_a);
|
||||
for (int i = 0; i < m; ++i) {
|
||||
int l = 0, r = n - 1;
|
||||
while (l < r) {
|
||||
int mid = l + r >> 1;
|
||||
if (a[mid].x > b[i].y) {
|
||||
r = mid;
|
||||
} else {
|
||||
l = mid + 1;
|
||||
}
|
||||
}
|
||||
if (a[l].x > b[i].y) {
|
||||
// debug(make_pair(i, l));
|
||||
ch[1 << b_indicator | i] = ss_a[l];
|
||||
}
|
||||
}
|
||||
vector<int> type(tot);
|
||||
vector<bool> vis(tot);
|
||||
auto dfs = [&] (auto dfs, int v) -> void {
|
||||
if (vis[v]) {
|
||||
if (type[v] == 0) {
|
||||
type[v] = -1;
|
||||
}
|
||||
} else {
|
||||
vis[v] = 1;
|
||||
if (ch[v] == -1) {
|
||||
if (v >> b_indicator) {
|
||||
type[v] = 2;
|
||||
} else {
|
||||
type[v] = 1;
|
||||
}
|
||||
if (v == 0) {
|
||||
// debug(type[v]);
|
||||
}
|
||||
} else {
|
||||
dfs(dfs, ch[v]);
|
||||
type[v] = type[ch[v]];
|
||||
}
|
||||
}
|
||||
};
|
||||
for (int i = 0; i < tot; ++i) {
|
||||
if (not vis[i] and ((i >> b_indicator) == 0 and i < n or (i >> b_indicator) == 1 and (i ^ (1 << b_indicator)) < m)) {
|
||||
dfs(dfs, i);
|
||||
}
|
||||
}
|
||||
// debug(ch);
|
||||
int win = 0, lose = 0, draw = 0;
|
||||
// debug(type);
|
||||
for (int i = 0; i < tot and not (i >> b_indicator); ++i) {
|
||||
if (type[i] == 1) {
|
||||
win += 1;
|
||||
} else if (type[i] == 2) {
|
||||
lose += 1;
|
||||
} else if (type[i] == -1) {
|
||||
draw += 1;
|
||||
}
|
||||
}
|
||||
cout << win << ' ' << draw << ' ' << lose << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t < (DUMP_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,451 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
#if LONG_LONG_MAX != INT64_MAX
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
#else
|
||||
using ll = long long;
|
||||
using ull = unsigned long long;
|
||||
#endif
|
||||
using int128 = __int128_t;
|
||||
using uint128 = __uint128_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
constexpr int128 INT128_MAX = numeric_limits<int128>::max();
|
||||
constexpr uint128 UINT128_MAX = numeric_limits<uint128>::max();
|
||||
constexpr int128 INT128_MIN = numeric_limits<int128>::min();
|
||||
constexpr uint128 UINT128_MIN = numeric_limits<uint128>::min();
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
uniform_int_distribution<mt19937::result_type> dist(PRIME);
|
||||
const size_t __array_hash_b = 31, __array_hash_mdl1 = dist(rd), __array_hash_mdl2 = dist(rd);
|
||||
struct array_hash {
|
||||
template <typename Sequence>
|
||||
size_t operator()(const Sequence& arr) const {
|
||||
size_t pw1 = 1, pw2 = 1;
|
||||
size_t res1 = 0, res2 = 0;
|
||||
for (auto&& x : arr) {
|
||||
res1 = (res1 + x * pw1) % __array_hash_mdl1;
|
||||
res2 = (res2 + x * pw2) % __array_hash_mdl2;
|
||||
pw1 = (pw1 * __array_hash_b) % __array_hash_mdl1;
|
||||
pw2 = (pw2 * __array_hash_b) % __array_hash_mdl2;
|
||||
}
|
||||
return res1 + res2;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
std::ostream& operator<<(std::ostream& dest, const int128& value) {
|
||||
// https://stackoverflow.com/a/25115163/23881100
|
||||
std::ostream::sentry s( dest );
|
||||
if ( s ) {
|
||||
uint128 tmp = value < 0 ? -value : value;
|
||||
char buffer[ 128 ];
|
||||
char* d = std::end( buffer );
|
||||
do {
|
||||
-- d;
|
||||
*d = "0123456789"[ tmp % 10 ];
|
||||
tmp /= 10;
|
||||
} while ( tmp != 0 );
|
||||
if ( value < 0 ) {
|
||||
-- d;
|
||||
*d = '-';
|
||||
}
|
||||
int len = std::end( buffer ) - d;
|
||||
if ( dest.rdbuf()->sputn( d, len ) != len ) {
|
||||
dest.setstate( std::ios_base::badbit );
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* modular arithmetic */
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
MLL(const MLL<mdl>& other) : val(other.val) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
friend bool operator==(const MLL& lhs, const MLL& rhs) { return lhs.val == rhs.val; }
|
||||
friend bool operator!=(const MLL& lhs, const MLL& rhs) { return lhs.val != rhs.val; }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 7219
|
||||
|
||||
void dump() {}
|
||||
|
||||
void dump_ignore() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
vector<ll> a(n + 1);
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
cin >> a[i];
|
||||
}
|
||||
adj(ch, n);
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
read(int, u, v);
|
||||
edge(ch, u, v);
|
||||
}
|
||||
if (n == 2) {
|
||||
ll res = max(max(max(ll(0), a[1]), a[2]), a[1] + a[2]);
|
||||
cout << res << '\n';
|
||||
} else {
|
||||
ll res = 0;
|
||||
vector<ll> dp(n + 1);
|
||||
int root = 1;
|
||||
auto dfs = [&] (auto dfs, int v, int pa) -> void {
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
dfs(dfs, u, v);
|
||||
}
|
||||
int c = ch[v].size() - (v != root);
|
||||
if (v != root) {
|
||||
ll no_v = -INFLL, has_v = a[v];
|
||||
vector<ll> son;
|
||||
ll tot = 0;
|
||||
if (c > 0) {
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;;
|
||||
no_v = max(no_v, dp[u]);
|
||||
son.emplace_back(-dp[u]);
|
||||
tot += dp[u];
|
||||
}
|
||||
if (c > 1) {
|
||||
sort(son.begin(), son.end(), greater<>());
|
||||
ll curr = 0;
|
||||
for (int i = 0; i < c - 2 and son[i] > 0; ++i) {
|
||||
curr += son[i];
|
||||
}
|
||||
has_v = max(has_v, a[v] + tot + curr);
|
||||
}
|
||||
}
|
||||
dp[v] = max(has_v, no_v);
|
||||
}
|
||||
ll no_v = 0, has_v = a[v];
|
||||
vector<ll> son;
|
||||
ll tot = 0;
|
||||
if (c > 1) {
|
||||
vector<ll> al;
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;;
|
||||
al.emplace_back(dp[u]);
|
||||
son.emplace_back(-dp[u]);
|
||||
tot += dp[u];
|
||||
}
|
||||
sort(al.begin(), al.end(), greater<>());
|
||||
if (al.size() >= 2) {
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
no_v += al[i];
|
||||
}
|
||||
}
|
||||
if (c > 2) {
|
||||
sort(son.begin(), son.end(), greater<>());
|
||||
ll curr = 0;
|
||||
for (int i = 0; i < c - 3 and son[i] > 0; ++i) {
|
||||
curr += son[i];
|
||||
}
|
||||
has_v = max(has_v, a[v] + tot + curr);
|
||||
}
|
||||
}
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
has_v = max(has_v, a[v] + dp[u]);
|
||||
}
|
||||
// debug(make_tuple(v, has_v, no_v, dp[v]));
|
||||
res = max(res, max(has_v, no_v));
|
||||
};
|
||||
dfs(dfs, root, 0);
|
||||
cout << res << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (t < (DUMP_TEST_CASE)) {
|
||||
solve();
|
||||
} else if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
dump_ignore();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -31,6 +31,8 @@ using pii = pair<int, int>;
|
|||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
|
@ -338,20 +340,33 @@ void dump_ignore() {}
|
|||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
readvec(int, a, n);
|
||||
unordered_map<int, int, safe_hash> oc;
|
||||
for (int i = 0; i < n; ++i) oc[a[i]] += 1;
|
||||
unordered_set<int, safe_hash> has;
|
||||
ll res = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (not has.count(a[i])) {
|
||||
has.emplace(a[i]);
|
||||
res += oc.size();
|
||||
pll intersect(ll l1, ll r1, ll l2, ll r2) {
|
||||
ll l = max(l1, l2), r = min(r1, r2);
|
||||
if (l > r) {
|
||||
return {-1, -1};
|
||||
}
|
||||
return {l, r};
|
||||
}
|
||||
|
||||
void solve() {
|
||||
using mll = MLL<MDL>;
|
||||
read(ll, a, b);
|
||||
// enumerate y
|
||||
mll res = 0;
|
||||
for (int y = 2; y < 61; ++y) {
|
||||
ll yz = 1;
|
||||
auto [l, r] = intersect(ll(1) << y, (ll(1) << y + 1) - 1, a, b);
|
||||
if (l == -1) {
|
||||
continue;
|
||||
}
|
||||
for (int z = 0; ; z += 1, yz *= y) {
|
||||
auto [p, q] = intersect(l, r, yz, yz > 4e18 / y ? LLONG_MAX : yz * y - 1);
|
||||
if (p != -1) {
|
||||
res += mll(1) * (q - p + 1) * z;
|
||||
}
|
||||
if (yz > 4e18 / y) {
|
||||
break;
|
||||
}
|
||||
if (--oc[a[i]] == 0) {
|
||||
oc.erase(a[i]);
|
||||
}
|
||||
}
|
||||
cout << res << '\n';
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
6
|
||||
1
|
||||
1
|
||||
2
|
||||
1 1
|
||||
3
|
||||
1 2 1
|
||||
4
|
||||
2 3 2 1
|
||||
5
|
||||
4 5 4 5 4
|
||||
10
|
||||
1 7 7 2 3 4 3 2 1 100
|
||||
12
|
||||
4 6
|
||||
4 7
|
||||
4 8
|
||||
4 100000
|
||||
179 1000000000000000000
|
||||
57 179
|
||||
4 201018959
|
||||
7 201018960
|
||||
729 50624
|
||||
728 50624
|
||||
728 50625
|
||||
729 50625
|
||||
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
-1 3 -5
|
||||
-1 -1
|
||||
-1 3
|
||||
1 1 1 -7
|
|
@ -31,6 +31,8 @@ using pii = pair<int, int>;
|
|||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
template <typename T> using max_heap = priority_queue<T>;
|
||||
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<>>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
|
|
|
@ -1,54 +1,43 @@
|
|||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int cherryPickup(vector<vector<int>>& a) {
|
||||
int n = a.size();
|
||||
vector<vector<int>> dp(n, vector<int>(n));
|
||||
vector<vector<int>> col_pos(n, vector<int>(n + 1)), col_neg(n, vector<int>(n + 1));
|
||||
vector<vector<int>> row_pos(n, vector<int>(n + 1)), row_neg(n, vector<int>(n + 1));
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 1; j <= n; ++j) {
|
||||
row_pos[i][j] = row_pos[i][j - 1] + (a[i][j - 1] == 1);
|
||||
row_neg[i][j] = row_neg[i][j - 1] + (a[i][j - 1] == -1);
|
||||
typedef long long ll;
|
||||
|
||||
const int MAXN = 5e5 + 10;
|
||||
|
||||
int t, n; ll a[MAXN], dp[MAXN], ans;
|
||||
|
||||
vector<int> g[MAXN];
|
||||
|
||||
void dfs(int u, int fa) {
|
||||
ll x; vector<ll> d; dp[u] = a[u], ans = max(ans, dp[u]);
|
||||
for (int v : g[u]) {
|
||||
if (v == fa) continue; dfs(v, u), d.push_back(dp[v]);
|
||||
ans = max(ans, a[u] + dp[v]), dp[u] = max(dp[u], dp[v]);
|
||||
}
|
||||
sort(d.begin(), d.end(), greater<ll>());
|
||||
if (d.size() >= 2) {
|
||||
x = d[0] + d[1], ans = max(ans, x);
|
||||
for (int i = 2; i < d.size() && d[i] > 0; i++) x += d[i];
|
||||
dp[u] = max(dp[u], a[u] + x);
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 1; j <= n; ++j) {
|
||||
col_pos[i][j] = col_pos[i][j - 1] + (a[j - 1][i] == 1);
|
||||
col_neg[i][j] = col_neg[i][j - 1] + (a[j - 1][i] == -1);
|
||||
if (d.size() >= 3) {
|
||||
x = d[0] + d[1] + d[2];
|
||||
for (int i = 3; i < d.size() && d[i] > 0; i++) x += d[i];
|
||||
ans = max(ans, a[u] + x);
|
||||
}
|
||||
}
|
||||
dp[0][0] = (a[0][0] == 1);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
for (int k = 0; k <= i; ++k) {
|
||||
for (int l = 0; l <= j; ++l) {
|
||||
if (k == i and l == j) continue;
|
||||
if (row_neg[i][j + 1] - row_neg[i][l] or row_neg[k][j + 1] - row_neg[k][l]) continue;
|
||||
if (col_neg[l][i + 1] - col_neg[l][k] or col_neg[j][i + 1] - col_neg[j][k]) continue;
|
||||
if (k == i) {
|
||||
dp[i][j] = max(dp[i][j], dp[k][l] + row_pos[i][j + 1] - row_pos[i][l + 1]);
|
||||
} else if (l == j) {
|
||||
dp[i][j] = max(dp[i][j], col_pos[j][i + 1] - col_pos[j][k + 1]);
|
||||
} else {
|
||||
dp[i][j] = max(dp[i][j], dp[k][l] + row_pos[k][j + 1] - row_pos[k][l] + col_pos[j][i + 1] - col_pos[j][k] + row_pos[i][j + 1] - row_pos[i][l] + col_pos[l][i + 1] - col_pos[l][k ] - 2 * (a[k][l] == 1) - (a[k][j] == 1) - (a[i][j] == 1) - (a[i][l] == 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// for (int i = 0; i < n; ++i) {
|
||||
// for (int j = 0; j < n; ++j) {
|
||||
// cerr << dp[i][j] << " \n"[j + 1 == n];
|
||||
// }
|
||||
// }
|
||||
return dp[n - 1][n - 1];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<vector<int>> grid = {{0, 1, -1}, {1, 0, -1}, {1, 1, 1}};
|
||||
cout << Solution().cherryPickup(grid);
|
||||
for (scanf("%d", &t); t--;) {
|
||||
scanf("%d", &n), ans = 0;
|
||||
for (int i = 1; i <= n; i++) g[i].clear();
|
||||
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
|
||||
for (int i = 1, u, v; i < n; i++) {
|
||||
scanf("%d%d", &u, &v);
|
||||
g[u].push_back(v), g[v].push_back(u);
|
||||
}
|
||||
dfs(1, 0), printf("%lld\n", ans);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,17 +1,29 @@
|
|||
from random import shuffle, randint
|
||||
from itertools import pairwise
|
||||
from os import system
|
||||
|
||||
while True:
|
||||
n = randint(2, 10)
|
||||
a = list(range(n))
|
||||
shuffle(a)
|
||||
inp = [str(i ^ j) for i, j in pairwise(a)]
|
||||
with open('std.in', 'w') as f:
|
||||
f.write(str(n) + '\n' + ' '.join(inp))
|
||||
ret = system('./a.out < std.in > /dev/null')
|
||||
if ret:
|
||||
print(a)
|
||||
print(inp)
|
||||
break
|
||||
def gen_tree(n):
|
||||
tree = ''
|
||||
for i in range(2, n + 1):
|
||||
fa = randint(1, i - 1)
|
||||
tree += f'{fa} {i}\n'
|
||||
return tree
|
||||
|
||||
if __name__ == '__main__':
|
||||
while 1:
|
||||
n = randint(2, 10)
|
||||
tr = gen_tree(n)
|
||||
val = ' '.join([str(randint(-10, 10)) for _ in range(n)])
|
||||
content = '\n'.join((str(n), val, tr))
|
||||
with open('std.in', 'w') as f:
|
||||
f.write('1\n' + content)
|
||||
system('./my.out < std.in > std.out')
|
||||
with open('std.out') as f:
|
||||
my_ans = f.read().strip()
|
||||
system('./ans.out < std.in > std.out')
|
||||
with open('std.out') as f:
|
||||
his_ans = f.read().strip()
|
||||
if my_ans != his_ans:
|
||||
print(content)
|
||||
print(my_ans)
|
||||
print(his_ans)
|
||||
break
|
||||
|
|
Loading…
Reference in New Issue