• hdu 3333 离线线段树 + 思维/树状数组 /在线主席树


    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cmath>
    #include<cstring>
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const ll maxn = 30000 + 10;
    ll a[30000 + 10];
    
    struct tree
    {
        ll l , r;
        ll val;
    }t[maxn << 2];
    
    void push_up(ll rt){
        //if(t[rt << 1].val != t[rt << 1|1].val)
        t[rt].val = t[rt << 1].val + t[rt << 1|1].val;
        //else 
          //  t[rt].val += t[rt << 1].val ;
    }
    
    void build(ll rt,ll l ,ll r){
        t[rt].l = l;
        t[rt].r = r;
        t[rt].val = 0;
        if(l == r){
            //t[rt].val = a[l];
            return;
        }
        ll mid = (l + r) >> 1;
        build(rt << 1, l , mid);
        build(rt << 1|1,mid+1,r);
        push_up(rt);
    }
    
    void updata(ll rt,ll x,ll v){
        if(x == t[rt].l && t[rt].r == x){
            t[rt].val += v;
            return;
        }
        ll mid = (t[rt].l + t[rt].r) >> 1;
        if(x <= mid) updata(rt << 1 , x , v);
        else updata(rt << 1|1, x , v);
        push_up(rt);
    }
    
    ll query(ll rt,ll l,ll r){
         if(l <= t[rt].l && r >= t[rt].r) return t[rt].val;
        ll ret  = 0;
        ll mid = (t[rt].l + t[rt].r) >> 1;
        // if(r <= mid) return query(rt << 1, l, r);
        // else if(l > mid) return query(rt << 1|1,l,r);
        // else return query(rt << 1,l,mid) + query(rt << 1|1,mid + 1,r);
        if (l <= mid) ret += query(rt << 1 , l ,r);
        if( r > mid) ret += query(rt << 1|1,l,r);
        push_up(rt);
        return ret;
    }
    
    
    
    
    
    struct node
    {
        ll x,y,id;
    }nod[100000 + 7];
    map<ll,ll> mp;
    bool cmp(node s1 ,node s2){
         //if(s1.y == s2.y)
          //   return s1.x < s2.x;
        return s1.y < s2.y;
    
    }
    ll ans[100000 + 7];
    int main(int argc, char const *argv[])
    {
        ll tt;
        scanf("%lld",&tt);
        while(tt--){
            ll n;
            memset(a,0,sizeof a);
            memset(t,0,sizeof t);
            memset(ans,0,sizeof ans);
            mp.clear();
            memset(nod,0,sizeof nod);
            scanf("%lld",&n);
            for(ll i = 1;i <= n ; i++){
                scanf("%lld",&a[i]);
            }
            build(1,1,n);
            ll q;
            scanf("%lld",&q);
            for(ll i = 1;i <= q; i++){
                ll a,b;
                scanf("%lld%d",&a,&b);
            //printf("%lld
    ",query(1,a,b));
                nod[i].x = a,nod[i].y = b,nod[i].id = i;
            }
            ll i = 0;
            sort(nod + 1,nod + q + 1,cmp);
            
            for(ll j = 1; j <= q;j ++){
            for( ;i <= nod[j].y ; i++){
                if(mp[a[i]])
                //mp[a[i]] = 1;
                
                updata(1,mp[a[i]],-a[i]);
               // mp[a[i]] = 
               //cout << 1 ;
                mp[a[i]] = i;
                updata(1,i,a[i]);
            }
           // cout << 3 << endl ;
         //   cout << nod[j].id << " " << nod[j].x << " " << nod[j].y <<endl;
            ans[nod[j].id] = query(1,nod[j].x,nod[j].y);
           // cout << 1 << endl;
        }
       // cout << 1 ;
        //cout << q << endl;
        for(ll i = 1;i <= q;i ++){
            printf("%lld
    ",ans[i] );
        }
        }
        return 0;
    }
    线段树
  • 相关阅读:
    优秀大整数
    洛谷—— P3908 异或之和
    洛谷—— P1869 愚蠢的组合数
    洛谷—— P1680 奇怪的分组
    洛谷—— P1609 最小回文数
    Something I like
    数学相关
    新博客测试中~
    P3369 【模板】普通平衡树
    2017 11.6 NOIP模拟赛
  • 原文地址:https://www.cnblogs.com/DWVictor/p/11192912.html
Copyright © 2020-2023  润新知