跳到正文

算法·理论

复盘

Vodyanitsaa约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年11月20日
收录
2026年11月25日
更新
2026年11月25日
篇幅
约 8 分钟

版本

  1. v1当前

11/16

听了 jxj 讲数学。怎么这么难,具体可以看这篇笔记。

11/17

等会写。

11/23

https://qdezoj.cpolar.cn/d/tigao/contest/6b02fb2c238f2366bb435789

http://47.113.203.55/contest/6aff9e9d973eae2699628565

travel

cargo

recall

11/24

https://qdezoj.cpolar.cn/d/tigao/contest/6b0400dc238f2366bb4739bc

minsparse

从高位往低位贪,如果 ll 和 rr 的这一位相同那么就不能动,直到遇到不同的一位,此时一定 rr 这一位是 11,ll 这一位是 00,记这一位前的 11 的个数是 cc。后面展开分讨。

  • 如果 ll 后面有 00 个 11:那么保留前面所有的 11,后面不添加 11 即是最优解,那么最大最小值都是 ll。

  • 如果 ll 后面有 11 个 11:最小的 11 的个数即是 c+1c+1,那么把小答案后面的 11 放在原位,大答案取不一样的那一位 11 即可。

  • 如果 ll 后面有多个 11:大答案的处理同上,小答案取后面第一个 11 的前一位为 11 可以保证 11 的个数最少并且是小答案。

code
C++
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define debug(x) cerr<<"debug "<<__LINE__<<":"<<#x<<" = "<<x<<"\n"
#define For(i,a,b) for(int i = a;i<=b;i++)
#define Rof(i,a,b) for(int i = a;i>=b;i--)
inline 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 f*x;}
inline void write(ll x){if(x<0){putchar('-');x=-x;}if(x>9)write(x/10);putchar(x%10+'0');}
using namespace std;
int main(){
	freopen("minsparse.in","r",stdin);
	freopen("minsparse.out","w",stdout);
	//std::ios::sync_with_stdio(false);
	//std::cin.tie(nullptr);
	int q = read();
	while(q--){
		ll l = read(),r = read();
		ll pos = 60;
		while(pos>=0 && (l&(1ll<<pos)) == (r&(1ll<<pos))) pos--;
		ll cnt = 0,num;
		Rof(i,pos-1,0){
			if((1ll<<i)&l){
				cnt++;
				if(cnt == 1) num = i+1;
			}
		}
		if(cnt == 0){
			write(l);
			putchar(' ');
			write(l);
			putchar('\n');
		}
		else if(cnt == 1){
			write(l);
			ll ans = 0;
			//pos-1~0全部置为0 
			For(i,pos,60){
				if((1ll<<i)&r) ans |= (1ll<<i);
			} 
			putchar(' ');
			write(ans);
			putchar('\n');
		}
		else{
			ll ans = 0,res = 0;
			For(i,pos,60){
				if((1ll<<i)&r) ans |= (1ll<<i);
			}
			For(i,pos+1,60) if((1ll<<i)&l) res|=(1ll<<i);
			res|=(1ll<<(num));
			write(res);
			putchar(' ');
			write(ans);
			putchar('\n');
		}
	}
	return 0;
}

P12780 [ICPC 2024 Yokohama R] Omnes Viae Yokohamam Ducunt?

https://qdezoj.cpolar.cn/d/tigao/p/T7015

怎么还有 road?!

对于一个点 pp,它连向它的子树的边一定会在它子树中所有的点上算一遍贡献。所以求出 11 号点到所有点的最短路,每个点的贡献就是最短路长度乘上边权,答案就是 ∑i=2ndisi×ai\sum\limits_{i = 2}^{n} dis_i \times a_i(EZOJ题面)。

code
C++
#include<bits/stdc++.h>
#define ll long long
#define int long long
#define pii pair<int,int>
#define debug(x) cerr<<"debug "<<__LINE__<<":"<<#x<<" = "<<x<<"\n"
#define For(i,a,b) for(int i = a;i<=b;i++)
#define Rof(i,a,b) for(int i = a;i>=b;i--)
inline 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 f*x;}
inline void write(ll x){if(x<0){putchar('-');x=-x;}if(x>9)write(x/10);putchar(x%10+'0');}
using namespace std;
struct edge{
	int v,w;
};
struct node{
	int dis,u;
	bool operator>(const node &a) const {return dis>a.dis;}
};
vector<edge> g[100005];
int dis[100005],a[100005];
bool vis[100005];
priority_queue<node,vector<node>,greater<node>> q;
void dij(int s){
	memset(dis,0x3f,sizeof(dis));
	dis[s] = 0;
	q.push({0,s});
	while(!q.empty()){
		int u = q.top().u;
		q.pop();
		if(vis[u]) continue;
		vis[u] = 1;
		for(auto it:g[u]){
			int v = it.v,w = it.w;
			if(dis[v]>dis[u]+w){
				dis[v] = dis[u]+w;
				q.push({dis[v],v});
			}
		}
	}
}
signed main(){
	//freopen("road.in","r",stdin);
	//freopen("road.out","w",stdout);
	//std::ios::sync_with_stdio(false);
	//std::cin.tie(nullptr);
	int n = read(),m = read();
	For(i,1,n){
		a[i] = read();
	}
	For(i,1,m){
		int u = read(),v = read(),w = read();
		g[u].push_back({v,w});
		g[v].push_back({u,w});
	}
	dij(1);
	ll ans = 0;
	For(i,1,n){
		ans+=dis[i]*a[i];
	}
	write(ans);
	return 0;
}

P12786 [ICPC 2024 Yokohama R] Greatest of the Greatest Common Divisors

https://qdezoj.cpolar.cn/d/tigao/p/T7016

这里:https://www.luogu.com.cn/article/t4ge0fob

原文