zoukankan      html  css  js  c++  java
  • HDU 6610 Game

    #pragma GCC optimize("O2")
    #include<bits/stdc++.h>
    const int maxn = 1100000;
    typedef long long ll;
    struct T{
        int l,r,t,id;
    } q[maxn];
    inline int cmp(T a,T b){
        static const int bl = 2048;
        return
        a.l / bl == b.l / bl ? a.r / bl == b.r / bl ? a.r / bl & 1 ? a.t < b.t : a.t > b.t : a.r < b.r : a.l < b.l;
    }
    int a[maxn],suf[maxn],t,n,m;
    int pos[maxn];
    ll ans,aaa[maxn];
    int bak[maxn];
    inline void ins(int x){ ans += bak[x]++; }
    inline void del(int x){ ans -= --bak[x]; }
    inline void mdf(int pos,int l,int r){
        if(l <= pos && pos <= r) del(suf[pos]);
        if(l <= pos + 1 && pos + 1 <= r) del(suf[pos + 1]);
        std::swap(a[pos],a[pos + 1]),suf[pos]=suf[pos-1]^a[pos],suf[pos+1]=suf[pos]^a[pos+1];
        if(l <= pos && pos <= r) ins(suf[pos]);
        if(l <= pos + 1 && pos + 1 <= r) ins(suf[pos + 1]);
    }
    
    int main(){
        std::ios::sync_with_stdio(false),std::cin.tie(0);
        while(std::cin >> n >> m){
            for(int i=1;i<=n;++i) std::cin >> a[i],suf[i] = suf[i-1] ^ a[i];
            int tm = 0,tot=0;
            for(int i=1,opt;i<=m;++i){
                std::cin >> opt;
                if(opt == 1){
                    ++tot, std::cin >> q[tot].l >> q[tot].r; --q[tot].l;
                    q[tot].t = tm; q[tot].id = tot;
                    const int len = q[tot].r - q[tot].l;
                    aaa[tot] = ll(len + 1) * len >> 1;
                }else std::cin >> pos[++tm];
            }
            memset(bak,0,sizeof bak);
            std::sort(q + 1,q + tot + 1,cmp); ans = 0; ins(0);
            for(int i=1,l=0,r=0,tm=0;i<=tot;++i){
                while(l > q[i].l) ins(suf[--l]); while(r < q[i].r) ins(suf[++r]);
                while(l < q[i].l) del(suf[l++]); while(r > q[i].r) del(suf[r--]);
                while(tm < q[i].t) mdf(pos[++tm],l,r);
                while(tm > q[i].t) mdf(pos[tm--],l,r);
                aaa[q[i].id] -= ans;
            }
            for(int i=1;i<=tot;++i) std::cout << aaa[i] << '
    ';
    
        }
    }
    
  • 相关阅读:
    Tree MapByFold
    Tree DepthByFold
    Tree SizeByFold
    Tree MaximumByFold
    Tree Fold
    Tree Map
    Tree Depth
    Tree Maximum
    Tree Size
    Tree
  • 原文地址:https://www.cnblogs.com/skip1978/p/11455181.html
Copyright © 2011-2022 走看看