跳到正文

题解

P6627

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

文章信息

发布
2023年12月12日
收录
2026年10月15日
更新
2026年11月7日
篇幅
约 3 分钟
获赞
2
题目
P6627

版本

  1. v1当前

Solution

我们称一个初等的优惠为 (l,r,w)(l,r,w) 表示 [l,r][l,r] 中每个人获得 ww 的优惠。

那么我们的操作事实上是这样的:

  • op=1op=1,直接加入 (l,r,w)(l,r,w)。
  • op=2op=2,加入 (−∞,a−1,w)(-\infty,a-1,w) 和 (a+1,+∞,w)(a+1,+\infty,w)。
  • op=3op=3,加入 (a,a,w)(a,a,w)。

然后离散化+前缀和即可。

注意为了特判极端情况,我加了个 (0,0,0)(0,0,0)。

C++
#include<bits/stdc++.h>
#define int long long
#define ffor(i,a,b) for(int i=(a);i<=(b);i++)
#define roff(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
const int MAXN=4e5+10;
int n,tot,w[MAXN],lsh[MAXN];
vector<pair<pair<int,int>,int>> upd;
void insert(int l,int r,int w) {
	lsh[++tot]=l,lsh[++tot]=r+1;
	upd.push_back({{l,r},w});
	return ;	
}
int ans,pos;
void check(int W,int Pos) {
	if(W>ans) return ans=W,pos=Pos,void();
	if(W<ans) return ;
	if(W==ans&&((abs(Pos)<abs(pos))||(abs(Pos)==abs(pos)&&Pos>pos))) return ans=W,pos=Pos,void();
	return ;
}
signed main() {
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n;
	insert(0,0,0);
	int mx=1000000000;
	ffor(i,1,n) {
		int op; cin>>op;
		if(op==1) {
			int l,r,w; cin>>l>>r>>w;
			insert(l,r,w);
		}
		if(op==2) {
			int pos,v;
			cin>>pos>>v;
			insert(pos,pos,v);	
		}
		if(op==3) {
			int pos,v;
			cin>>pos>>v;	
			insert(-mx-1,pos-1,v);
			insert(pos+1,mx+1,v);
		}
	}
	sort(lsh+1,lsh+tot+1);
	tot=unique(lsh+1,lsh+tot+1)-lsh-1;
	for(auto pr:upd) {
		int l=pr.first.first,r=pr.first.second+1,v=pr.second;
		l=lower_bound(lsh+1,lsh+tot+1,l)-lsh;
		r=lower_bound(lsh+1,lsh+tot+1,r)-lsh;
		w[l]^=v,w[r]^=v;
	}
	ffor(i,1,tot) w[i]^=w[i-1];
	ffor(i,1,tot) check(w[i],lsh[i]);
	ffor(i,1,tot-1) check(w[i],lsh[i+1]-1);
	cout<<ans<<' '<<pos<<'\n';
	return 0;
}

感觉,这题,难度,差不多是,红~橙。

原文
存档评论5 条抓取于