线段树MLE,求调
查看原帖
线段树MLE,求调
1423362
tyyj0324楼主2025/8/2 15:13

过了两组样例,其他的都MLE,求大佬帮忙调一下

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define pll pair<long, long>
#define endl '\n'
typedef long long ll;
#define fix(x) fixed << setprecision(x)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
const int mod = 1e9 + 7;
const int N = 1e5 + 9;
ll a[N], t[N * 4], lz[N * 4];
ll n, q;
void pushup(int o)
{
    t[o] = t[o << 1] + t[o << 1 | 1];
}
void build(int s = 1, int e = n, int o = 1)
{
    if(s == e)
    {
        t[o] = a[s];
        return;
    }
    int mid = (s + e) >> 1;
    build(s, mid, o << 1);
    build(mid + 1, e, o << 1 | 1);
    pushup(o);
}
void pushdown(int s, int e, int o)
{
    if(lz[o])
    {
        int ls = o << 1, rs = o << 1 | 1, mid = (s + e) >> 1;
        t[ls] = (mid - s + 1) - t[ls], lz[ls] ^= lz[o];
        t[rs] = (e - mid) - t[rs], lz[rs] ^= lz[o];
        lz[o] = 0;
    }
}
void update(int l, int r, int s = 1, int e = n, int o = 1)
{
    if(l <= s && e <= r)
    {
        t[o] = (e - s + 1) - t[o];
        lz[o] ^= 1;
        return;
    }
    pushdown(s, e, o);
    int mid = (s + e) >> 1;
    if(mid >= l)update(l, r, s, mid, o << 1);
    if(mid + 1 <= r)update(l, r, mid + 1, e, o << 1 | 1);
    pushup(o);
}
int query(int l, int r, int s = 1, int e = n, int o = 1)
{
    if(s == e)
    {
        return t[o];
    }
    pushdown(s, e, o);
    int mid = (s + e) >> 1;
    int res = 0;
    if(mid >= l)res += query(l, r, s, mid, o << 1);
    if(mid + 1 <= r)res += query(l, r, mid + 1, e, o << 1 | 1);
    pushup(o);
    return res;
}
void solve()
{
    cin >> n >> q;
    build();
    while(q--)
    {
        int op;
        cin >> op;
        if(op == 0)
        {
            int l, r;
            cin >> l >> r;
            update(l, r);
        }
        else
        {
            int l, r;
            cin >> l >> r;
            cout << query(l, r) <<'\n';
        }
    }
}

signed main ()
{
    IOS;
    int T = 1;
    //cin >> T;
    while(T--)
    {
        solve();
    }
    return 0;
}
2025/8/2 15:13
加载中...