跳到正文

个人记录

8.23

__LZH__约 7 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

P3120

先朴素DP得:

dpi,j=∑x<i,y<j,ai,j≠ax,ydpx,ydp_{i, j} = \sum_{x < i, y < j, a_{i, j} ≠ a_{x, y}} dp_{x, y}

直接写CDQ优化DP,而且既然是不等于,直接开桶即可

C++
#include<bits/stdc++.h>

#define int long long

using namespace std;

const int N = 755, MOD = 1e9 + 7;

struct node{
	int x, y, c, t;
}a[N * N];

int n, m, k, top, sum[N * N];

bool cmp(node a, node b){
	return a.y < b.y;
}

void cdq(int l, int r){
	if(l >= r) return ;
	sort(a + l, a + r + 1, [](const node &a, const node &b){ return a.x < b.x; });
	if(a[l].x == a[r].x) return ;
	int md = (a[r].x + a[l].x) / 2, mid = md * m;
	cdq(l, mid), sort(a + l, a + mid + 1, cmp), sort(a + mid + 1, a + r + 1, cmp);
	for(int i = mid + 1, s = 0, j = l; i <= r; i++){
		for(; j <= mid && a[i].y > a[j].y; j++) (sum[a[j].c] += a[j].t) %= MOD, (s += a[j].t) %= MOD;
		a[i].t = (a[i].t + s - sum[a[i].c] + MOD) % MOD;
	}
	for(int i = l; i <= mid; i++) sum[a[i].c] = 0;
	cdq(mid + 1, r);
}

signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m >> k;
  for(int i = 1; i <= n; i++){
  	for(int j = 1, x; j <= m; j++){
  		cin >> x; a[++top] = {i, j, x, (i == 1 && j == 1)};
		}
	}
	cdq(1, top);
	for(int i = 1; i <= top; i++){
		if(a[i].x == n && a[i].y == m) cout << a[i].t;
	}
	return 0;
}

P2487

这题我们可以发现答案可以用 ii 为开头或结尾的最长不上升子序列表示,然后就用CDQ计算一下,用总方案数包含与 ii 的方案除一下即可。

C++
#include<bits/stdc++.h>

#define int long long

using namespace std;

const int N = 2e6 + 10;

struct node{
	int id, x, y, t1;
	double t2;
}a[N], b[N];

bool cmp(node a, node b){
	return a.x < b.x;
}

int n, m, k, c[N], tr1[N], len1[N], len2[N];
double cnt1[N], cnt2[N], tr2[N];

void add(int x, int t1, double t2){
	for(; x <= m; x += (x & -x)){
		if(t1 == tr1[x]) tr2[x] += t2;
		else if(tr1[x] < t1) tr1[x] = t1, tr2[x] = t2;
	}
}

void query(int x, int &t1, double &t2){
	for(; x; x -= (x & -x)){
		if(t1 == tr1[x]) t2 += tr2[x];
		else if(t1 < tr1[x]) t1 = tr1[x], t2 = tr2[x];
	}
}

void clear(int x){
	for(; x <= m; x += (x & -x)) tr1[x] = tr2[x] = 0;
}

void cdq(node a[N], int l, int r){
	if(l >= r) return ;
	int mid = (l + r) >> 1; 
	sort(a + l, a + r + 1, [](const node &a, const node &b){ return a.id < b.id; }); 
	cdq(a, l, mid), sort(a + l, a + mid + 1, cmp), sort(a + mid + 1, a + r + 1, cmp);
	for(int i = l, j = mid + 1; j <= r; j++){
		for(; i <= mid && a[j].x >= a[i].x; i++) add(a[i].y, a[i].t1, a[i].t2);
		int t1 = 0; double t2 = 0; query(a[j].y, t1, t2);
		if(a[j].t1 == t1 + 1) a[j].t2 += t2;
		else if(t1 + 1 > a[j].t1) a[j].t1 = t1 + 1, a[j].t2 = t2;
	}
	for(int i = l; i <= mid; i++) clear(a[i].y);
	cdq(a, mid + 1, r);
}

signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 1, x, y; i <= n; i++) cin >> x >> y, a[i] = {i, 1e9 - x, y, 1, 1}, c[i] = y;
	sort(c + 1, c + 1 + n), m = unique(c + 1, c + 1 + n) - c - 1;
	for(int i = 1; i <= n; i++) a[i].y = m - (lower_bound(c + 1, c + 1 + m, a[i].y) - c) + 1;
	cdq(a, 1, n);
	for(int i = 1; i <= n; i++) b[i].id = n - a[i].id + 1, b[i].x = 1e9 - a[i].x, b[i].y = m - a[i].y + 1, b[i].t1 = b[i].t2 = 1;
	cdq(b, 1, n); int ans1 = 0; double ans2 = 0;
	for(int i = 1; i <= n; i++){
		if(ans1 < a[i].t1) ans1 = a[i].t1, ans2 = a[i].t2; 
		else if(ans1 == a[i].t1) ans2 += a[i].t2;
		len1[a[i].id] = a[i].t1, len2[n - b[i].id + 1] = b[i].t1;
		cnt1[a[i].id] = a[i].t2, cnt2[n - b[i].id + 1] = b[i].t2;
	}
	cout << ans1 << '\n';
	cout << fixed << setprecision(5);
	for(int i = 1; i <= n; i++){
		if(len1[i] + len2[i] - 1 == ans1){
			cout << 1.0 * cnt1[i] * cnt2[i] / ans2 << ' ';
		}else{
			cout << "0.0000 ";
		}
	}
	return 0;
}
原文