跳到正文

KJun

1371632

Why bother? What's in it for you?

在洛谷打开
资料、奖项与版本

资料

编号
1371632
抓取

你好鸭!\color{orange}你好鸭!

你是第\color{orange}{\textit{\textbf{你是第}}} 来到这里的人wa!\color{orange}{\textit{\textbf{来到这里的人wa!}}}

1371632的洛谷用户卡片 随机图片,刷新试试!\color{red}{\text{\textbf{随机图片,刷新试试!}}}

我是一只超级无敌菜的蒟蒻awa

AC=Answer Coarse=粗劣地回答\color{red}{\text{AC=Answer Coarse=粗劣地回答}}
WA=Wonderful Answer=完美的答案\color{orange}{\text{WA=Wonderful Answer=完美的答案}}
TLE=Time Limit Enough=时间充裕\color{yellow}{\text{TLE=Time Limit Enough=时间充裕}}
MLE=Memory Limit Enough=内存充裕\color{green}{\text{MLE=Memory Limit Enough=内存充裕}}
CE=Compile Easily=轻松通过编译\color{cyan}{\text{CE=Compile Easily=轻松通过编译}}
RE=Run excellently=完美运行\color{blue}{\text{RE=Run excellently=完美运行}}
UKE=Unbelievably Keep Enough Score=难以置信地保持分数\color{purple}{\text{UKE=Unbelievably Keep Enough Score=难以置信地保持分数}}
AU=All Unaccepted=全都不正确\color{pink}{\text{AU=All Unaccepted=全都不正确}}
IOI=I’m Only Ignorant=我只是无知\color{grey}{\text{IOI=I'm Only Ignorant=我只是无知}}

下面是一些代码的模版(未完待续)

快速幂模版

C++
long long binpow(long long a,long long b){
	long long res=1;
	while(b>0){
		if(b&1) res=res*a;
		a=a*a;
		b>>=1;
	}
	return res;
}

最大公约数与最小公倍数模版

C++
int gcd(int a,int b){
	if(b==0) return a;
	return gcd(b,a%b);
}
int lcm(int a,int b){
	return a*b/gcd(a,b);
}

Dijkstra算法(朴素版)

C++
#include <bits/stdc++.h>
using namespace std;
const int maxn = 11450;
int n,m,s;
struct Edge{
	int v,w;
};

vector<Edge> e[maxn];

void addedge(int u,int v,int w){
	Edge edge;
	edge.v=v;
	edge.w=w;
	e[u].push_back(edge);
};
const int INF = INT_MAX;
int d[maxn];
bool done[maxn];
void dijkstra(){
	for(int i=0;i<=n;i++){
		d[i]=INF;
	}

	d[1]=0;
	for(int i=1;i<=n;i++){
        int u=0;
		for(int j=1;j<=n;j++){
			if(!done[j]&&d[u]>d[j]) u=j;
		}
		for(Edge edge:e[u]){
			int v=edge.v;
			int w=edge.w;
			d[v]=min(
				d[v],
				d[u]+w
				);
		}
		done[u]=1;
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m>>s;
	while(m--){
		int u,v,w;
		cin>>u>>v>>w;
		addedge(u,v,w);
	}
	dijkstra();
	for(int i=1;i<=n;i++){
		cout<<d[i]<<" ";
	}
}

Dijkstra算法(堆优化)

C++
#include <bits/stdc++.h>
using namespace std;
int n,m,s;
const int maxn=114510;
struct Edge{
	int v,w;
};
bool operator < (Edge a,Edge b){
	return a.w > b.w;
}
vector<Edge> e[maxn];
void addEdge(int u,int v,int w){
	Edge edge;
	edge.v=v;
	edge.w=w;
	e[u].push_back(edge);
}
const int inf = INT_MAX;//0x7fffffff
int d[maxn];
bool done[maxn];
priority_queue<Edge> pq;
void dijkstra(){
	memset(done,0,sizeof(done));
	for(int i=0;i<=n;i++){
		d[i]=inf;
	}
	d[s]=0;
	Edge edge={s,0};
	pq.push(edge);
	while(!pq.empty()){
		Edge edge=pq.top();
		pq.pop();
		int u=edge.v;
		if(done[u]) continue;
		for(int i=0;i<e[u].size();i++){
			Edge edge=e[u][i];
			int v=edge.v,w=edge.w;
			d[v]=min(d[v],d[u]+w);
			edge.v=v;
			edge.w=d[u]+w;
			pq.push(edge);
		}
		done[u]=1;
	}
}
int main(){
	cin>>n>>m>>s;
	while(m--){
		int u,v,w;
		cin>>u>>v>>w;
		addEdge(u,v,w);
	}
	dijkstra();
	for(int i=1;i<=n;i++){
		cout<<d[i]<<" ";
	}
}

Prim朴素版

C++
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000 + 10;
int n,m;
struct Edge{
    int u,v,w;
};
vector<Edge> edges;
void addedge(int x,int y,int z){
    Edge edge;
    edge.u=x,edge.v=y,edge.w=z;
    edges.push_back(edge);
}
bool vis[maxn];
int prim(int scr){
    int ans=0;
    vis[1]=1;
    for(int i=1;i<=n-1;i++){
        int new_v=0,min_w=11451410;
        for(Edge edge:edges){
            if(vis[edge.u]+vis[edge.v]==1){
                if(edge.w<min_w){
                    min_w=edge.w;
                    if(!vis[edge.u]) new_v=edge.u;
                    else new_v=edge.v;
                }
            }
        }
        if(new_v==0) return -1;
        vis[new_v]=1;
        ans+=min_w;
    }
    return ans;
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int x,y,z;
        cin>>x>>y>>z;
        addedge(x,y,z);
    }
    int ans=prim(1);
    if(ans!=-1) cout<<ans;
    else cout<<"orz";
}

并查集(递归与路径压缩)

C++
int find(int x){
	if(father[x]==x) return x;
	else{
		father[x] = find(father[x]);
	}
	return father[x];
}
void add(int x,int y){
	father[find(y)]=find(x);
}

kruskal算法(并查集)

C++
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)2e5+10;
struct Edge{
	int u,v,w;
};
long long n,m,fa[5000+10];
long long ans=0;
Edge edges[maxn];
void addedge(int x,int y,int z,int cnt){
	edges[cnt].u=x,edges[cnt].v=y,edges[cnt].w=z;
}
bool cmp(Edge a,Edge b){
	return a.w<b.w;
}
int find(int x){
	if(fa[x]==x) return x;
	else return fa[x]=find(fa[x]);
}
void join(int x){
	ans+=edges[x].w;
	fa[find(edges[x].u)] = find(edges[x].v);
}
void Kruskal(){
	int cnt = 0;
	sort(edges+1,edges+m+1,cmp);
	for(int i=1;i<=n;i++) fa[i]=i;
	for(int i=1;i<=m;i++){
		if(cnt==n-1) break;
		if(find(edges[i].u)==find(edges[i].v)) continue;
		join(i);
		cnt++;
	}
	if(cnt==n-1)cout<<ans;
    else cout<<"orz";
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int x,y,z;
		cin>>x>>y>>z;
		addedge(x,y,z,i);
	}
	Kruskal();
}

