# 2020牛客暑期多校训练营（第七场）

## Contest Info

Solved A B C D E F G H I J
6 / 10 - O O O - - - O O O
• O 在比赛中通过
• Ø 赛后通过
• ! 尝试了但是失败了
• - 没有尝试

## Solutions

Code
// Author : heyuhhh
// Created Time : 2020/08/01 12:54:54
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;
void run() {
int n, m;
cin >> n >> m;
vector<int> ans;
while (n) {
if (n < m) swap(n, m);
for (int i = 1; i <= m; i++) {
ans.push_back(m);
}
n -= m;
}
sort(all(ans));
reverse(all(ans));
cout << sz(ans) << '\n';
for (auto it : ans) {
cout << it << ' ';
}
cout << '\n';
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}


### C. A National Pandemic

• 选定$$x$$，然后每个点权值加上$$w-dist(x,y)$$
• $$F(x)=min(F(x),0)$$
• 询问$$F(x)$$

Code
// Author : heyuhhh
// Created Time : 2020/08/02 18:04:12
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
void err(int x) {cerr << x;}
void err(long long x) {cerr << x;}
void err(double x) {cerr << x;}
void err(char x) {cerr << '"' << x << '"';}
void err(const string &x) {cerr << '"' << x << '"';}
void _print() {cerr << "]\n";}
template<typename T, typename V>
void err(const pair<T, V> &x) {cerr << '{'; err(x.first); cerr << ','; err(x.second); cerr << '}';}
template<typename T>
void err(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), err(i); cerr << "}";}
template <typename T, typename... V>
void _print(T t, V... v) {err(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef Local
#define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
const int N = 1e5 + 5;

vector<int> G[N];
int n, m;
// 注意bson的初始化，其余可以自动初始化
// 注意每个点实际值为dfn[x]
int sz[N], deep[N], bson[N], ff[N];
int top[N], dfn[N], T;
void dfs(int u, int fa) {
deep[u] = deep[fa] + 1;
sz[u] = 1;
ff[u] = fa;
int Max = -1;
for (auto v : G[u]) {
if (v != fa) {
dfs(v, u);
sz[u] += sz[v];
if (sz[v] > Max) {
Max = sz[v];
bson[u] = v;
}
}
}
}

void dfs(int u, int fa, int topf) {
dfn[u] = ++T;
top[u] = topf;
if (bson[u] != 0) {
dfs(bson[u], u, topf);
}
for (auto v : G[u]) {
if (v != fa && v != bson[u]) {
dfs(v, u, v);
}
}
}

ll sumv[N << 2], lz[N << 2];

//区间加
void tag(int o, int l, int r, ll v) {
sumv[o] += 1ll * (r - l + 1) * v;
lz[o] += v;
}

void push_up(int o) {
sumv[o] = sumv[o << 1] + sumv[o << 1|1];
}

void push_down(int o, int l, int r) {
if(lz[o] != 0) {
int mid = (l + r) >> 1;
tag(o << 1, l, mid, lz[o]);
tag(o << 1|1, mid + 1, r, lz[o]);
lz[o] = 0;
}
}

void build(int o, int l, int r) {
lz[o] = 0;
if(l == r) {
sumv[o] = 0;
return;
}
int mid = (l + r) >> 1;
build(o << 1, l, mid), build(o << 1|1, mid + 1, r);
push_up(o);
}

void update(int o, int l, int r, int L, int R, int v) {
if(L <= l && r <= R) {
tag(o, l, r, v);
return;
}
push_down(o, l, r);
int mid = (l + r) >> 1;
if(L <= mid) update(o << 1, l, mid, L, R, v);
if(R > mid) update(o << 1|1, mid + 1, r, L, R, v);
push_up(o);
}

ll query(int o, int l, int r, int L, int R) {
if(L <= l && r <= R) {
return sumv[o];
}
push_down(o, l, r);
int mid = (l + r) >> 1;
ll res = 0;
if(L <= mid) res += query(o << 1, l, mid, L, R);
if(R > mid) res += query(o << 1|1, mid + 1, r, L, R);
return res;
}

ll cnt, sumx;

ll query(int x) {
ll res = 0;
int t = x;
while (x != 0) {
res += query(1, 1, n, dfn[top[x]], dfn[x]);
x = ff[top[x]];
}
return res + add[t] - 1ll * cnt * deep[t] + sumx;
}

void modify(int x) {
while (x != 0) {
update(1, 1, n, dfn[top[x]], dfn[x], 2);
x = ff[top[x]];
}
}

void run() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
G[i].clear();
bson[i] = 0;
}
T = sumx = cnt = 0;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, 0);
dfs(1, 0, 1);
build(1, 1, n);
while (m--) {
int op;
cin >> op;
if (op == 1) {
int w, x;
cin >> x >> w;
++cnt;
sumx += w - deep[x];
modify(x);
} else if (op == 2) {
int x;
cin >> x;
ll res = query(x);
if (res > 0) {
}
} else {
int x;
cin >> x;
ll res = query(x);
cout << res << '\n';
}
}
}

