跳到正文

个人记录

8.10数据结构专题测试4

czhusi约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年8月11日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 8 分钟

版本

  1. v1当前

T1

思路

我们需要统计每个星星左下方的星星数量,并统计每个等级有多少颗星星。

注意到题目给出的星星是按 yy 坐标升序排列的,yy 相同时按 xx 升序排列。所以树状数组动态维护即可。

代码

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

using namespace std;
using ll = long long;

const int N = 4e4;

int n, ans[N + 10], c[N + 10];

int lowbit(int x){
	return x & -x;
}

void UP(int x, int p) {
    for(; x < N; x += lowbit(x)){
        c[x] += p;
    }
}

int Query(int x) {
    int sum = 0;
    for(; x; x -= lowbit(x)){
        sum += c[x];
    }
    return sum;
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for(int i = 1, x, y; i <= n; i++){
		cin >> x >> y;
        ans[Query(x + 1)]++;     
        UP(x + 1, 1);    
    }
    for (int i = 0; i < n; ++i) {
        cout << ans[i] << '\n';
    }
	return 0;
}

T2

思路

经典的线段树,单修区查,设添加操作共执行了 xx 那我们就可以将此题看作,用一棵线段树维护一个 n+xn + x 的区间,一开始后 xx 个元素的值为 00。

代码

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

using namespace std;
using ll = long long;

const int N = 2e5 + 10, MAXM = 2e5;

int m, p, c[4 * N], lans, len;

int comb(int a, int b){
	return max(a, b);
} 

void UP(int id, int l, int r, int x, int p){
	if(l == r){
		c[id] = p;
		return ;
	}
	int mid = (l + r) / 2;
	if(x <= mid){
		UP(id * 2, l, mid, x, p);
	}else{
		UP(id * 2 + 1, mid + 1, r, x, p);
	}
	c[id] = comb(c[id * 2], c[id * 2 + 1]);
}

int Query(int id, int l, int r, int x, int y){
	if(x <= l && y >= r){
		return c[id];
	}
	if(x > r || y < l){
		return 0;
	}
	int mid = (l + r) / 2;
	return comb(Query(id * 2, l, mid, x, y), Query(id * 2 + 1, mid + 1, r, x, y));
}

int main(){
	ios::sync_with_stdio(0), cin.tie(0);
    cin >> m >> p;
    while(m--){
        char op;
        cin >> op;
        if (op == 'A') {
            int t;
            cin >> t;
            len++;
            UP(1, 1, MAXM, len, (t + lans) % p);
        } else {
            int t;
            cin >> t;
            int res = Query(1, 1, MAXM, len - t + 1, len);
            cout << res << '\n';
            lans = res;
        }
    }
    
	return 0;
}

T3

思路

本题要求我们计算从城池 lili 到城池 riri 的最少天数,其中每天移动的距离不超过 LL。由于城池按编号顺序排列,且位置递增,我们可以利用 stst 表来维护答案。

要求解答案我们需要对于每个城池 ii,维护两个信息:

  • ans[0][i].firstans[0][i].first:从城池 ii 出发,一天内能到达的最右边的城池。
  • ans[0][i].secondans[0][i].second:从城池 ii 出发,一天内能到达的最左边的城池。

对于上面两个信息,直接二分求解即可。

最后,对于每个查询 (l,r)(l, r):

  • 如果 l==rl == r,直接返回 00。
  • 如果 l<rl < r(向右):
    • 从大到小枚举 kk,如果 2k2^k 天移动后仍未到达 rr,则移动并累加天数。
  • 如果 l>rl > r(向左):
    • 同理,从大到小枚举 kk,如果 2k2^k 天移动后仍未到达 rr,则移动并累加天数。
  • 最后加上剩余的 11 天。

代码

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

using namespace std;
using ll = long long;

#define int ll

const int M = 1e5 + 10, G = 17; 

int n, Q, x[M], L;

pair<int, int> ans[G][M];

void P(){
    for (int i = 1; i <= n; i++){
        int l = i, r = n;
        while(l <= r){
            int mid = (l + r) / 2;
            if (x[mid] <= x[i] + L){
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        ans[0][i].first = l - 1;
        l = 1, r = i;
        while(l <= r){
            int mid = (l + r) / 2;
            if(x[mid] >= x[i] - L){
                r = mid - 1;
            }else{
                l = mid + 1;
            }
        }
        ans[0][i].second = r + 1;
    }
    for(int k = 1; k < G; k++){
        for(int i = 1; i <= n; i++){
            ans[k][i].first = ans[k - 1][ans[k - 1][i].first].first;
            ans[k][i].second = ans[k - 1][ans[k - 1][i].second].second;
        }
    }
}

int Query(int x, int y){
    if(x == y){
        return 0;
    }
    int anss = 0;
    if(x < y){
        for(int k = G - 1; k >= 0; k--){
            if(ans[k][x].first < y){
                x = ans[k][x].first;
                anss += (1 << k);
            }
        }
    }else{ 
        for(int k = G - 1; k >= 0; k--){
            if(ans[k][x].second > y){
                x = ans[k][x].second;
                anss += (1 << k);
            }
        }
    }
    return anss + 1;
}

signed main(){
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> x[i];
    }
    cin >> L >> Q;
    P();
    while(Q--){
        int l, r;
        cin >> l >> r;
        cout << Query(l, r) << '\n';
    }
    return 0;
}
原文