ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

GYM103373F(线段树,子段问题)

2022-08-08 10:03:21  阅读:183  来源: 互联网

标签:lc GYM103373F 子段 int mxR 线段 tr mxL resL


GYM103373F(线段树,子段问题)

题意

给定一个01串。定义01交替或者10交替的子段为交替串。

有两个操作:

  1. flip区间 \([l,r]\)
  2. 输出区间 \([l,r]\) 内交替串的数量

思路

  1. 如果有极长交替串长度,可以算出交替串数量

于是考虑维护交替串的长度。我们用线段树对其维护。

维护当前结点的答案 \(res\)

前缀极长交替串和后缀极长交替串 \(mxL,xmR\)

区间左右端点的数字 \(numL,numR\)

合并区间前需要判断是否可以将两个子区间的串拼接。具体的维护pushup如下

void pushup(int p) {
    tr[p].res = tr[lc].res + tr[rc].res;
    tr[p].mxL = tr[lc].mxL;
    tr[p].mxR = tr[rc].mxR;
    tr[p].numL = tr[lc].numL;
    tr[p].numR = tr[rc].numR;
    
    if(tr[lc].numR == (tr[rc].numL ^ 1)) {
        tr[p].res += tr[lc].mxR * tr[rc].mxL;
        
        if(tr[lc].mxL == tr[lc].r - tr[lc].l + 1)
            tr[p].mxL = tr[lc].mxL + tr[rc].mxL;
        
        if(tr[rc].mxR == tr[rc].r - tr[rc].l + 1)
            tr[p].mxR = tr[rc].mxR + tr[lc].mxR;
    }
}

对flip操作,翻转两次等于没有翻转,因此只要用一个tag标记该子区间是否翻转即可。

代码实现上需要注意在query的过程中,仍然需要判断左右区间中间部分能否合并,操作同pushup。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<stack>
#include<string>
#include<random>
#include<functional>
#include<iomanip>
#define yes puts("yes");
#define inf 0x3f3f3f3f
#define ll long long
#define linf 0x3f3f3f3f3f3f3f3fll
#define ull unsigned long long
#define endl '\n'
#define int long long
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
using namespace std;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x;}
using PII = array<int,2>;
const int MAXN =10 + 2e5 ,mod=1e9 + 7;
int n,q;
int a[MAXN];

#define lc (p << 1)
#define rc (p << 1 | 1)
struct Node {
    int l,r;
    int res,mxL,mxR,numL,numR;
    int tag;
}tr[MAXN << 2];
void pushup(int p) {
    tr[p].res = tr[lc].res + tr[rc].res;
    tr[p].mxL = tr[lc].mxL;
    tr[p].mxR = tr[rc].mxR;
    tr[p].numL = tr[lc].numL;
    tr[p].numR = tr[rc].numR;
    
    if(tr[lc].numR == (tr[rc].numL ^ 1)) {
        tr[p].res += tr[lc].mxR * tr[rc].mxL;
        
        if(tr[lc].mxL == tr[lc].r - tr[lc].l + 1)
            tr[p].mxL = tr[lc].mxL + tr[rc].mxL;
        
        if(tr[rc].mxR == tr[rc].r - tr[rc].l + 1)
            tr[p].mxR = tr[rc].mxR + tr[lc].mxR;
    }
}
void upd(int p) {
    tr[p].numL ^= 1;
    tr[p].numR ^= 1;
    tr[p].tag ^= 1;
}
void pushdown(int p) {
    if(tr[p].tag) {
        upd(lc);
        upd(rc);
        tr[p].tag = 0;
    }
}
void build(int l,int r,int p = 1) {
    tr[p].l = l,tr[p].r = r;
    if(l == r) {
        tr[p].mxL = tr[p].mxR = tr[p].res = 1;
        tr[p].numL = tr[p].numR = a[l];
        tr[p].tag = 0;
        return;
    }
    int mid = l + r >> 1;
    build(l,mid,lc);
    build(mid + 1,r,rc);
    pushup(p);
}
void modify(int l,int r,int p = 1) {
    if(l <= tr[p].l and tr[p].r <= r) {
        upd(p);
        return;
    }
    int mid = tr[p].l + tr[p].r >> 1;
    pushdown(p);
    if(l <= mid)
        modify(l,r,lc);
    if(r > mid)
        modify(l,r,rc);
    pushup(p);
}
Node query(int l,int r,int p = 1) {
    if(l <= tr[p].l and tr[p].r <= r) 
        return tr[p];
    
    int mid = tr[p].l + tr[p].r >> 1;
    
    pushdown(p);
    
    Node ans;
    if(l <= mid and r > mid) {
        Node resL,resR;
        resL = query(l,r,lc);
        resR = query(l,r,rc);

        ans.l = resL.l;
        ans.r = resR.r;
        ans.res = resL.res + resR.res;
        ans.mxL = resL.mxL;
        ans.mxR = resR.mxR;
        ans.numL = resL.numL;
        ans.numR = resR.numR;
        
        if(resL.numR == (resR.numL ^ 1)) {
            ans.res += resL.mxR * resR.mxL;
            
            if(resL.mxL == resL.r - resL.l + 1)
                ans.mxL = resL.mxL + resR.mxL;
            
            if(resR.mxR == resR.r - resR.l + 1)
                ans.mxR = resR.mxR + resL.mxR;
        }
        
        return ans;
    }
    if(l <= mid) 
        return query(l,r,lc);
    return query(l,r,rc);
    
}
void solve()
{    
    cin >> n >> q;
    for(int i = 1;i <= n;i += 1)
        cin >> a[i];
    
    build(1,n);
    
    while(q --) {
        int op,l,r; cin >> op >> l >> r;
        if(op == 1) 
            modify(l,r);
        else
            cout << query(l,r).res << endl;
    }
}
signed main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

    // int T;cin>>T;
    // while(T--)
        solve();

    return 0;
}

标签:lc,GYM103373F,子段,int,mxR,线段,tr,mxL,resL
来源: https://www.cnblogs.com/Mxrush/p/16560755.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有