当前位置: 首页 > 工具软件 > Messenger > 使用案例 >

D - Messenger Simulator

邵修诚
2023-12-01

题意:给你一个“聊天栏”,以及m条按照顺序发来的消息id,

对于3,4,1,2

1,2,3,4->3,1,2,4->4,3,1,2->1,4,3,2->2,1,4,3

要求找出每一个数字在数组中下标的最左以及最右端

思路:对于每个操作,我们发现,它会下标严格小于被操作数的下标加1,对于每一个id来说,它的左端总是1或者i。而右端就显得难以计算了。

非常巧妙特殊的模拟方式,对研究对象的精准选取

我们对于每个位置做树状数组(在操作中的所有移动过的位置即在每次操作操作元素的下标不断向左偏移)

对于上面那个例子

1/1+m,2/2+m,3/3+m,4/4+m->1/1+m,2/2+m,3/m,4/4+m->...

对于每个位置,我们维护它前面数字的数量,注意树状数组前缀和的特性,以及优越的logn的时间复杂度

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>

//#define int ll
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 300010, M = 2 * N, B = N, MOD = int(1e9+7);
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

//int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
int dx[8] = { 1,2,2,1,-1,-2,-2,-1 }, dy[8] = { 2,1,-1,-2,-2,-1,1,2 };
int n, m, k;
int a[N];
int l[N], r[N];
int tr[M];
int p[N];

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll MOD) {
    ll res = 1;
    while (b) {
        if (b & 1) res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

inline void init() {}

void add(int x, int c)
{
    for (int i = x; i <= M - 20; i += lowbit(i))
        tr[i] += c;
}

int qsum(int x)
{
    int res = 0;
    for (int i = x; i; i -= lowbit(i))
        res += tr[i];
    return res;
}

void slove()
{
    cin >> n >> m;
    pre(i, 1, m) cin >> a[i];

    for (int i = 1; i <= n; i++)
    {
        l[i] = r[i] = i;
        p[i] = i + m - 1;
        add(p[i], 1);
    }

    for (int i = 1; i <= m; i++)
    {
        k = a[i];
        l[k] = 1;
        r[k] = max(r[k], qsum(p[k]));
        add(p[k], -1);
        p[k] = m - i + 1;
        add(p[k], 1);
    }

    for (int i = 1; i <= n; i++)
    {
        r[i] = max(r[i], qsum(p[i]));
        cout << l[i] << ' ' << r[i] << endl;
    }
    return;
}

signed main()
{
    //IOS;
    int _ = 1;
    //si(_);
    init();
    while (_--)
    {
        slove();
    }
    return 0;
}
/*

*/

 类似资料: