树状数组进阶

· · 个人记录

基础

今天的题目需要结合一些其他的东西,那么就需要进行推导与结合,所以题目叫难。

需要的思路是树上问题转成区间问题,还有进行推到规律。

例题

P5149 会议座位

这道题就是一个逆序对。对于字符串存入到 map 中,映射到下一个输入中,其他的就是模板逆序对。

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
int n ;
string s[100005] ;
struct cTr
{
    vector<int> c ;
    void init()
    {
        c.clear() ;
        c.resize(n + 5) ;
    }
    inline int lowbit(int x)
    {
        return x & -x ;
    }
    void update(int x , int k)
    {
        while(x <= n)
        {
            c[x] += k ;
            x += lowbit(x) ;
        }
    }
    int query(int x)
    {
        int ans = 0 ;
        while(x > 0)
        {
            ans += c[x] ;
            x -= lowbit(x) ;
        }
        return ans ;
    }
} ;
map<string , int> mp ;
void solve()
{
  cin >> n ;
  for( int i = 1 ; i <= n ; i++ ) cin >> s[i] ;
  cTr Tr ;
  Tr.init() ;
  for( int i = 1 ; i <= n ; i++ ) 
  {
    string S ;
    cin >> S ;
    mp[S] = i ;
    }
    ll ans = 0 ;
  for( int i = n ; i >= 1 ; i-- )
  {
    ans += Tr.query(mp[s[i]]) ;
    Tr.update(mp[s[i]] , 1) ;
    }
    cout << ans ;
}
signed main()
{
  ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int oT_To = 1 ;
//  cin >> oT_To ;
  while(oT_To--) solve() ;
  return 0 ;
}

P12894 [蓝桥杯 2025 国 Java B] 智能交通信号灯

这道题目,观察到只有求两个数的 mex,那么 ans\in \{1,2,3\}。所以分类讨论:答案是 1 的话,那么答案就可以看成 cnt_2\times cnt_3+cnt2\times (cnt_2-1)\div 2+cnt3\times (cnt_3-1)\div 2;2 的答案就是 cnt_1\times cnt_3+cnt1\times (cnt_1-1)\div 2;答案是 3 的话,答案就是 cnt_1\times cnt_2。

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
int n , m ;
int a[100005] ;
struct cTr
{
    vector<int> c1 , c2 ;
    void init()
    {
        c1.clear() ;
        c1.resize(n + 5) ;
        c2.clear() ;
        c2.resize(n + 5) ;
    }
    inline int lowbit(int x)
    {
        return x & -x ;
    }
    void It(int x , int k)
    {
        int id = x ;
        while(x <= n)
        {
            c1[x] += (a[id] == 1) ;
            c2[x] += (a[id] == 2) ;
            x += lowbit(x) ;
        }
    }
    void update(int x , int k)
    {
        int id = x ;
        while(x <= n)
        {
            c1[x] -= (a[id] == 1) ;
            c2[x] -= (a[id] == 2) ;
            x += lowbit(x) ;
        }
        a[id] = k ;
        x = id ;
        while(x <= n)
        {
            c1[x] += (a[id] == 1) ;
            c2[x] += (a[id] == 2) ;
            x += lowbit(x) ;
        }
    }
    int query1(int x)
    {
        int ans = 0 ;
        while(x > 0)
        {
            ans += c1[x] ;
            x -= lowbit(x) ;
        }
        return ans ;
    }
    int query2(int x)
    {
        int ans = 0 ;
        while(x > 0)
        {
            ans += c2[x] ;
            x -= lowbit(x) ;
        }
        return ans ;
    }
} ;
void solve()
{
  cin >> n >> m ;
  cTr Tr ;
  Tr.init() ;
  for( int i = 1 ; i <= n ; i++ ) 
    {
        cin >> a[i] ;
        Tr.It(i , a[i]) ;
    }
  while(m--)
  {
    int op ;
    cin >> op ;
    if(op == 1)
    {
        int l , r ;
        cin >> l >> r ;
        ll _1 = Tr.query1(r) - Tr.query1(l - 1) ;
        ll _2 = Tr.query2(r) - Tr.query2(l - 1) ;
        ll _0 = r - l + 1 - _1 - _2 ;
        ll ans = 0 ;
//          cout << _0 << " " << _1 << " " << _2 << " " ;
        ans += 3ll * _1 * _2 ;
        ans += 2ll * (_1 * _0 + (_1 * (_1 - 1) / 2)) ;
        ans += 1ll * ((_0 * (_0 - 1) / 2) + _2 * _0 + (_2 * (_2 - 1) / 2)) ;
        cout << ans << "\n" ;
        }
        else
        {
            int k , x ;
            cin >> k >> x ;
            Tr.update(k , x) ;
        }
    }
}
signed main()
{
  ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int oT_To = 1 ;
//  cin >> oT_To ;
  while(oT_To--) solve() ;
  return 0 ;
}

P12385 [蓝桥杯 2023 省 Python B] 异或和

