跳到正文

题解

题解:P1001 A+B Problem

Moth约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
题目
P1001

版本

  1. v2当前
  2. v1

写在前面

这是一篇恶搞题解,如果你是oier新手,请移步新手村查看新手友好代码


这是一道神圣的题目

无论基础的红题,还是地狱级黑题,都由这里发源

所以,为了避免亵渎这道神圣的题目,我们应该使用神圣的数据结构来解决这道题目

初看这道题,相信大家和我一样,毫无头绪,所以我们可以转换一步

如果我们将 a,b 设为点 A 到点 B,点 B 到点 C的线段上的权值,那么,我们会发现,这道题的答案就是所有区间线段 (好吧,总共也就两根) 的权值之和

所以,问题就转换为:给定线段权值,求线段权值和,而这种区间染色问题,就可以使用 ODT 珂朵莉树 来解决

在珂朵莉树上面对两个长度为 x (方便起见,令 x 为一)进行染色,颜色为 a 和 b ,最后求一遍权值和即可

代码如下:

C++
//Moth
#include <iostream>
#include <set>

#define Iter set<Node>::iterator
#define int long long

using namespace std;

struct Node{
	int l,r;
	mutable int v;
	Node(){}
	Node(int a,int b=-1,int c=0):l(a),r(b),v(c){}
	bool operator <(const Node &a)const{
		return l<a.l;
	}
};
set<Node> odt;
int a,b;

Iter split(int x){
	auto it=odt.lower_bound(Node(x));
	if(it!=odt.end()&&it->l==x)return it;
    it--;
	int l=it->l,r=it->r,v=it->v;
	odt.erase(it);
	odt.emplace(Node(l,x-1,v));
	return odt.emplace(Node(x,r,v)).first;
}

pair<Iter,Iter> get(int l,int r){
	auto itr=split(r+1),itl=split(l);
	return make_pair(itl,itr);
}

void assign(int l,int r,int v){
	auto p=get(l,r);
	odt.erase(p.first,p.second);
	odt.emplace(Node(l,r,v));
}

inline int query(int l,int r){
	int sum=0;
	auto p=get(l,r);
	for(auto i=p.first;i!=p.second;i++)sum+=((i->r)-(i->l)+1)*(i->v);
	return sum;
}

signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    odt.emplace(0,10,0);
    cin>>a>>b;
    assign(0,0,a);
    assign(1,1,b);
    cout<<query(0,10);
	return 0;
}

完结撒花

原文