你群の海灯节欢乐赛 2026 题解
题目信息
| 序号 | 题目名称 | 出题 | 施工 |
|---|---|---|---|
| A%B Problem | @the___ | @JuRuoOIer | |
| 白垩之章 | @JuRuoOIer | @JuRuoOIer | |
| 非凡契约 | @See_you_soon | @See_you_soon | |
| 歌仙之章 | @JuRuoOIer | @JuRuoOIer | |
| 过卒卒 | @快乐的大童 | @快乐的大童 | |
| 黑斑猫之章 | @cAncAn998244353 | @JuRuoOIer | |
| 蒟蒻 OIer 游历各国 | @IceWinter | @IceWinter | |
| 口香糖算命先生 | @lkwbian | @JuRuoOIer | |
| 石三论圣鸡 | @快乐的大童 | @快乐的大童 | |
| 四叶草之章 | 搬运 | @JuRuoOIer | |
| Tree | @vAlerainTeach | @vAlerainTeach | |
| 挑战 NP-Hard | 搬运(供题:@cAncAn998244353) | @JuRuoOIer | |
| 学政治学的 | @JuRuoOIer | @JuRuoOIer | |
| 一道小学数学题 | @vAlerainTeach | @vAlerainTeach | |
| 智慧主之章 第二幕 | 搬运 | @JuRuoOIer | |
| 智慧主之章 第一幕 | @JuRuoOIer | @JuRuoOIer | |
| 海洋与你 | @vAlerainTeach | @vAlerainTeach | |
| 问 | @the___ | @the___ | |
| 口香糖算命先生 II | @definieren | @definieren |
没有特殊说明的,题解一律来自“出题”一栏的选手。
T 题暂不开放题解。
A / Q
做法一
Solution by @JuRuoOIer
考虑将 int,然后每段乘起来压成一个 int,读入的过程中就只需要对这若干个 int 取模即可。
注意压成 int 比压成 long long 更快。当然 long long 多交几发也是可以的,我一开始不知道也压的 long long。
const ll mod[]={279770238283536000ll,86239147714071593ll,501370363510288057ll,126459568506372769ll,
5624351580503521ll,39203};
const ll rng[]={29,71,109,157,193,199};
ll a[10],b;
char ch;
void proc(ll &p,ll &r,ll np,ll nr){
for(int i=0;i<p*np;i++){
if(i%p==r&&i%np==nr){
p*=np;
r=i%p;
return ;
}
}
}
int main(){
ch=getchar();
while(ch!=' '){
a[0]*=10;
a[0]+=ch-'0';
a[0]%=mod[0];
a[1]*=10;
a[1]+=ch-'0';
a[1]%=mod[1];
a[2]*=10;
a[2]+=ch-'0';
a[2]%=mod[2];
a[3]*=10;
a[3]+=ch-'0';
a[3]%=mod[3];
a[4]*=10;
a[4]+=ch-'0';
a[4]%=mod[4];
a[5]*=10;
a[5]+=ch-'0';
a[5]%=mod[5];
ch=getchar();
}
cin>>b;
ll p=1,r=0;
for(int i=2;i<=13;i++){
if(b%i==0){
ll tmp=1;
while(b%i==0){
tmp*=i;
b/=i;
}
proc(p,r,tmp,a[0]%tmp);
}
}
if(b>1){
ll pos=0;
while(rng[pos]<b)pos++;
proc(p,r,b,a[pos]%b);
}
cout<<r;
return 0;
}
做法二
Solution by @cAncAn998244353
通过 fseek 函数实现先读入 b 后读入 a。
#include <bits/stdc++.h>
using namespace std;
//#define getc getchar
char buf[1<<21],*p1=buf,*p2=buf;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline long long read(){
char c = getc() , f = 1;
while (c > '9' || c < '0'){
if (c == '-')f = -1;
c = getc();
}
long long p = 0;
while (c >= '0' && c <= '9'){
p = p * 10 + c - '0';
c = getc();
}
return p * f;
}
char s[30000005];
int x[2005];
signed main(){
int mod = 0;
char c = getc();
while (c != ' ')c = getc();
c = getc();
while (c >= '0' && c <= '9'){
mod = mod * 10 + c - '0';
c = getc();
}
for (int i = 1;i < 10 * mod;++i){
x[i] = i % mod;
}
fseek(stdin,0,SEEK_SET);
int ans = 0;
c = getc();
while (c != ' '){
ans = x[ans * 10 + c - '0'];
c = getc();
}
printf ("%d" , ans);
return 0;
}
B
直接模拟即可,任何国外的模型应该都能写出 std。由于出题人编写的 std 有锅,此处随便找了一个选手代码。
#include<bits/stdc++.h>
//#include<bits/extc++.h>
using namespace std;
using namespace __gnu_cxx;
//using namespace __gnu_pbds;
#define int long long
#define ull unsigned long long
#define vull vector<ull>
#define inf INT_MAX
#define iinf inf
#define linf LLONG_MAX
#define pii pair<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define pipii pair<int,pii>
#define vpii vector<pair<int,int>>
#define vvpii vector<vpii>
#define _max(x,y) (((x)>(y))?(x):(y))
#define _min(x,y) (((x)<(y))?(x):(y))
#define max(...) max({__VA_ARGS__})
#define min(...) min({__VA_ARGS__})
#define tomax(x,...) ((x)=max((x),__VA_ARGS__))
#define tomin(x,...) ((x)=min((x),__VA_ARGS__))
#define tomx(x,...) tomax(x,__VA_ARGS__)
#define tomn(x,...) tomin(x,__VA_ARGS__)
#define link tfshtrj
#define y1 drshzhsrd
#define mkp make_pair
#define pb push_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define emp emplace
#define eb emplace_back
#define lowbit(x) ((x)&-(x))
#define ctz(x) __builtin_ctz(x)
#define popc(x) __builtin_popcount(x)
#define ld long double
#define ull unsigned long long
#define vull vector<ull>
#define vld vector<ld>
#define endl "\n"
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define all(x) begin(x),end(x)
#define allt(x,y,z) begin(x)+y,begin(x)+z
#define fi first
#define se second
#define beg begin
#define debug(x) cerr<<"line "<<__LINE__<<" : "<<#x<<": "<<x<<"\n"
//-O2 -std=c++14 -DONLINE_JUDGE -Wall -Wextra -Wl,-stack=536870912
const vector<string> S={"H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba","La","Ce","Pr","Nd","Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu","Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl","Pb","Bi","Po","At","Rn","Fr","Ra","Ac","Th","Pa","U","Np","Pu","Am","Cm","Bk","Cf","Es","Fm","Md","No","Lr","Rf","Db","Sg","Bh","Hs","Mt","Ds","Rg","Cn","Uut","Fl","Uup","Lv","Uus","Uuo"};
const vector<int> M={10,40,69,90,108,120,140,160,190,202,230,243,270,281,310,321,355,400,391,401,450,479,509,520,549,559,589,587,636,654,697,726,749,790,799,838,855,876,889,912,929,960,980,1011,1029,1064,1079,1124,1148,1187,1218,1276,1269,1313,1329,1373,1389,1401,1409,1442,1450,1504,1520,1573,1589,1625,1649,1673,1689,1730,1750,1785,1809,1838,1862,1902,1922,1951,1970,2006,2044,2072,2090,2090,2100,2220,2230,2260,2270,2320,2310,2380,2370,2440,2430,2470,2470,2510,2520,2570,2580,2590,2620,2650,2680,2710,2720,2700,2760,2810,2800,2850,2840,2890,2880,2930,2920,2940};
unordered_map<string,int> commass;
const int K=S.size()-1;
struct com{
string name;
vector<pair<int,int>> ele;//id,cnt
int ms;
}val[10004];
bool vis[10004];
int valcnt;
int pw[8]={1,10,100,1000,10000,100000,1000000,10000000};
int tot[130];
unordered_map<string,int> atomass;
unordered_map<string,int> atoid;
unordered_map<string,int> yid;
bool calcmass(const string&s){
if(commass.count(s)) return 0;
int coms=0;
assert(s[0]==' ');
fill(tot,tot+120,0);
int c=s.size()-1;
int mul=1;
vi sta;
int nm=1;
int len=0;
per(i,c,1){
if(s[i]==')'){
sta.eb(nm);
mul*=nm;
nm=1;
len=0;
}
if(isdigit(s[i])){
if(len==0){
nm=s[i]-'0';
len=1;
}else{
nm+=pw[len]*(s[i]-'0');
len++;
}
}
if(s[i]=='('){
if(sta.empty()) return 0;
if(len) return 0;
mul/=sta.back();
sta.pop_back();
}
if(isalpha(s[i])){
string name;
if(islower(s[i])){
if(!isalpha(s[i-1])) return 0;
if(islower(s[i-1])){
name=string{s[i-2],s[i-1],s[i]};
i-=2;
}else{
name=string{s[i-1],s[i]};
i--;
}
}else{
name=string{s[i]};
}
if(!atoid.count(name)) return 0;
tot[atoid[name]]+=nm*mul;
// cerr<<"NAME{"<<name<<"}nm{"<<nm<<"}mul{"<<mul<<"}\n";
nm=1;
len=0;
}
}
if(sta.size()) return 0;
if(len) return 0;
valcnt++;
val[valcnt].name=s;
rep(i,0,K){
if(tot[i]){
coms+=tot[i]*M[i];
val[valcnt].ele.eb(i,tot[i]);
}
}
val[valcnt].ms=coms;
commass[s]=coms;
return 1;
}
#define Q 312345
int OP[Q];
string SQ[Q][6];
int CN[Q];
bitset<10004> B2[130][130],Bt;
int tid[10];
int dis[60],DE[Q];
main(){
ios::sync_with_stdio(0),cin.tie(nullptr);
// freopen("chemistry3.in","r",stdin);
// freopen("chemistry.out","w",stdout);
rep(i,0,K) atomass[S[i]]=M[i];
rep(i,0,K) atoid[S[i]]=i;
int op;
int q=0;
for(;;){
cin>>op;
if(!op) break;
q++;
if(op==1){
OP[q]=1;
string s;
cin>>s;
s=" "+s;
SQ[q][0]=s;
bool t1=calcmass(s);
}
if(op==2){
int d;
string s;
cin>>d;
OP[q]=2+d;
if(d==0){
cin>>s;
s=" "+s;
SQ[q][0]=s;
}
if(d==1){
cin>>d;
CN[q]=d;
rep(i,0,d-1){
cin>>SQ[q][i];
// SQ[q][i].insert(beg(SQ[q][i]),' ');
}
cin>>d;
DE[q]=d;
}
}
if(op==3){
int d;
cin>>d;
CN[q]=d;
OP[q]=4;
rep(i,0,d-1){
cin>>SQ[q][i];
// SQ[q][i].insert(beg(SQ[q][i]),' ');
}
}
if(op==4){
OP[q]=5;
}
}
sort(allt(val,1,valcnt+1),[&](const com&a,const com&b){
return a.name<b.name;
});
rep(i,1,valcnt){
yid[val[i].name]=i;
}
// rep(i,1,valcnt){
// cerr<<"name{"<<val[i].name<<"}; \nel: ";
// int res=0;
// for(auto[x,y]:val[i].ele) res+=M[x]*y,cerr<<"{"<<S[x]<<","<<y<<"} ";
// debug(res);
// cerr<<"\n";
// }
fill(tot,tot+120,0);
int as=0;
rep(i,1,q){
// debug(i);debug(OP[i]);
op=OP[i];
// if(i==)
if(OP[i]==1){
if(commass.count(SQ[i][0])){
int id=yid[SQ[i][0]];
if(vis[id]){
cout<<"Repeat.\n";
}else{
for(size_t k=0;k<val[id].ele.size();k++){
tot[val[id].ele[k].fi]+=1;
}
as+=1;
for(size_t k=0;k<val[id].ele.size();k++){
for(size_t j=k;j<val[id].ele.size();j++){
B2[val[id].ele[k].fi][val[id].ele[j].fi][id]=1;
}
}
vis[id]=1;
cout<<"=";
int d=commass[SQ[i][0]];
// if(d%10)
cout<<d/10<<"."<<d%10<<".\n";
// else cout<<d/10<<".\n";
}
}else{
cout<<"Invalid.\n";
}
}else if(OP[i]==2){
int id=yid[SQ[i][0]];
if(vis[id]){
cout<<"Successfully.\n";
vis[id]=0;
// debug(id);
for(size_t k=0;k<val[id].ele.size();k++){
tot[val[id].ele[k].fi]-=1;
}
as-=1;
for(size_t k=0;k<val[id].ele.size();k++){
for(size_t j=k;j<val[id].ele.size();j++){
B2[val[id].ele[k].fi][val[id].ele[j].fi][id]=0;
}
}
}else{
cout<<"Not found.\n";
}
}else if(OP[i]==3||OP[i]==4){
// return 0;
// if(OP[i]==3) assert(0);
int cbt;
int p=0;
int d=CN[i];
rep(j,0,d-1){
if(!atoid.count(SQ[i][j])){
cout<<"Invalid.\n";
goto ed;
}
tid[j]=atoid[SQ[i][j]];
}
sort(allt(tid,0,d));
if(d==1){
Bt=B2[tid[0]][tid[0]];
}else if(d==2){
Bt=B2[tid[0]][tid[1]];
}else if(d==3){
Bt=B2[tid[0]][tid[1]]&B2[tid[2]][tid[2]];
}else if(d==4){
Bt=B2[tid[0]][tid[1]]&B2[tid[2]][tid[3]];
}else{
Bt=B2[tid[0]][tid[1]]&B2[tid[2]][tid[3]]&B2[tid[4]][tid[4]];
}
cbt=Bt.count();
cout<<cbt<<" formula(s) are founded.\n";
cbt=min<int>(cbt,50);
rep(j,1,cbt){
p=Bt._Find_next(p);
dis[j]=p;
assert(p!=Bt.size());
cout<<j<<" -"<<val[p].name<<"("<<val[p].ms/10<<"."<<val[p].ms%10<<")\n";
}
if(OP[i]==3){
cout<<"Type a number:";
int d=DE[i];
// debug(d);
if(d>cbt||d<1){
cout<<"Invalid.\n";
}else{
cout<<"Successfully.\n";
int id=dis[d];
vis[id]=0;
for(size_t k=0;k<val[id].ele.size();k++){
tot[val[id].ele[k].fi]-=1;
}
as-=1;
for(size_t k=0;k<val[id].ele.size();k++){
for(size_t j=k;j<val[id].ele.size();j++){
B2[val[id].ele[k].fi][val[id].ele[j].fi][id]=0;
}
}
}
}
ed:;
}else if(OP[i]==5){
cout<<"In general:"<<as<<"\n";
rep(i,0,K){
if(tot[i]) cout<<S[i]<<":"<<tot[i]<<"\n";
}
}
}
}
C
主要算法(康托展开)
感觉没有我想得那么难,几位做了的大佬都看出来了,具体内容可以从这道模板题里看,在这里简单讲述一下什么是康托展开以及具体实现。
康托展开是一个全排列到一个自然数的双射,常用于构建哈希表时的空间压缩。康托展开的实质是计算当前排列在所有由小到大全排列中的顺序,因此是可逆的。康托展开公式为对于一个排列
其中
观察公式可以发现,排列的排名实际上是一个变进制数,具体的:
- 第
n 位的权值是0! (基数为 1) - 第
n-1 位的权值是1! (基数为 2) - ...
- 第 1 位的权值是
(n-1)! (基数为n )
因此,计算
具体实现
为了使用康托展开公式,我们要先对于排列中的每一个数
通过上面的操作,得到的两个序列
注意:最高位的进位直接舍弃(因为是
然后得到最终的
总体时间复杂度为
代码
50 分 O(n^2) 代码
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<bitset>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int N =1e5+6;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int cnt[N];
int c[N],a[N],b[N],A[N],B[N],ans[N];
int main() {
int n = read();
for(int i = 1;i <= n;++i) {
a[i] = read();
}
for(int i = 1;i <= n;++i) {
b[i] = read();
}
for(int i = 1;i <= n;++i) {
for(int j = i + 1;j <= n;++j) {
if(a[j] < a[i]) A[i]++;
if(b[j] < b[i]) B[i]++;
}
}
for(int i = 1;i <= n;++i) {
c[i] = A[i] + B[i];
}
for(int i = n - 1;i >= 1;--i) {
if(c[i] >= n - i + 1) {
c[i - 1] += c[i] / (n - i + 1);
c[i] %= n - i + 1;
}
}
for(int i = 1;i <= n;++i) {
int tmp = 0;
while(c[i] != -1) {
if(!cnt[tmp]) {
c[i]--;
}
++tmp;
}
cnt[tmp - 1] = 1;
ans[i] = tmp - 1;
}
for(int i = 1;i <= n;++i) {
printf("%d ",ans[i]);
}
return 0;
}
100 分 O(n\log n) 代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+6;
inline int read(){
int x=0,f=1;
char c=getchar();
while(c<'0' || c>'9'){
if(c=='-')
f=-1;
c=getchar();
}
while(c>='0' && c<='9'){
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int n=read(),c[N],a[N],b[N],t[N];
inline int lowbit(int x){return x&-x;}
inline void add(int x,int val){
for(;x<=n;x+=lowbit(x))
c[x]+=val;
}
inline int ask(int x){
int ans=0;
for(;x;x-=lowbit(x))
ans+=c[x];
return ans;
}
inline int sfind(int x){
int l=1,r=n,res=1;
while(l<=r){
int mid=(l+r)>>1;
if(ask(mid)>=x)
res=mid,r=mid-1;
else
l=mid+1;
}
return res;
}
int main(){
for(int i=1;i<=n;i++) a[i]=read();
for(int i=1;i<=n;i++) b[i]=read();
for(int i=1;i<=n;i++) add(i,1);
for(int i=1;i<=n;i++){
int x=ask(a[i]);
add(a[i]+1,-1);
a[i]=x;
}
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++) add(i,1);
for(int i=1;i<=n;i++){
int x=ask(b[i]);
add(b[i]+1,-1);
b[i]=x;
}
for(int i=n;i>=1;i--){
int mod=n-i+1;
t[i]+=a[i]+b[i];
if(i!=1) t[i-1]+=t[i]/mod;
t[i]%=mod;
}
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++) add(i,1);
for(int i=1;i<=n;i++){
int val=sfind(t[i]+1);
printf("%d ",val-1);
add(val,-1);
}
return 0;
}
D
本题最大的难度是发现它是图论。
我们看到每个限制条件是两首歌在相邻的节目,这种条件很大可能是建边来转化成图论问题。于是我们给每一个条件建双向边。
然后先判定是否有解。注意到有边相连的节点由于是相邻的节目,所以场次奇偶性一定不同,然后进行黑白染色看能否成功即可。
接着求最多的节目数量。由于路径上相邻两点的节目编号之差的绝对值为
另外原题保证了图连通,而本题并没有保证。注意到不同连通块之间完全不影响,我们将各连通块分别计算然后把答案加起来就行了。如果直接 copy 原题代码,只能得到至多
ll n,vis[2010],v[2010],dis[2010],ans,flg,mx;
ll u;
vector<ll> a[2010];
void work(ll now){
mx=0;//当前连通块内的答案
memset(v,0,sizeof(v));//记录本连通块内的点集
vis[now]=1;//1/2染色方案 0表示没染
v[now]=1;
queue<ll> q;
q.push(now);
while(!q.empty()){//BFS 进行染色
ll tmp=q.front();
q.pop();
for(int i=0;i<a[tmp].size();i++){
if(!vis[a[tmp][i]]){
vis[a[tmp][i]]=3-vis[tmp];
v[a[tmp][i]]=1;
q.push(a[tmp][i]);
}
else if(vis[a[tmp][i]]==vis[tmp]){
flg=1;
return ;
}
}
}
for(int i=1;i<=n;i++){//枚举每个点
if(!v[i])continue;
memset(dis,0,sizeof(dis));
dis[i]=1;
q.push(i);
while(!q.empty()){//BFS 求到各点的距离
ll tmp=q.front();
q.pop();
for(int i=0;i<a[tmp].size();i++){
if(!dis[a[tmp][i]]){
dis[a[tmp][i]]=dis[tmp]+1;
q.push(a[tmp][i]);
}
}
}
for(int j=1;j<=n;j++){//求最大值
mx=max(mx,dis[j]);
}
}
ans+=mx;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
ll tmp;
cin>>tmp;
while(tmp--){
cin>>u;
a[i].push_back(u);
a[u].push_back(i);
}
}
for(int i=1;i<=n;i++){
if(!vis[i])work(i);
if(flg){
cout<<-1;
return 0;
}
}
cout<<ans;
return 0;
}
E
#include<cstdio>
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
template<typename T>inline void write(T qwqx,char ch='\n'){
if(qwqx<0){qwqx=-qwqx;putchar('-');}
int qwqy=0;static char qwqz[40];
while(qwqx||!qwqy){qwqz[qwqy++]=qwqx%10+48;qwqx/=10;}
while(qwqy--){putchar(qwqz[qwqy]);}if(ch)putchar(ch);
}
int n,m,arr[20000005];
int main(){
scanf("%d",&n);
int mid=n+1;
rep(d,0,n-1){
int l=mid-d,r=mid+d;
if(d&1){
arr[++m]=r+1;
perrange(i,r-1,l-1,2)arr[++m]=i;
}else{
arr[++m]=l-1;
reprange(i,l+1,r+1,2)arr[++m]=i;
}
}
per(d,n,1){
int l=mid-d,r=mid+d;
if(d&1){
arr[++m]=r-1;
perrange(i,r-3,l+1,2)arr[++m]=i;
}else{
arr[++m]=l+1;
reprange(i,l+3,r-1,2)arr[++m]=i;
}
}
write(m);
rep(i,1,m)write(arr[i],32);
}
F
考虑
一般来说小学应该会讲到如何判定一个数能否被
注意到数的位数非常大(就像在题面中说的一样)所以盲猜一把和数的位置没啥关系,联系刚才
- 由于
k\equiv 1\pmod {(k-1)} ,k^2\equiv k(k-1)+k\equiv k \equiv 1\pmod{(k-1)} ,所以对于任意的n \in \mathbb{N} 我们有k^n\equiv\pmod{(k-1)} 。 - 因此,如果第
p\in\mathbb{N^+} 位上是a ,那么该位对整个数模(k-1) 的值的贡献就是ak^{p-1}\equiv a\pmod{(k-1)} 。 - 于是刚才的结论成立。
ll t,n,s,a,u,v,w,k;
int main(){
cin>>t;
while(t--){
cin>>n>>k;
s=0;
while(n--){
cin>>a>>u>>v>>w;
s+=a;
s%=k-1;
}
if(s)cout<<"No";
else cout<<"Yes";
cout<<endl;
}
return 0;
}
G
树剖换根 + 势能线段树。
核心思路
1. 线段树势能分析
对于 区间取模 和 区间开方,显然难以维护 Lazy Tag。但利用两个数学性质(证明略):
因此我们可以使用如下方式来剪枝:
维护区间最大值 max_val。
- 若 max_val < p (取模) 或 max_val <= 1 (开方),操作对该区间无意义,直接 Return。
- 否则,暴力递归到叶子修改。
势能分析证明复杂度均摊为
2. 换根操作
不需要LCT,只需假定
1,
2,
3,
- 找到
u \to rt 路径上紧贴u 的那个直系儿子v 。 - 操作范围 = 全树 挖掉
v 的物理子树。AC 代码
#include<bits/stdc++.h> using namespace std; const int MAXN = 100005; typedef long long ll;
int n, m; ll val[MAXN]; vector<int> adj[MAXN]; int root_now;
int fa[MAXN], dep[MAXN], siz[MAXN], son[MAXN]; int top[MAXN], dfn[MAXN], rnk[MAXN], timer;
struct Node { ll sum; ll max_val; } tree[MAXN<<2];
void dfs1(int u, int f) { fa[u] = f; dep[u] = dep[f] + 1; siz[u] = 1; son[u] = 0; for (int v : adj[u]) { if (v == f) continue; dfs1(v, u); siz[u] += siz[v]; if (siz[v] > siz[son[u]]) son[u] = v; } }
void dfs2(int u, int t) { top[u] = t; dfn[u] = ++timer; rnk[timer] = u; if (!son[u]) return; dfs2(son[u], t); for (int v : adj[u]) { if (v != fa[u] && v != son[u]) dfs2(v, v); } }
void pushup(int p) { tree[p].sum = tree[p<<1].sum + tree[p<<1|1].sum; tree[p].max_val = max(tree[p<<1].max_val, tree[p<<1|1].max_val); }
void build(int p, int l, int r) { if (l == r) { tree[p].sum = val[rnk[l]]; tree[p].max_val = val[rnk[l]]; return; } int mid = (l + r) >> 1; build(p<<1, l, mid); build(p<<1|1, mid + 1, r); pushup(p); }
// 区间取模 void modify_mod(int p, int l, int r, int ql, int qr, int x) { if (tree[p].max_val < x) return; if (l == r) { tree[p].sum %= x; tree[p].max_val = tree[p].sum; return; } int mid = (l + r) >> 1; if (ql <= mid) modify_mod(p<<1, l, mid, ql, qr, x); if (qr > mid) modify_mod(p<<1|1, mid + 1, r, ql, qr, x); pushup(p); }
// 区间开方 void modify_sqrt(int p, int l, int r, int ql, int qr) { if (tree[p].max_val <= 1) return; if (l == r) { tree[p].sum = (ll)sqrt(tree[p].sum); tree[p].max_val = tree[p].sum; return; } int mid = (l + r) >> 1; if (ql <= mid) modify_sqrt(p<<1, l, mid, ql, qr); if (qr > mid) modify_sqrt(p<<1|1, mid + 1, r, ql, qr); pushup(p); }
ll query(int p, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return tree[p].sum; int mid = (l + r) >> 1; ll res = 0; if (ql <= mid) res += query(p<<1, l, mid, ql, qr); if (qr > mid) res += query(p<<1|1, mid + 1, r, ql, qr); return res; }
void path_mod(int u, int v, int x) { while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) swap(u, v); modify_mod(1, 1, n, dfn[top[u]], dfn[u], x); u = fa[top[u]]; } if (dep[u] > dep[v]) swap(u, v); modify_mod(1, 1, n, dfn[u], dfn[v], x); }
int get_lca(int u, int v) { while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) swap(u, v); u = fa[top[u]]; } return dep[u] < dep[v] ? u : v; }
int find_child(int u, int root_node) { int res = root_node; while (top[res] != top[u]) { if (fa[top[res]] == u) return top[res]; res = fa[top[res]]; } return son[u]; }
void handle_subtree(int u, int type, int mod_val = 0) { if (u == root_now) { if (type == 1) modify_sqrt(1, 1, n, 1, n); else cout << query(1, 1, n, 1, n) << endl; return; }
int lca = get_lca(u, root_now);
if (lca == u) {
int v = find_child(u, root_now);
if (type == 1) {
if (dfn[v] > 1) modify_sqrt(1, 1, n, 1, dfn[v] - 1);
if (dfn[v] + siz[v] <= n) modify_sqrt(1, 1, n, dfn[v] + siz[v], n);
} else {
ll ans = 0;
if (dfn[v] > 1) ans += query(1, 1, n, 1, dfn[v] - 1);
if (dfn[v] + siz[v] <= n) ans += query(1, 1, n, dfn[v] + siz[v], n);
cout << ans << endl;
}
} else {
if (type == 1) {
modify_sqrt(1, 1, n, dfn[u], dfn[u] + siz[u] - 1);
} else {
cout << query(1, 1, n, dfn[u], dfn[u] + siz[u] - 1) << endl;
}
}
}
int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> val[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cin >> root_now;
dfs1(1, 0);
dfs2(1, 1);
build(1, 1, n);
while (m--) {
int opt;
cin >> opt;
if (opt == 1) {
int id;
cin >> id;
root_now = id;
} else if (opt == 2) {
int u, v, x;
cin >> u >> v >> x;
path_mod(u, v, x);
} else if (opt == 3) {
int id;
cin >> id;
handle_subtree(id, 1);
} else if (opt == 4) {
int id;
cin >> id;
handle_subtree(id, 2);
}
}
return 0;
}
# H
设 $n \ge m$。
$$E(x) = \frac{(n-m)^2}{\binom{n+m}{n}} \sum_{k=0}^{m} \frac{4^k}{n+m-2k} \binom{n+m-2k}{n-k}$$
# I
```cpp
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<functional>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<array>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0)
#define OTIE cout.tie(0)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define il inline
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof x)
#define popc __builtin_popcountll
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) ((x)&-(x))
#define lson(x) ((x)<<1)
#define rson(x) ((x)<<1|1)
//#define double long double
#define int long long
// #define int __int128
using namespace std;
using i64=long long;
using u64=unsigned long long;
using pii=pair<int,int>;
template<typename T1,typename T2>inline void ckmx(T1 &x,T2 y){x=x>y?x:y;}
template<typename T1,typename T2>inline void ckmn(T1 &x,T2 y){x=x<y?x:y;}
inline auto rd(){
int qwqx=0,qwqf=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')qwqf=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){qwqx=(qwqx<<1)+(qwqx<<3)+ch-48;ch=getchar();}return qwqx*qwqf;
}
template<typename T>inline void write(T qwqx,char ch='\n'){
if(qwqx<0){qwqx=-qwqx;putchar('-');}
int qwqy=0;static char qwqz[40];
while(qwqx||!qwqy){qwqz[qwqy++]=qwqx%10+48;qwqx/=10;}
while(qwqy--)putchar(qwqz[qwqy]);if(ch)putchar(ch);
}
bool Mbg;
const int maxn=5e5+5,inf=0x3f3f3f3f;
const i64 mod=1e9+7,inv6=166666668;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int QwQ,n,Q;
i64 a[maxn];
#define mid ((l+r)>>1)
il i64 cal(i64 x){
return (x*(x+1)/2)%mod;
}
il i64 cal2(i64 x){
return x*(x+1)%mod*(2*x+1)%mod*inv6%mod;
}
il i64 cal3(i64 x){
return cal(x)*cal(x)%mod;
}
class sgt1{
private:
struct node{
//ansi : \sum_{[l',r']<=[l,r]} sum*(r'-l'+1)
i64 sum,sum2,pre,pre2,prei,sec,sec2,seci,ansi,ans2;
int len;
node(){sum=pre=sec=sum2=pre2=sec2=ans2=prei=seci=ansi=len=0;}
il friend node operator+(node x,node y){
node res;
res.len=x.len+y.len;
res.sum=(x.sum+y.sum)%mod;
res.sum2=(x.sum2+y.sum2+2*x.sum*y.sum%mod)%mod;
res.pre=(x.pre+y.pre+y.len*x.sum%mod)%mod;
res.pre2=(x.pre2+y.pre2+y.len*x.sum2%mod+2*x.sum*y.pre%mod)%mod;
res.prei=(x.prei+y.prei+x.len*y.len%mod*x.sum%mod+x.len*y.pre%mod+x.sum*cal(y.len)%mod)%mod;
res.sec=(x.sec+y.sec+x.len*y.sum%mod)%mod;
res.sec2=(x.sec2+y.sec2+x.len*y.sum2%mod+2*y.sum*x.sec%mod)%mod;
res.seci=(x.seci+y.seci+x.len*y.len%mod*y.sum%mod+y.len*x.sec%mod+y.sum*cal(x.len)%mod)%mod;
res.ansi=(x.ansi+y.ansi+x.sec*cal(y.len)%mod+y.len*x.seci%mod+y.pre*cal(x.len)%mod+x.len*y.prei%mod)%mod;
res.ans2=(x.ans2+y.ans2+y.len*x.sec2%mod+2*x.sec*y.pre%mod+x.len*y.pre2%mod)%mod;
return res;
}
il friend node operator+(node x,i64 y){
node res;
const i64 z=y*x.len%mod,sqry=y*y%mod;
res.len=x.len;
res.sum=(x.sum+z)%mod;
res.sum2=(x.sum2+z*z%mod+2*z*x.sum%mod)%mod;
res.pre=(x.pre+cal(x.len)*y%mod)%mod;
res.pre2=(x.pre2+2*y*x.prei%mod+cal2(x.len)*sqry%mod)%mod;
res.prei=(x.prei+cal2(x.len)*y%mod)%mod;
res.sec=(x.sec+cal(x.len)*y%mod)%mod;
res.sec2=(x.sec2+2*y*x.seci%mod+cal2(x.len)*sqry%mod)%mod;
res.seci=(x.seci+cal2(x.len)*y%mod)%mod;
res.ansi=(x.ansi+y*(x.len+1)%mod*cal2(x.len)%mod-y*cal3(x.len)%mod+mod)%mod;
res.ans2=(x.ans2+2*y*x.ansi%mod+sqry*(x.len+1)%mod*cal2(x.len)%mod-sqry*cal3(x.len)%mod+mod)%mod;
return res;
}
}d[maxn<<2];
int tag[maxn<<2];
il void pu(int p){
d[p]=d[lson(p)]+d[rson(p)];
}
il void pt(int p,int v){
d[p]=d[p]+v,tag[p]=(tag[p]+v)%mod;
}
il void pd(int p){
if(tag[p]){
pt(lson(p),tag[p]),pt(rson(p),tag[p]);
tag[p]=0;
}
}
public:
void bd(int l=1,int r=n,int p=1){
if(l==r){
d[p].len=1;
d[p].sum=d[p].pre=d[p].sec=d[p].prei=d[p].seci=d[p].ansi=a[l];
d[p].sum2=d[p].pre2=d[p].sec2=d[p].ans2=a[l]*a[l]%mod;
return;
}
bd(l,mid,lson(p)),bd(mid+1,r,rson(p)),pu(p);
}
il void upd(int ll,int rr,int v,int l=1,int r=n,int p=1){
if(ll<=l&&r<=rr)return pt(p,v);
pd(p);
if(ll<=mid)upd(ll,rr,v,l,mid,lson(p));
if(rr>mid)upd(ll,rr,v,mid+1,r,rson(p));
pu(p);
}
il node qry(int ll,int rr,int l=1,int r=n,int p=1){
if(ll<=l&&r<=rr)return d[p];
pd(p);
if(rr<=mid)return qry(ll,rr,l,mid,lson(p));
if(ll>mid)return qry(ll,rr,mid+1,r,rson(p));
return qry(ll,rr,l,mid,lson(p))+qry(ll,rr,mid+1,r,rson(p));
}
} S;
#undef mid
inline void solve_the_problem(){
n=rd(),Q=rd();
rep(i,1,n)a[i]=rd();
S.bd();
while(Q--){
int op=rd(),l=rd(),r=rd(),x;
if(l>r)swap(l,r);
if(op==1){
x=rd();
S.upd(l,r,x);
}else if(op==2){
write(S.qry(l,r).ansi);
}else{
write(S.qry(l,r).ans2);
}
}
}
bool Med;
signed main(){
// freopen("b.in","r",stdin);freopen("b.out","w",stdout);
// freopen("302.in","r",stdin);
fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
int _=1;
while(_--)solve_the_problem();
}
/*
*/
J
Solution by @JuRuoOIer
注意到我们是每打一个人就可以换一次,那我们考虑对每个人量身定制,也就是打每个人最菜的两项,由于要求严格大于,所以此时我们总的等级要大于这个人最菜的两项的等级加
但是如何优化到
注意到 a)维护最菜的两项的等级加
接着对这个桶求个前缀和,此时 a[i] 就代表 a[i] 个人。然后对于每个人的三项之和可以
代码:
直接贴的当时的代码,码风可能稍有不同。
int a[9000010],s[3000010],mx,n,x,y,z,tsum;
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>x>>y>>z;
s[i]=x+y+z;
if(x>=y&&x>=z){
a[y+z+2]++;
}
else if(y>=x&&y>=z){
a[x+z+2]++;
}
else{
a[x+y+2]++;
}
}
for(int i=0;i<9000010;i++){
tsum+=a[i];
a[i]=tsum;
}
for(int i=0;i<n;i++){
cout<<a[s[i]]<<endl;
}
return 0;
}
K
考虑一个不带修改的版本。我们在树上做最大权匹配,使用经典的树形 DP。设