跳到正文

个人记录

信奥大联赛周赛(提高组)#2515-S-C-小梦的物理小球

Nori_Ai约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

题目链接:D1507 G-小梦的物理小球

题意

给定一个平面直角坐标系,其上有若干条与 xx 轴平行的线段,将往上放若干个小球,小球将竖直向下移动,直至与 xx 轴重合。若途径线段(不包括两端 l,rl,r),则改变坠落位置,变到两端的概率均为 50%50\%。求所有小球坠落至 xx 轴横坐标的期望之和。

思路

一眼期望 dp。求直接落到每个线段后的期望和,定义函数 f(x)f(x) 表示从横坐标 xx 向下坠落落到的第一条线段或坐标轴。

得转换方程:

dpi=dpf(l)+dpf(r)2dp_i=\frac{dp_{f(l)}+dp_{f(r)}}{2}

求得 22 关于 998244353998244353 的逆元为 499122177499122177,这样可以处理分数取模。

难点在于 f(x)f(x),暴力思路是对线段按纵坐标排序,然后线性枚举,这么做的复杂度是 O(n2+qn)O(n^2+qn) 的,可以拿到 4040 分。

考虑按纵坐标从小到大添加线段,标记区间 [l,r][l,r] 的最高层线段,可以用线段树维护区间赋值,单点查询,注意需要离散化。

这样复杂度就降到了 O((n+q)log⁡n)O((n+q)\log n)。

在处理查询时需要求得其下落的第一个线段,因此需要重复一遍增加线段的步骤,从下往上枚举,根据查询的高度增加对应的线段,这里可以用双指针来维护。

具体实现并不困难,思路也不是很难想,夹杂了两到三个内容,评蓝没问题。

代码

C++
#include<bits/stdc++.h>
#define int long long
#define I_love_Foccarus return
#define cin_fast ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define endl '\n'
//#define getchar getc
#define pii pair<int,int>
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
#define pd(a) push_back(a)
#define in(a) a = read_int()
using namespace std;
const int Size = 1 << 14;
const int N = 6e5 + 5 , mod = 998244353;
const int inf = 0x3f3f3f3f3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
inline char getc(){
	static char syn[Size] , *begin = syn , *end = syn;
	if(begin == end) begin = syn , end = syn + fread(syn , 1 , Size , stdin);
	I_love_Foccarus *begin ++;
}
inline int read_int() {
	int x = 0;
	char ch = getchar();
	bool f = 0;
	while('9' < ch || ch < '0') f |= ch == '-' , ch = getchar();
	while('0' <= ch && ch <= '9') x = (x << 3) + (x << 1) + ch - '0' , ch = getchar();
	I_love_Foccarus f ? -x : x;
}
struct Tree{
    int ans , lazy = inf;
}t[N << 2];
void push_down(int idx , int l , int r) {
    if(t[idx].lazy != inf) {
        int mid = (l + r) >> 1;
        t[idx << 1].ans = t[idx].lazy , t[idx << 1].lazy = t[idx].lazy;
        t[idx << 1 | 1].ans = t[idx].lazy , t[idx << 1 | 1].lazy = t[idx].lazy;
        t[idx].lazy = inf;
    }
}
void build(int idx , int l , int r) {
	t[idx].ans = 0;
	t[idx].lazy = inf;
    if(l == r) {
        return;
    }
    int mid = (l + r) >> 1;
    build(idx << 1 , l , mid) , build(idx << 1 | 1 , mid + 1 , r);
}
void change(int idx , int l , int r , int ql , int qr , int val) {
    if(qr < l || r < ql) return;
    if(ql <= l && r <= qr) {
        t[idx].ans = val , t[idx].lazy = val;
        return;
    }
    int mid = (l + r) >> 1;
    push_down(idx , l , r);
    change(idx << 1 , l , mid , ql , qr , val);
    change(idx << 1 | 1 , mid + 1 , r , ql , qr , val);
}
//[ql,qr] [l,r]
int query(int idx , int l , int r , int qv) {
    if(l == r) return t[idx].ans;
    int mid = (l + r) >> 1;
    push_down(idx , l , r);
    if(qv <= mid) return query(idx << 1 , l , mid , qv);
    else return query(idx << 1 | 1 , mid + 1 , r , qv);
}
int b[N] , tot , dp[N] , n , q;
 struct qwe {
	int l , r , y;
}a[N];
bool cmp(qwe x , qwe y) {
	return x.y < y.y;
}
bool cmp2(pair<int , int> x , pair<int , int> y) {
	return x.se < y.se;
}
int c(int i , int x) {
	if(i) return dp[i];
	else return x;
}
pair<int , int>p[N];
signed main() {
	//cin_fast;
	int nt;
	in(nt);
	while(nt --) {
		memset(dp , 0 , sizeof(dp));
		in(n) , in(q);
		tot = 0;
		for(int i = 1 ; i <= n ; i ++) in(a[i].l) , in(a[i].r) , in(a[i].y) , b[++ tot] = a[i].l , b[++ tot] = a[i].r , b[++ tot] = a[i].y;
		for(int i = 1 ; i <= q ; i ++) in(p[i].fi) , in(p[i].se) , b[++ tot] = p[i].fi , b[++ tot] = p[i].se;
		sort(b + 1 , b + tot + 1);
		build(1 , 1 , tot);
		tot = unique(b + 1 , b + tot + 1) - b - 1;
		for(int i = 1 ; i <= n ; i ++) {
			a[i].l = lower_bound(b + 1 , b + tot + 1 , a[i].l) - b;
			a[i].r = lower_bound(b + 1 , b + tot + 1 , a[i].r) - b;
			a[i].y = lower_bound(b + 1 , b + tot + 1 , a[i].y) - b;
		}
		sort(a + 1 , a + n + 1 , cmp);
		for(int i = 1 ; i <= n ; i ++) {
			int l = b[a[i].l] , r = b[a[i].r];
			dp[i] = (c(query(1 , 1 , tot , a[i].l) , l) + c(query(1 , 1 , tot , a[i].r) , r)) * 499122177;
			dp[i] %= mod;
			change(1 , 1 , tot , a[i].l + 1 , a[i].r - 1 , i);
		}
		sort(p + 1 , p + q + 1 , cmp2);
		build(1 , 1 , tot);
		int ans = 0 , j = 1;
		for(int i = 1 ; i <= q ; i ++) {
			int x = lower_bound(b + 1 , b + tot + 1 , p[i].fi) - b , y = lower_bound(b + 1 , b + tot + 1 ,  p[i].se) - b;
			while(b[a[j].y] <= b[y] && j <= n) {
				change(1 , 1 , tot , a[j].l + 1 , a[j].r - 1 , j);
				j ++;
			}
			ans += c(query(1 , 1 , tot , x) , b[x]);
			ans %= mod;
		}
		cout<<ans<<'\n';
	}
	I_love_Foccarus 0;
}
原文