dsu做法 不知道为什么tle on test 4
查看原帖
dsu做法 不知道为什么tle on test 4
1106289
KomorKomor楼主2024/10/16 22:11
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int fa[200010], a[200010];

int find(int x)
{
    if (x == fa[x])  return x;
    else return x = find(fa[x]);
}

void add(int x, int y)
{
    int xx = find(x), yy = find(y);
    if (xx != yy)    fa[xx] = yy;
}

void solve()
{
    int n, q;   cin >> n >> q;

    for (int i = 1; i <= n + 1; i++) fa[i] = i;
    for (int i = 1; i <= n; i++)    cin >> a[i];

    auto cal = [&](int x)
    {
        int sum = 0;
        while (x)
        {
            sum += x % 10;
            x /= 10;
        }

        return sum;
    };

    while (q--)
    {
        int op;     cin >> op;
        if (op == 2)
        {
            int x;  cin >> x;
            cout << a[x] << endl;
        }
        else
        {
            int l, r;   cin >> l >> r;
            for (int i = l; i <= r; i++)
            {
                i = find(i);
                if (i > r)   break;
                a[i] = cal(a[i]);
                if(a[i] < 10)   add(i, i + 1);
            }
        }
    }     

}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int t = 1;  cin >> t;
    while (t--) solve();

    return 0;
}
2024/10/16 22:11
加载中...