Bigint类模板

C++
struct Bigint{
	int len,a[maxn];
	Bigint(int x=0){
		memset(a,0,sizeof(a));
		for(len=1;x;len++){
			a[len]=x%10,x/=10;
		}
		len--;
	}
	int &operator[] (int i){
		return a[i];
	}
	void flatten(int L){
		len = L;
		for(int i=1;i<=len;i++){
			a[i+1]+=a[i]/10;
			a[i]%=10;
		}
		for(;!a[len];) len--;
	}
	void print(){
		for(int i=max(len,1);i>=1;i--){
			cout<<a[i];
		}
	} 
};
Bigint operator+(Bigint a,Bigint b){
	Bigint c;
	int len = max(a.len,b.len);
	for(int i=1;i<=len;i++){
		c[i]+=a[i]+b[i];
	}
	c.flatten(len+1);
	return c;
}
Bigint operator*(Bigint a,int b){
	Bigint c;
	int len = a.len;
	for(int i=1;i<=len;i++){
		c[i]=a[i]*b;
	}
	c.flatten(len+11);
	return c;
}

线段树1(区间查询与加法修改)

C++
#include <bits/stdc++.h>
#define _for(i, a, b) for(ll i = (a); i <= (b); i++)
#define rep(i, a, b) for(ll i = (a); i < (b); i++)
#define lc p << 1
#define rc p << 1 | 1
using namespace std;
//Segment Tree1
typedef long long ll;
const int maxn = 100005;
ll n, m, w[maxn];
struct node {
    ll l, r, sum, add;
} tr[maxn * 4];
void pushup(ll p) {
    tr[p].sum = tr[lc].sum + tr[rc].sum;
}
void pushdown(ll p) {
    if(tr[p].add) {
        tr[lc].sum += tr[p].add * (tr[lc].r - tr[lc].l + 1);
        tr[rc].sum += tr[p].add * (tr[rc].r - tr[rc].l + 1);
        tr[lc].add += tr[p].add;
        tr[rc].add += tr[p].add;
        tr[p].add = 0;
    }
}
void build(ll p, ll l, ll r) {
    tr[p] = {l, r, w[l], 0};
    if(l == r) return;
    ll m = l + r >> 1;
    build(lc, l, m);
    build(rc, m + 1, r);
    pushup(p);
}
ll query(ll p, ll x, ll y) {
    if(x <= tr[p].l && tr[p].r <= y) 
        return tr[p].sum;
    ll m = tr[p].l + tr[p].r >> 1;
    pushdown(p);
    ll sum = 0;
    if(x <= m) sum += query(lc, x, y);
    if(y > m) sum += query(rc, x, y);
    return sum;
}
void update(ll p, ll x, ll y, ll k) {
    if(x <= tr[p].l && tr[p].r <= y) {
        tr[p].sum += (tr[p].r - tr[p].l + 1) * k;
        tr[p].add += k;
        return;
    }
    ll m = tr[p].l + tr[p].r >> 1;
    pushdown(p);
    if(x <= m) update(lc, x, y, k);
    if(y > m) update(rc, x, y, k);
    pushup(p);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m;
    _for(i, 1, n) {
        cin >> w[i];
    }
    build(1, 1, n);
    _for(i, 1, m) {
        ll opt, x, y, k;
        cin >> opt;
        switch (opt)
        {
        case 1:
            cin >> x >> y >> k;
            update(1, x, y, k);
            break;
        case 2:
            cin >> x >> y;
            cout << query(1, x, y) << endl;
            break;
        }
    }
    return 0;
}

线段树2(区间查询与乘法修改)

C++
#include <bits/stdc++.h>
#define _for(i, a, b) for(ll i = (a); i <= (b); i++)
#define rep(i, a, b) for(ll i = (a); i < (b); i++)
#define lc u << 1
#define rc u << 1 | 1
using namespace std;
//Segment Tree2
typedef long long ll;
const int maxn = 100005;
ll n, q, w[maxn], p;
struct Tree {
    ll l, r, sum, mul, add;
} tr[maxn * 4];
void pushup(ll u) {
    tr[u].sum = (tr[lc].sum + tr[rc].sum) % p;
}
void calc(Tree &t, ll m, ll a) {
    t.sum = (t.sum * m + (t.r - t.l + 1) * a) % p;
    t.mul = (t.mul * m) % p;
    t.add = (t.add * m + a) % p;
}
void pushdown(ll u) {
    calc(tr[lc], tr[u].mul, tr[u].add);
    calc(tr[rc], tr[u].mul, tr[u].add);
    tr[u].add = 0;
    tr[u].mul = 1;

}
void build(ll u, ll l, ll r) {
    tr[u] = {l, r, w[l], 1, 0};
    if(l == r) return;
    ll m = l + r >> 1;
    build(lc, l, m);
    build(rc, m + 1, r);
    pushup(u);
}
ll query(ll u, ll x, ll y) {
    if(x <= tr[u].l && tr[u].r <= y) 
        return tr[u].sum;
    ll m = tr[u].l + tr[u].r >> 1;
    pushdown(u);
    ll sum = 0;
    if(x <= m) sum += query(lc, x, y);
    if(y > m) sum += query(rc, x, y);
    return sum;
}
void update(ll u, ll l, ll r, ll m, ll a) {
    if(l > tr[u].r or  r < tr[u].l) return;
    if(l <= tr[u].l and tr[u].r <= r) {
        calc(tr[u], m, a);
        return;
    }
    pushdown(u);
    update(lc, l, r, m, a);
    update(rc, l, r, m, a);
    pushup(u);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> q >> p;
    _for(i, 1, n) {
        cin >> w[i];
    }
    build(1, 1, n);
    _for(i, 1, q) {
        ll opt, x, y, k;
        cin >> opt;
        switch (opt)
        {
        case 1:
            cin >> x >> y >> k;
            update(1, x, y, k, 0);
            break;
        case 2:
            cin >> x >> y >> k;
            update(1, x, y, 1, k);
            break;
        case 3:
            cin >> x >> y;
            cout << query(1, x, y) % p << endl;
            break;
        }
    }
    return 0;
}

重链剖分

https://www.luogu.com.cn/paste/12n6d5ne