这道题就是对于每一个点 i 变成 dfn_i,就变成了区间问题,树状数组即可。

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define int long long
int n , m ;
int A[100005] , a[100005] ;
struct cTr
{
    vector<int> c ;
    void init()
    {
        c.clear() ;
        c.resize(n + 5) ;
    }
    inline int lowbit(int x)
    {
        return x & -x ;
    }
    void It(int x , int k)
    {
        while(x <= n)
        {
            c[x] ^= k ;
            x += lowbit(x) ;
        }
    }
    void update(int x , int k)
    {
        int id = x ;
        while(x <= n)
        {
            c[x] ^= k ;
            x += lowbit(x) ;
        }
    }
    int query(int x)
    {
        int ans = 0 ;
        while(x > 0)
        {
            ans ^= c[x] ;
            x -= lowbit(x) ;
        }
        return ans ;
    }
} ;
vector<int> G[100005] ;
int now = 0 ;
int F[100005] ;
int siz[100005] ;
void dfs(int u , int fa)
{
    F[u] = ++now ;
    siz[u] = 1 ;
    for( int i = 0 ; i < G[u].size() ; i++ )
    {
        int v = G[u][i] ;
        if(v == fa) continue ;
        dfs(v , u) ;
        siz[u] += siz[v] ;
//      cout << now << " " ;
    }
}
void solve()
{
  cin >> n >> m ;
  for( int i = 1 ; i <= n ; i++ ) 
    {
        cin >> A[i] ;
    }
    for( int i = 1 ; i < n ; i++ )
    {
        int u , v ;
        cin >> u >> v ;
        G[u].push_back(v) ;
        G[v].push_back(u) ;
    }
  cTr Tr ;
  Tr.init() ;
  dfs(1 , 0) ; 
  for( int i = 1 ; i <= n ; i++ )
  {
    Tr.It(F[i] , A[i]) ;
    }
  while(m--)
  {
    int op ;
    cin >> op ;
    if(op == 1)
    {
        int k , x ;
            cin >> k >> x ;
            Tr.update(F[k] , A[k] ^ x) ;
            A[k] = x ;
        }
        else
        {
        int x ;
        cin >> x ;
        cout << (Tr.query(F[x] + siz[x] - 1) ^ Tr.query(F[x] - 1)) << "\n" ;
        }
    }
}
signed main()
{
  ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int oT_To = 1 ;
//  cin >> oT_To ;
  while(oT_To--) solve() ;
  return 0 ;
}

P5673 「SWTR-2」Picking Gifts

这道题,就是 HH的项链 的加强版,那一题中,是只有一个种类中只有一个有贡献,这个题是 k 个有贡献。其余的就没有什么不同了。

考虑队列进行 k 个贡献的存储,但是会 MLE,这是因为,每一个队列都要预留大约 0.5MB 的空间,就会炸掉。则考虑进行链表模拟。

#include<bits/stdc++.h>
using namespace std ;
#define ll long long
#define int long long
int n , m , k ;
struct node
{
    int p , v ;
}a[1000005] ;
struct cTr
{
    vector<int> c ;
    void init()
    {
        c.clear() ;
        c.resize(n + 5) ;
    }
    inline int lowbit(int x)
    {
        return x & -x ;
    }
    void update(int x , int k)
    {
        while(x <= n)
        {
            c[x] += k ;
            x += lowbit(x) ;
        }
    }
    int query(int x)
    {
        int ans = 0 ;
        while(x > 0)
        {
            ans += c[x] ;
            x -= lowbit(x) ;
        }
        return ans ;
    }
} ;
struct query
{
    int l , r , id ;
}Q[500005] ;
bool cmp(query x , query y)
{
    return x.r < y.r ;
}
vector<int> q[1000005] ;
int N[1000005] ;
int as[500005] ;
void solve()
{
  cin >> n >> m >> k ;
  for( int i = 1 ; i <= n ; i++ ) 
    {
        cin >> a[i].p ;
    }
    for( int i = 1 ; i <= n ; i++ ) 
    {
        cin >> a[i].v ;
    }
  cTr Tr ;
  Tr.init() ;
  for( int i = 1 ; i <= m ; i++ )
  {
    int l , r ;
    cin >> l >> r ;
    Q[i] = {l , r , i} ;
    }
    sort(Q + 1 , Q + m + 1 , cmp) ; 
    int now = 1 ;
    for( int i = 1 ; i <= n ; i++ )
    {
        if(q[a[i].p].size() - N[a[i].p] == k - 1)
        {
//          cout << a[i].p << " " << q[a[i].p].front() << " " << i << "\n" ;
            Tr.update(q[a[i].p][N[a[i].p]] , -a[q[a[i].p][N[a[i].p]]].v) ;
      N[a[i].p]++ ;
        }
        q[a[i].p].push_back(i) ;
        Tr.update(i , a[i].v) ;
        while(now <= m && Q[now].r == i)
        {
//          cout << Tr.query(Q[now].r) << " " ;
            as[Q[now].id] = Tr.query(Q[now].r) - Tr.query(Q[now].l - 1) ;
            now++ ;
        }
    }
    for( int i = 1 ; i <= m ; i++ ) cout << as[i] << "\n" ;
}
signed main()
{
  ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
  int oT_To = 1 ;
//  cin >> oT_To ;
  while(oT_To--) solve() ;
  return 0 ;
}