int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}


Code
#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define LC k<<1
#define RC k<<1|1

typedef long long LL;
const int N=510000;
const int M=1100000;
const LL mod=1e9+7;

int T,n,m;
LL delta[N],sumw;
struct point_tree
{
vector<int> ch[N];
vector<int> g[N];
int f[N],sum,n,son[N],pp[N],nxt,root;
int up[N],dis[20][N],dep[N];
LL s1[20][N],s2[20][N];
void getroot(int x,int fa)
{
son[x]=1,f[x]=0;
for (int i=0;i<g[x].size();i++)
if (!pp[g[x][i]]&&g[x][i]!=fa)
{
getroot(g[x][i],x);
son[x]+=son[g[x][i]];
f[x]=max(f[x],son[g[x][i]]);
}
f[x]=max(f[x],sum-son[x]);
if (f[x]<f[nxt]) nxt=x;
}
void dfs(int x,int fa,int depth)
{
if (fa) dis[depth][x]=dis[depth][fa]+1;
else dis[depth][x]=0;
for (int i=0;i<g[x].size();i++)
if (!pp[g[x][i]]&&g[x][i]!=fa) dfs(g[x][i],x,depth);
}
void work(int x,int fa,int depth)
{
dep[x]=depth;
dfs(x,0,depth);
up[x]=fa;
pp[x]=1;
for (int i=0;i<g[x].size();i++)
if (!pp[g[x][i]])
{
nxt=0,sum=son[g[x][i]];
getroot(g[x][i],0);
ch[x].pb(nxt);
work(nxt,x,depth+1);
}
}
void change(int x)
{
int cur=x,depth=dep[x],last=0;
while (cur)
{
s1[depth][cur]+=dis[depth][x];
s2[depth][cur]+=1;
if (last) s1[depth][last]-=dis[depth][x],s2[depth][last]-=1;
last=cur;
cur=up[cur];
depth--;
}
}
{
LL res=sumw-delta[x];
int cur=x,depth=dep[x],last=0;
while (cur)
{
res-=s1[depth][cur];
res-=s2[depth][cur]*dis[depth][x];
if (last) res-=s1[depth][last],res-=s2[depth][last]*dis[depth][x];
last=cur;
cur=up[cur];
depth--;
}
return res;
}
void init()
{
for (int i=1;i<=n;i++)
g[i].clear(),pp[i]=0;
memset(s1,0,sizeof(s1));
memset(s2,0,sizeof(s2));
for (int i=1;i<n;i++)
{
int x,y;
scanf("%d %d",&x,&y);
g[x].pb(y);
g[y].pb(x);
}
f[0]=sum=n;
nxt=0;
getroot(1,0);
root=nxt;
work(root,0,0);
}
}A;
int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d %d",&n,&m);
sumw=0;
A.n=n;
A.init();
for (int i=1;i<=n;i++)
delta[i]=0;
while (m--)
{
int op;
scanf("%d",&op);
if (op==1)
{
int x,w;
scanf("%d %d",&x,&w);
A.change(x);
sumw+=w;
}
else if (op==2)
{
int x;
scanf("%d",&x);
if (val>0) delta[x]+=val;
}
else
{
int x;
scanf("%d",&x);
printf("%lld\n",val);
}
}
}
return 0;
}


### D. Fake News

Code
// Author : heyuhhh
// Created Time : 2020/08/01 12:06:53
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 5;
void run() {
ll n;
cin >> n;
if (n == 1 || n == 24) {
cout << "Fake news!" << '\n';
} else {
cout << "Nobody knows it better than me!" << '\n';
}
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while (T--)
run();
return 0;
}


### I. Valuable Forests

$$n,n\leq 5000$$个点生成一个森林，森林的贡献为$$\sum (d(i))^2,d(i)$$代表每个结点的度数。

Code
// Author : heyuhhh
// Created Time : 2020/08/05 20:10:05
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 5000 + 5;
int MOD;