共 6 篇
  1. 题解

    【GDOI2008】彩球游戏

    【GDOI2008】彩球游戏*colball* 注意:本题解仅通过随机数据测试,请谨慎甄别 先讲讲题外话,这道题不管是从哪一方面都非常有思维难度。AI给出的难度评级为*提高+/省选-*,我认为应该为上位蓝或者下位紫,很难!真的很难!赛时最高分该题为10pts,这个出题人绝对是天才…

  2. 题解

    P1600 [NOIP 2016 提高组] 天天爱跑步

    本文是对洛谷 P1600《天天爱跑步》(NOIP 2016 提高组)的题解总结。 题目大意:给定一棵树,多名玩家从起点沿最短路移动到终点,每秒移动一条边。每个节点有一个观测时间,若玩家经过该节点的时刻等于观测时间,则计入人数。需输出每个节点的统计结果。 核心算法:树上差分 + 线段树合并。 将路径拆分为“向上走”和“向下走”两段,根据深度差值转化为线段树上的区间查询问题。 为每个节点维护一棵权值线段树,记录路径上出现的深度信息。 通过 DFS 自底向上合并子树的线段树,并在合并过程中查询满足特定深度条件的节点数量。 代码实现:提供了完整的 C++ 代码,包含树链剖分求 LCA、动态开点线段树的单点修改、合并及查询操作。

    1 赞
  3. 题解

    P3960 [NOIP 2017 提高组] 列队

    该题解针对NOIP 2017提高组列队问题,提出了一种基于线段树与队列结合的高效算法。核心思路是通过动态开点线段树维护每行和最后一列的空位信息,避免暴力模拟。具体方法为:在每行和最后一列分别建立线段树,记录当前空位数量;同时用队列存储新进入和出队的元素。当处理离队操作时,通过线段树快速查询目标位置的行或列的空位下标,再根据是否在最后一列决定使用原队列还是新队列。最终将离队元素插入到(n, m)位置,并输出其编号。该方法将时间复杂度优化至O(n log n),通过空间换时间策略高效解决大规模数据问题。

    1 赞
  4. 题解

    P2680 [NOIP 2015 提高组] 运输计划题解

    该题解针对洛谷P2680运输计划问题,核心思路是通过二分答案结合树上差分技术寻找最优边修改方案。具体步骤如下: 问题转化:将问题转化为寻找一条被所有运输路径覆盖的边,将其权值改为0后,使最长运输路径(即两点间距离)最小。 关键公式:利用LCA算法计算两点间距离,公式为dist(u,v) = weight[u] + weight[v] - 2*weight[lca(u,v)],其中weight数组存储根节点到各节点的距离。 树上差分:通过差分数组标记运输路径覆盖情况: 在起点和终点分别+1 在LCA节点-2 通过DFS传递标记,统计每条边的覆盖次数 二分查找:设定运输时间的二分范围[l, r],每次检查当前候选时间x是否可行: 若存在运输路径长度超过x,则标记这些路径的起点、终点和LCA 找出覆盖次数等于总违规路径数的边,选择权值最大的边进行修改 判断修改后最长路径是否≤x 算法实现: 使用树链剖分预处理LCA信息 通过三次DFS完成树结构分解、重链划分和距离计算 二分查找过程中结合差分数组快速判断可行性 该方法通过二分答案降低时间复杂度,利用树上差分高效统计边覆盖次数,最终选择权值最大的覆盖边进行修改,以达到最优解。

    1 赞
  5. 题解

    题解:P2038 [NOIP2014 提高组] 无线网络发射器选址

    该代码旨在解决在一个128×128的网格中放置网络发射器以最大化覆盖的公共场所数量的问题。其核心逻辑如下: 输入处理:使用二维数组r存储网格中各点的公共场所数量,通过循环读取输入数据并填充。 覆盖计算:对每个网格点,遍历其以自身为中心、边长为2d的正方形区域(即横纵坐标范围为i-d到i+d),若坐标越界则忽略,否则累加区域内公共场所的值,结果存入数组a。 统计结果:遍历数组a,记录最大覆盖数量ans及对应的方案数ans2。若当前值大于ans则更新最大值并重置方案数;若等于则方案数加一。 最终输出最大覆盖数量及实现该数量的放置方案总数。

    2 赞
  6. 题解

    题解:P13729 【MGVOI R1-A】超级奇数(odd)

    P13729 超级奇数题解 题目描述 原题链接\ 简单来说就是给定一个数 a ,然后找到一个数 b ,使 a + b 满足十进制位下每一位的数都是奇数。 (赛时)解题思路 由于这是 T1 ,所以我以为这是签到题。但是不然,一看数据范围 a ≤ 10^12 ,直接枚举然后判断是否为…