A-Tokitsukaze and a+b=n (easy)
思路
直接暴力遍历一遍一个区间,根据 a + b = n a+b=n a+b=n得出 b = n − a b=n-a b=n−a,判断b是否属于另一个区间
code
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
void solve() {
int n;
cin >> n;
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
LL res = 0;
for (int i = l1; i <= r1; i ++ ) {
if (n - i) {
if (n - i >= l2 && n - i <= r2) {
res ++ ;
}
}
}
cout << res << endl;
}
int main() {
int t = 1;
cin >> t;
while (t -- ) {
solve();
}
return 0;
}
B-Tokitsukaze and a+b=n (medium)
思路
O ( 1 ) O(1) O(1)的复杂度下才不会超时,所以要推式子
code
嘎嘎麻烦的一种写法
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
void solve() {
int n;
cin >> n;
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
int x = r1 - l1 + 1, y = r2 - l2 + 1;
if (x < y) {
swap(l1, l2);
swap(r1, r2);
}
LL res = 0;
int a = n - r1, b = n - l1;
if (l2 > b || r2 < a) {
puts("0");
} else if (a <= l2 && b >= l2 && b <= r2) {
cout << b - l2 + 1 << endl;
} else if (a <= l2 && b >= r2) {
cout << r2 - l2 + 1 << endl;
} else {
cout << r2 - a + 1 << endl;
}
}
int main() {
int t = 1;
cin >> t;
while (t -- ) {
solve();
}
return 0;
}
另一种结论的写法
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
int inter(int l1, int r1, int l2, int r2) {
return max(min(r1, r2) - max(l1, l2) + 1ll, 0ll);
}
void solve() {
int n;
cin >> n;
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
cout << inter(l2, r2, n - r1, n - l1) << endl;
}
int main() {
int t = 1;
cin >> t;
while (t -- ) {
solve();
}
return 0;
}
C-Tokitsukaze and a+b=n (hard)
思路
要求
i
≠
j
i\not=j
i=j的对数,可以求所有满足条件的
i
,
j
i,j
i,j的对数减去
i
=
j
i=j
i=j的对数
其中,
i
=
j
i=j
i=j的情况直接可以转化成B题进行计算,从一个区间中找两个符合条件的数
c
i
c_i
ci表示i被多少条线段覆盖,代表想选一个i可以有几种选择来选
所有的
i
,
j
i,j
i,j对:
a
+
b
=
n
a+b=n
a+b=n 不同的选法:
∑
a
=
0
n
c
a
∗
c
n
−
a
\sum_{a=0}^{n}c_a*c_{n-a}
∑a=0nca∗cn−a
code
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 4e5 + 10, mod = 998244353;
vector<int> v;
int a[N];
int inter(int l1, int r1, int l2, int r2) {
return max(min(r1, r2) - max(l1, l2) + 1ll, 0ll);
}
void solve() {
int n, m;
cin >> n >> m;
LL res = 0;
while (m -- ) {
int l, r;
cin >> l >> r;
a[l] ++ ; // 差分
a[r + 1] -- ;
int ans = inter(l, n - l, n - r, r); // i == j 的情况
res -= ans; // 直接减去i=j的情况
res %= mod;
}
for (int i = 1; i <= n; i ++ ) { // 差分
a[i] += a[i - 1];
a[i] %= mod;
}
for (int i = 1; i <= n; i ++ ) {
int j = n - i;
res += 1ll * a[i] * a[j] % mod;
res %= mod;
}
cout << res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t -- ) {
solve();
}
return 0;
}
D-Tokitsukaze and Energy Tree
思路
结论:两向量内部允许任意重排,则点积最大方法为都升序排列
深度从小到大排列,球的能量从小到大排列,依次相乘
求深度的两种方式:
①树dfs,dfs下一层深度+1
②维护到祖宗节点距离的并查集
code
①树dfs,dfs下一层深度+1
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
vector<int> v[N];
LL a[N], d[N];
int n;
void dfs(int u, int p) { // p表示上一个节点
for (int i = 0; i < v[u].size(); i ++ ) {
int k = v[u][i];
if (p != k) { // 假如搜到的不是上一个节点,就往下递归
d[k] = d[u] + 1;
dfs(v[u][i], u);
}
}
}
void solve() {
cin >> n;
for (int i = 2; i <= n; i ++ ) {
int x;
cin >> x;
v[x].push_back(i);
}
for (int i = 1; i <= n; i ++ ) {
cin >> a[i];
}
d[1] = 1;
dfs(1, 0);
sort(d + 1, d + 1 + n);
sort(a + 1, a + 1 + n);
// for (int i = 1; i <= n; i ++ ) {
// cout << d[i] << " ";
// }
LL res = 0;
for (int i = 1; i <= n; i ++ ) {
res += d[i] * a[i];
}
cout << res << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t -- ) {
solve();
}
return 0;
}
②维护到祖宗节点距离的并查集
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 3e5 + 10;
int p[N], d[N], a[N];
PII v[N];
int find(int x) {
if (p[x] != x) {
int u = find(p[x]);
d[x] += d[p[x]];
p[x] = u;
}
return p[x];
}
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i ++ ) {
p[i] = i;
}
for (int i = 2; i <= n; i ++ ) {
int x;
cin >> x;
int pi = find(i), px = find(x);
if (pi != px) {
p[pi] = find(i);
d[pi] = d[x] - d[i] + 1;
}
}
// for (int i = 1; i <= n; i ++ ) {
// cout << d[i] << " ";
// }
// cout << endl;
for (int i = 1; i <= n; i ++ ) {
cin >> a[i];
}
sort(a + 1, a + 1 + n);
// for (int i = 1; i <= n; i ++ ) {
// cout << a[i] << " ";
// }
// cout << endl;
sort(d + 1, d + 1 + n);
LL res = 0;
for (int i = 1; i <= n; i ++ ) {
res += (LL)(d[i] + 1) * a[i];
}
cout << res << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t -- ) {
solve();
}
return 0;
}
H-Tokitsukaze and K-Sequence
思路
单独考虑算贡献
f
(
x
,
k
)
=
{
k
−
1
,
x
>
k
x
,
x
<
=
k
f(x,k)=\begin{cases} k-1,\quad x> k\\ x, \quad x<=k \end{cases}
f(x,k)={k−1,x>kx,x<=k
x表示出现的次数,k表示第k次
因为要计算从
1
−
n
1-n
1−n中每个k的贡献的最大值,所以需要计算。
当
i
=
k
i=k
i=k时,sum表示小于k的所有和,ge表示当前set有多少个数
>
=
k
>=k
>=k, 结果就是
s
u
m
+
g
e
∗
(
i
−
1
)
sum+ge*(i-1)
sum+ge∗(i−1),sum和ge具体算法见代码块内注释
code
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
int a[N];
void solve() {
int n;
cin >> n;
memset(a, 0, sizeof a);
for (int i = 1; i <= n; i ++ ) {
int x;
cin >> x;
a[x] ++ ;
}
multiset<int> m;
for (int i = 1; i <= 100000; i ++ ) {
m.insert(a[i]);
}
LL sum = 0, ge = m.size();
for (int i = 1; i <= n; i ++ ) {
while (!m.empty() && (*m.begin()) <= i) {
sum += (LL)(*m.begin()); // 加上当前满足的情况
m.erase(m.begin()); // 从set里面删除
ge -- ; // set中>=i的数减少一个
}
cout << sum + ge * (i - 1) << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
cin >> t;
while (t -- ) {
solve();
}
return 0;
}
J-Tokitsukaze and Sum of MxAb
思路
M
x
A
b
(
i
,
j
)
=
m
a
x
(
∣
a
i
−
a
j
∣
,
∣
a
i
+
a
j
∣
)
=
∣
a
i
∣
+
∣
a
j
∣
MxAb(i,j)=max(∣a_{i} −a_{j}∣,∣a_{i}+a_{j}∣)=∣a_{i}|+|a_{j}∣
MxAb(i,j)=max(∣ai−aj∣,∣ai+aj∣)=∣ai∣+∣aj∣
∑
i
=
1
n
∑
j
=
1
n
M
x
A
b
(
i
,
j
)
=
2
n
∑
i
=
1
n
∣
a
i
∣
\sum_{i=1}^{n}\sum_{j=1}^{n}MxAb(i,j)=2n\sum_{i=1}^{n}∣a_{i}|
∑i=1n∑j=1nMxAb(i,j)=2n∑i=1n∣ai∣
当时打比赛的时候没看出来这个结论,拿4试了一下得出的结论
code
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
LL a[N];
void solve() {
int n;
cin >> n;
LL sum = 0, res = 0;
for (int i = 1; i <= n; i ++ ) {
cin >> a[i];
if (a[i] < 0) {
a[i] *= -1;
}
sum += a[i];
}
res = sum * 2 * n;
cout << res << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
cin >> t;
while (t -- ) {
solve();
}
return 0;
}
K-Tokitsukaze and Synthesis and Traits
思路
题意转化为:给一张可能不连通的无向图,q 次查询,每次给出 k 个点,查询
k
∗
(
k
−
1
)
2
\frac{k*(k-1)}{2}
2k∗(k−1) 条边中,有多少条边在原图中出现过。
①直接按照度大小根号分治
②建图时建有向边,度小点指向度大点(度:原图的度数)
code
②
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
vector<int> v[N];
int a[N], b[N], d[N], x[N], now[N];
void solve() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= m; i ++ ) {
cin >> a[i] >> b[i];
d[a[i]] ++ ; // a[i] 的度++
d[b[i]] ++ ; // b[i] 的度++
}
for (int i = 1; i <= m; i ++ ) { // 建图 度小点指向度大点
if (d[a[i]] < d[b[i]]) {
v[a[i]].push_back(b[i]);
} else {
v[b[i]].push_back(a[i]);
}
}
for (int i = 1; i <= q; i ++ ) {
int k;
cin >> k;
for (int j = 1; j <= k; j ++ ) {
cin >> x[j];
now[x[j]] = i;
}
int res = 0;
for (int j = 1; j <= k; j ++ ) {
// for (int l = 0; l < v[x[j]].size(); l ++ ) {
// if (now[l] == i) {
// res ++ ;
// }
// }
for (auto to : v[x[j]]) {
if (now[to] == i) {
res ++ ;
}
}
}
cout << res << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t -- ) {
solve();
}
return 0;
}
L-Tokitsukaze and Three Integers
思路
(
a
i
∗
a
j
+
a
k
)
≡
x
(
m
o
d
p
)
(a_i*a_j+a_k)\equiv x(mod p)
(ai∗aj+ak)≡x(modp) 枚举只能枚举两个变量,三个会超时
枚举
a
k
a_k
ak 和
x
x
x,
a
i
∗
a
j
=
x
−
a
k
(
m
o
d
p
)
a_i*a_j=x-a_k(mod p)
ai∗aj=x−ak(modp),问题转化为
a
i
∗
a
j
=
一个确定的值
a_i*a_j =一个确定的值
ai∗aj=一个确定的值,问这样的
a
i
∗
a
j
a_i*a_j
ai∗aj有多少对
code
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 5000 + 10;
vector<int> v;
LL a[N], b[N], c[N][N];
// b[x] : ai * aj % p == x
// c[k][x] : ak * ai % p == x
void solve() {
int n, p;
cin >> n >> p;
for (int i = 1; i <= n; i ++ ) {
cin >> a[i];
a[i] %= p;
}
for (int i = 1; i <= n; i ++ ) {
for (int j = 1; j <= n; j ++ ) {
if (i == j) {
continue;
}
b[a[i] * a[j] % p] ++ ;
c[i][a[i] * a[j] % p] += 2; // 每个k会被a[i]和a[j]计算两次,最后结果需要去掉重复部分
}
}
for (int x = 0; x < p; x ++ ) {
LL res = 0;
for (int k = 1; k <= n; k ++ ) {
LL tmp = (x - a[k] + p) % p; // 当前ai*aj的结果
res += b[tmp] - c[k][tmp]; // ai*aj个数-其中有一个是ak的个数
}
cout << res << " ";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t -- ) {
solve();
}
return 0;
}