int qpow(ll a, ll b) {
ll res = 1;
while(b) {
if(b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
int fac[N], inv[N];
void init() {
fac[0] = 1;
for(int i = 1; i < N; i++) fac[i] = 1ll * fac[i - 1] * i % MOD;
inv[N - 1] = qpow(fac[N - 1], MOD - 2);
for(int i = N - 2; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % MOD;
}
int C(int n, int m) {
return 1ll * fac[n] * inv[m] % MOD * inv[n - m] % MOD;
}

int ans[N];
int f[N], A[N], a[N];

void run() {
int T;
cin >> T >> MOD;
init();
a[1] = 1;
for (int i = 2; i < N; i++) {
a[i] = qpow(i, i - 2);
}

f[0] = 1;
for (int i = 1; i < N; i++) {
for (int j = 0; j < i; j++) {
f[i] = (f[i] + 1ll * C(i - 1, j) * f[i - j - 1] % MOD * a[j + 1] % MOD) % MOD;
}
}

A[1] = 0;
for (int i = 2; i < N; i++) {
for (int d = 1; d < i; d++) {
A[i] = (A[i] + 1ll * i * C(i - 2, d - 1) % MOD * d % MOD * d % MOD * qpow(i - 1, i - 2 - d + 1) % MOD) % MOD;
}
}

ans[1] = 0;
for (int i = 2; i < N; i++) {
for (int j = 0; j < i; j++) {
ans[i] = (ans[i] + 1ll * C(i - 1, j) * ((1ll * f[i - j - 1] * A[j + 1] % MOD
+ 1ll * ans[i - j - 1] * a[j + 1] % MOD) % MOD) % MOD) % MOD;
}
}
while (T--) {
int n;
cin >> n;
cout << ans[n] << '\n';
}
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}


### J. Pointer Analysis

Code
// Author : heyuhhh
// Created Time : 2020/08/01 14:33:39
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
void err(int x) {cerr << x;}
void err(long long x) {cerr << x;}
void err(double x) {cerr << x;}
void err(char x) {cerr << '"' << x << '"';}
void err(const string &x) {cerr << '"' << x << '"';}
void _print() {cerr << "]\n";}
template<typename T, typename V>
void err(const pair<T, V> &x) {cerr << '{'; err(x.first); cerr << ','; err(x.second); cerr << '}';}
template<typename T>
void err(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), err(i); cerr << "}";}
template <typename T, typename... V>
void _print(T t, V... v) {err(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef Local
#define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
const int N = 1000 + 5;

int mp[N][N];

void Floyd(int n) {
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
mp[i][j] |= (mp[i][k] & mp[k][j]);
}
}
}
}

void run() {
int n;
cin >> n;
vector<string> el(n), er(n);
for (int i = 0; i < n; i++) {
string t;
cin >> el[i] >> t >> er[i];
}
auto is_big = [&](char x) {
return 'A' <= x && x <= 'Z';
};
auto trans_big = [&](char x) {
return x - 'A';
};
auto trans_samll = [&](char x) {
return x - 'a' + 26;
};
for (int i = 0; i < n; i++) {
if (el[i].length() == 1 && er[i].length() == 1) {
int u, v;
if (is_big(el[i][0])) {
u = trans_big(el[i][0]);
} else {
u = trans_samll(el[i][0]);
}
if (is_big(er[i][0])) {
v = trans_big(er[i][0]);
} else {
v = trans_samll(er[i][0]);
}
// dbg(u, v);
mp[u][v] = 1;
}
}
int tot = 52;
for (int i = 0; i < N; i++) {
mp[i][i] = 1;
}
Floyd(tot);

int cnt = 10;
vector<vector<int>> val(52, vector<int>(26, -1));

while (cnt--) {
for (int i = 0; i < n; i++) {
int l1 = el[i].length(), l2 = er[i].length();
if (l1 == 1 && l2 == 1) continue;
if (l2 == 1) {
int u = trans_big(el[i][0]);
int v = trans_big(er[i][0]);
int t = trans_samll(el[i][2]) - 26;
for (int j = 26; j < 52; j++) {
if (mp[u][j]) {
if (val[j][t] == -1) val[j][t] = tot++;
mp[val[j][t]][v] = 1;
}
}
}
}
Floyd(tot);
for (int i = 0; i < n; i++) {
int l1 = el[i].length(), l2 = er[i].length();
if (l1 == 1 && l2 == 1) continue;
if (l1 == 1) {
int u = trans_big(el[i][0]);
int v = trans_big(er[i][0]);
int t = trans_samll(er[i][2]) - 26;
for (int j = 26; j < 52; j++) {
if (mp[v][j]) {
// dbg(t, u, val[j][t]);
if (val[j][t] == -1) val[j][t] = tot++;
mp[u][val[j][t]] = 1;
}
}
}
}
Floyd(tot);
}

vector<vector<char>> ans(26);
for (int i = 0; i < 26; i++) {
for (int j = 26; j < 52; j++) {
if (mp[i][j]) {
ans[i].push_back('a' + (j - 26));
}
}
sort(all(ans[i]));
}
for (int i = 0; i < 26; i++) {
cout << char('A' + i) << ": ";
for (auto& it : ans[i]) {
cout << it;
}
cout << '\n';
}
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}s

posted @ 2020-08-08 09:53  heyuhhh  阅读(162)  评论(0编辑  收藏  举报