2023 GDCPC F-格子旅行(Traveling in Cells) 线段树上二分

思路参考题解:here

查了下没见到有人用线段树二分做的代码,所以自己实现了一个。线段树二分思路参考严格鸽 here

代码:(cf gym runid 229649227)

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll mn = 3e5 + 10, mt = mn * 4;

// 比 unordered_map 更快的哈希表
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash
{
    int operator()(int x) const { return x ^ RANDOM; }
};
typedef gp_hash_table<int, int, chash> hash_t;

ll s[mt];
hash_t h[mt];
#define lfs p << 1
#define rfs p << 1 | 1
#define mkcf ll cf = (lf + rf) >> 1
#define ci const int &
void modifyS(int p, int lf, int rf, ci pos, ci v)
{
    if (lf == rf)
    {
        s[p] += v;
        return;
    }
    mkcf;
    if (pos <= cf)
    {
        modifyS(lfs, lf, cf, pos, v);
    }
    else
    {
        modifyS(rfs, cf + 1, rf, pos, v);
    }
    s[p] = s[lfs] + s[rfs];
}
ll queryS(int p, int lf, int rf, ci lc, ci rc)
{
    if (lc <= lf && rf <= rc)
    {
        return s[p];
    }
    mkcf;
    ll ans = 0;
    if (lc <= cf)
    {
        ans += queryS(lfs, lf, cf, lc, rc);
    }
    if (cf + 1 <= rc)
    {
        ans += queryS(rfs, cf + 1, rf, lc, rc);
    }
    return ans;
}
void modifyC(int p, int lf, int rf, ci pos, ci c, ci v)
{
    h[p][c] += v;
    if (lf == rf)
    {
        return;
    }
    mkcf;
    if (pos <= cf)
    {
        modifyC(lfs, lf, cf, pos, c, v);
    }
    else
    {
        modifyC(rfs, cf + 1, rf, pos, c, v);
    }
}
#define colors const vector<ll> &
ll check(ll p, ll lf, ll rf, colors cs)
{
    ll sum = 0;
    for (auto &c : cs)
    {
        sum += h[p][c];
    }
    return sum == rf - lf + 1;
}
const ll fail = -1;
ll queryCR(int p, int lf, int rf, int lc, colors cs)
{
    if (rf < lc)
    {
        return fail;
    }
    if (lc <= lf)
    {
        if (check(p, lf, rf, cs))
        { // still ok, move right
            return -1;
        }
        if (lf == rf)
        { // first to fail is lf, then ok is lf-1
            return lf - 1;
        }
    }
    mkcf;
    ll pos = queryCR(lfs, lf, cf, lc, cs);
    if (pos != fail)
    {
        return pos;
    }
    return queryCR(rfs, cf + 1, rf, lc, cs);
}
ll queryCL(int p, int lf, int rf, int rc, colors cs)
{
    if (lf > rc)
    {
        return fail;
    }
    if (rf <= rc)
    {
        if (check(p, lf, rf, cs))
        {
            return -1;
        }
        if (lf == rf)
        {
            return lf + 1;
        }
    }
    mkcf;
    ll pos = queryCL(rfs, cf + 1, rf, rc, cs);
    if (pos != fail)
    {
        return pos;
    }
    return queryCL(lfs, lf, cf, rc, cs);
}

ll n, q, c[mn], v[mn];
void solve()
{
    cin >> n >> q;
    for (ll i = 1; i <= n; ++i)
    {
        cin >> c[i];
        modifyC(1, 1, n, i, c[i], 1);
    }
    for (ll i = 1; i <= n; ++i)
    {
        cin >> v[i];
        modifyS(1, 1, n, i, v[i]);
    }
    for (ll op, x, y; q--;)
    {
        cin >> op >> x >> y;
        if (op == 1)
        {
            modifyC(1, 1, n, x, c[x], -1);
            c[x] = y;
            modifyC(1, 1, n, x, c[x], 1);
        }
        else if (op == 2)
        {
            modifyS(1, 1, n, x, y - v[x]);
            v[x] = y;
        }
        else
        {
            bool ok = false;
            vector<ll> vec;
            for (ll i = 1, z; i <= y; ++i)
            {
                cin >> z;
                if (c[x] == z)
                {
                    ok = true;
                }
                vec.emplace_back(z);
            }
            if (!ok)
            {
                cout << "0\n";
                continue;
            }

            ll l = queryCL(1, 1, n, x, vec);
            ll r = queryCR(1, 1, n, x, vec);
            l = l == fail ? 1 : l;
            r = r == fail ? n : r;
            cout << queryS(1, 1, n, l, r) << '\n';
        }
    }

    for (ll i = 1; i <= 4 * n; ++i)
    {
        h[i].clear();
        s[i] = 0;
    }
}

signed main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    ll t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

/*
2
5 10
1 2 3 1 2
1 10 100 1000 10000
3 3 1 3
3 3 2 2 3
2 5 20000
2 3 200
3 3 2 1 3
3 3 3 1 2 3
1 3 4
2 1 100000
1 2 2
3 1 2 1 2
4 1
1 2 3 4
1000000 1000000 1000000 1000000
3 4 4 1 2 3 4
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值