线段树(填坑中)

 

敌兵布阵 HDU - 1166  

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 5e4 +7;
ll _,n,m,k,t,u,v,w,ans,cnt,ok;
int num[mxn] , last[mxn] , key[mxn] ;
map< pair< pair<int,int> ,int > ,int > mp;
string str;
struct node {int val , l , r , sum ;};
int lowbit(int x) {return x&-x;};
void add(int i,int val){while(i<=n) key[i]+=val , i+=lowbit(i) ;}
int ask(int x)
{
    int ans = 0 ;
    while(x>0) ans+=key[x] , x-=lowbit(x) ;
    return ans;
}
int main()
{
    tle;
    int Case = 1 ;
    for(cin>>t;t;t--)
    {
        cout<<"Case "<<Case++<<":"<<endl;
        cin>>n;memset(key,0,sizeof(key));
        rep(i,1,n) cin>>k  , add(i,k) ;
        while(cin>>str && str[0]!='E')
        {
            cin>>u>>v;
            if(str[0]=='Q') cout<<ask(v)-ask(u-1)<<endl;
            else if(str[0]=='A') add(u,v);
            else add(u,-v);
        }
    }
}
View Code

敌兵布阵 HRBUST - 1794  (cin,cout会TLE)

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 2e5 +7;
ll _,n,m,k,t,u,v,w,ans,cnt,ok;
int num[mxn] , last[mxn] , key[mxn] ;
/// string str ;
char str[10] ;
#define lc now<<1
#define rc now<<1|1
struct node {int val , l , r , sum ;}no[mxn<<4];
int lowbit(int x) {return x&-x;};
/// void add(int i,int val){while(i<=n) key[i]+=val , i+=lowbit(i) ;}
/// int ask(int x){int ans = 0 ;while(x>0) ans+=key[x] , x-=lowbit(x) ;return ans;}
void up(int now){no[now].val = no[lc].val + no[rc].val;}
void build(int now , int l , int r)
{
    no[now].l = l ; no[now].r = r ;
    if(l==r) {scanf("%d",&no[now].val) ; return ;}
    /// {cin>>no[now].val ;return ;}
    int mid = (l+r)>>1;
    build(lc,l,mid) ; build(rc,mid+1,r) ; up(now);
}
void update(int l,int r,int poi,int k,int now)
{
    if(l==r){no[now].val += k; return ;}
    int mid = (l+r)>>1;
    if(poi <= mid ) update(l,mid,poi,k,lc);
    else update(mid+1,r,poi,k,rc);
    up(now) ;
}
int ask(int l,int r,int L,int R,int now)
{
    if(L<=l && r<=R) {return no[now].val ;}
    int mid = (l+r)>>1 , ans = 0 ;
    if(L<=mid) ans+=ask(l,mid,L,R,lc);
    if(R>mid) ans+=ask(mid+1,r,L,R,rc);
    return ans ;
}
int main()
{
    tle;
    int Case = 1 ;
    for(scanf("%d",&t);t;t--)
    {
        printf("Case %d:\n",Case++);
        /// cout<<"Case "<<Case++<<":"<<endl;
        /// cin>>n;
        scanf("%d",&n);
        build(1,1,n);
        while(scanf("%s",str) && str[0]!='E')
        {
            /// cin>>u>>v;
            scanf("%d %d",&u,&v);
            if(str[0] == 'Q') printf("%d\n",ask(1,n,u,v,1)) ;
                ///cout<<ask(1,n,u,v,1)<<endl;
            else if(str[0] == 'A') update(1,n,u,v,1);
            else update(1,n,u,-v,1);
        }
    }
}
View Code

A Simple Problem with Integers  POJ - 3468  

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 2e5 +7;
int _,n,m,k,t,u,v,w,ans,cnt,ok;
int num[mxn] , last[mxn] , key[mxn] ;
/// string str ;
char str[10] , ch ;
#define lc now<<1
#define rc now<<1|1
struct node {ll val , l , r , sum , lazy ;};node no[mxn<<4];
int lowbit(int x) {return x&-x;};
/// void add(int i,int val){while(i<=n*4) key[i]+=val , i+=lowbit(i) ;}
/// int ask(int x){int ans = 0 ;while(x>0) ans+=key[x] , x-=lowbit(x) ;return ans;}
void pushup(int now){
    no[now].val = no[lc].val + no[rc].val ;
}
void updown(int now)
{
    /// no[now].val = no[lc].val + no[rc].val;
    /// no[now].val = max( no[lc].val , no[rc].val );
    if(no[now].lazy)
    {
        no[lc].lazy += no[now].lazy;
        no[rc].lazy += no[now].lazy;
        no[lc].val += (no[lc].r-no[lc].l+1)*no[now].lazy;
        no[rc].val += (no[rc].r-no[rc].l+1)*no[now].lazy;
        no[now].lazy = 0;
    }
}
void build(int l,int r,int now)
{
    no[now].l = l, no[now].r = r ,no[now].lazy = 0 ,no[now].val = 0 ;
    if(l==r){
        cin>>no[now].val;return ;
    }
    int mid = (l+r)>>1;
    build(l,mid,lc); build(mid+1,r,rc); pushup(now) ;
}
ll ask(int l,int r,int now)
{
    if(l<=no[now].l && r>=no[now].r){
        return no[now].val;
    }
    updown(now);
    int mid = (no[now].l+no[now].r)>>1;
    ll ans = 0 ;
    if(l<=mid) ans+=ask(l,r,lc);
    if(r>mid) ans+=ask(l,r,rc);
    return ans ;
}
void add(int l,int r,int val,int now)
{
    if(l<=no[now].l && no[now].r<=r){
        no[now].lazy += val ;
        no[now].val += (no[now].r-no[now].l+1)*val;
        return ;
    }
    updown(now);
    int mid = (no[now].l+no[now].r)>>1;
    if(l<=mid) add(l,r,val,lc);
    if(r>mid) add(l,r,val,rc);
    pushup(now);
}
int main()
{
    tle;
    while(cin>>n>>m)
    {
        build(1,n,1);
        while(m--)
        {
            int u,v;
            cin>>ch>>u>>v;
            if(ch=='Q') cout<<ask(u,v,1)<<endl;
            else cin>>k , add(u,v,k,1);
        }
    }
}
View Code

Big Water Problem

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 2e5 +7;
int _,n,m,t,k,u,v,ans,cnt,ok,lim;
ll num[mxn] , cost[mxn] ,  far[mxn] , sum[mxn];
char ch;
#define lc now<<1
#define rc now<<1|1
string str ;
struct node {ll l,r,val,lazy,mx,mn;}p[mxn<<4];

vector<int>vc[mxn];
int lowbit(int x) {return (x&-x);}
void add(int x,int k)
{
    while(x<=4*n){
        sum[x]+=k;
        x+=lowbit(x);
    }
}
ll ask(int x)
{
    ll ans = 0 ;
    while(x>0){
        ans+=sum[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    tle;
    while(cin>>n>>m)
    {
        memset(num,0,sizeof(num));
        rep(i,1,n) cin>>num[i] , add(i,num[i]) ;
        int l,r,k;
        while(m--)
        {
            cin>>k>>l>>r;
            if(k==1) add(l,r);
            if(k==2) cout<<ask(r)-ask(l-1)<<endl;
        }
    }
}
树状数组
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 2e5 +7;
ll _,n,m,k,t,u,v,w,ans,cnt,ok;
int num[mxn] , last[mxn] , key[mxn] ;
/// string str ;
char str[10] ;
#define lc now<<1
#define rc now<<1|1
struct node {int val , l , r , sum ;}no[mxn<<4];
int lowbit(int x) {return x&-x;};
/// void add(int i,int val){while(i<=n) key[i]+=val , i+=lowbit(i) ;}
/// int ask(int x){int ans = 0 ;while(x>0) ans+=key[x] , x-=lowbit(x) ;return ans;}
void up(int now){no[now].val = no[lc].val + no[rc].val;}
void build(int now , int l , int r)
{
    no[now].l = l ; no[now].r = r ;
    if(l==r) {scanf("%d",&no[now].val) ; return ;}
    /// {cin>>no[now].val ;return ;}
    int mid = (l+r)>>1;
    build(lc,l,mid) ; build(rc,mid+1,r) ; up(now);
}
void update(int l,int r,int poi,int k,int now)
{
    if(l==r){no[now].val += k; return ;}
    int mid = (l+r)>>1;
    if(poi <= mid ) update(l,mid,poi,k,lc);
    else update(mid+1,r,poi,k,rc);
    up(now) ;
}
int ask(int l,int r,int L,int R,int now)
{
    if(L<=l && r<=R) {return no[now].val ;}
    int mid = (l+r)>>1 , ans = 0 ;
    if(L<=mid) ans+=ask(l,mid,L,R,lc);
    if(R>mid) ans+=ask(mid+1,r,L,R,rc);
    return ans ;
}
int main()
{
    tle;
    while(~scanf("%d %d",&n,&m))
    {
        build(1,1,n);
        while(m--)
        {
            scanf("%d %d %d",&k,&u,&v);
            if(k==1) update(1,n,u,v,1);
            else printf("%d\n",ask(1,n,u,v,1)) ;
        }
    }
}
很繁琐的线段树写法,参数可以减少的

 

Stars

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 2e5 +7;
int _,n,m,k,t,u,v,w,ans,cnt,ok;
int num[mxn] , last[mxn] , key[mxn] ;
char str[10] , ch ;
#define lc now<<1
#define rc now<<1|1
struct node {int val , l , r , sum ;}no[mxn<<4];
int lowbit(int x) {return x&-x;};
void add(int i,int val){while(i<=n*4) key[i]+=val , i+=lowbit(i) ;}
int ask(int x){int ans = 0 ;while(x>0) ans+=key[x] , x-=lowbit(x) ;return ans;}
int main()
{
    while(cin>>n)
    {
        memset(num,0,sizeof(num));
        memset(key,0,sizeof(key));
        rep(i,1,n)
        {
            cin>>u>>v;u++;
            num[ ask(u) ]++; add(u,1);
        }
        rep(i,0,n-1) cout<< num[i]<<endl;
    }
}
View Code

 I Hate It HDU - 1754 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 2e5 +7;
int _,n,m,k,t,u,v,w,ans,cnt,ok;
int num[mxn] , last[mxn] , key[mxn] ;
/// string str ;
char str[10] , ch ;
#define lc now<<1
#define rc now<<1|1
struct node {int val , l , r , sum ;}no[mxn<<4];
int lowbit(int x) {return x&-x;};
/// void add(int i,int val){while(i<=n) key[i]+=val , i+=lowbit(i) ;}
/// int ask(int x){int ans = 0 ;while(x>0) ans+=key[x] , x-=lowbit(x) ;return ans;}
void up(int now)
{
    /// no[now].val = no[lc].val + no[rc].val;
    no[now].val = max( no[lc].val , no[rc].val );
}
void build(int l,int r,int now)
{
    no[now].l = l ; no[now].r = r;
    if(l==r) {scanf("%d",&no[now].val) ; return ;}
    int mid = (l+r)>>1;
    build(l,mid,lc) , build(mid+1,r,rc) , up(now);
}
int ask(int l,int r,int now)
{
    if(no[now].l>=l && r>=no[now].r) {return no[now].val;}
    int mid = (no[now].l+no[now].r)>>1 , ans = -1 ;
    if(l<=mid) { ans = max( ans , ask(l,r,lc) ); }
    if(r>mid) { ans = max( ans , ask(l,r,rc) ); }
    return ans;
}
void update(int x,int k,int now)
{
    if(no[now].l==x && no[now].r==x) {no[now].val = k;return ;}
    int mid = (no[now].l+no[now].r)/2;
    if(mid>=x) update(x,k,lc);
    else update(x,k,rc);
    up(now);
}
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        build(1,n,1);
        while(m--)
        {
            scanf("%s%d%d",str,&u,&v);
            if(str[0]=='Q') cout<<ask(u,v,1)<<endl;
            else if(str[0]=='U') update(u,v,1);
        }
    }
    return 0 ;
}
View Code

Codeforces Round #555 (Div. 3)- EMinimum Array

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 2e5 +7;
int _,n,m,k,t,u,v,w,ans,cnt,ok;
int num[mxn] , last[mxn] , key[mxn] ;
/// string str ;
char str[10] , ch ;
#define lc now<<1
#define rc now<<1|1
struct node {int val , l , r , sum ;}no[mxn<<4];
int lowbit(int x) {return x&-x;};int main()
{
    while(cin>>n)
    {
        s.clear();
        rep(i,1,n) cin>>key[i];
        rep(i,1,n) cin>>k , s.insert(k) ;
        multiset<int>::iterator it =s.begin();
        rep(i,1,n)
        {
            it = s.lower_bound(n-key[i]);
            if(it==s.end()) it = s.begin();
            cout<<(key[i]+*it)%n<<" ";
            s.erase(it);
        }
    }
}
View Code

 

 

Just a Hook  HDU - 1698 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 1e5 +7;
int _,n,m,t,u,ans,cnt,ok,lim;
ll w[mxn] , cost[mxn] , dp[mxn] , far[mxn] , siz[mxn];
double num[mxn];
#define lc now<<1
#define rc now<<1|1
string str ;
struct node {int l,r,val,lazy;}p[mxn<<4];
void pushup(int now){p[now].val = p[lc].val+p[rc].val;}
void pushdown(int now)
{
    if(p[now].lazy)
    {
        p[lc].lazy = p[rc].lazy = p[now].lazy;
        p[lc].val = p[now].lazy*(p[lc].r-p[lc].l+1);
        p[rc].val = p[now].lazy*(p[rc].r-p[rc].l+1);
        p[now].lazy = 0 ;
    }
}
void build(int l,int r,int now)
{
    p[now].l = l,p[now].r = r , p[now].val = 1 ,p[now].lazy = 1 ;
    if(l==r) return ;
    int mid = (l+r)>>1;
    build(l,mid,lc);build(mid+1,r,rc);pushup(now);
}
void add(int l,int r,int k,int now)
{
    if(l<= p[now].l && r>=p[now].r){
        p[now].val = k*(p[now].r-p[now].l+1) ;
        p[now].lazy = k ;
        return ;
    }
    pushdown(now);
    int mid = (p[now].l+p[now].r)>>1;
    if(l<=mid) add(l,r,k,lc);
    if(r>mid) add(l,r,k,rc);
    pushup(now);
}
int ask(int l,int r,int now)
{
    if(l<=p[now].l && r>=p[now].r){
        return p[now].val;
    }
    int mid = (p[now].l+p[now].r)>>1 , ans = 0;
    if(l<=mid) ans+=ask(l,r,lc);
    if(r>mid) ans+=ask(l,r,rc);
    return ans;
}
int main()
{
    tle;
    int Case = 1 ;
    for(cin>>t;t;t--)
    {
        cin>>n>>m;
        build(1,n,1);
        int l,r,k;
        while(m--)
        {
            cin>>l>>r>>k;
            add(l,r,k,1);
        }
        cout<<"Case "<<Case++<<": The total value of the hook is "<<ask(1,n,1)<<"."<<endl;
    }
}
Segment Tree

 

Balanced Lineup  POJ - 3264  

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 1e5 +7;
int _,n,m,t,u,v,ans,cnt,ok,lim;
ll w[mxn] , cost[mxn] , dp[mxn] , far[mxn] , siz[mxn];
double num[mxn];
#define lc now<<1
#define rc now<<1|1
string str ;
struct node {int l,r,val,lazy,mx,mn;}p[mxn<<4];
void pushup(int now){
    /// p[now].val = p[lc].val+p[rc].val;
    p[now].mx = max(p[lc].mx , p[rc].mx);
    p[now].mn = min(p[lc].mn , p[rc].mn);
}
void pushdown(int now)
{
    if(p[now].lazy)
    {
        p[lc].lazy = p[rc].lazy = p[now].lazy;
        p[lc].val = p[now].lazy*(p[lc].r-p[lc].l+1);
        p[rc].val = p[now].lazy*(p[rc].r-p[rc].l+1);
        p[now].lazy = 0 ;
    }
}
void build(int l,int r,int now)
{
    p[now].l = l,p[now].r = r ;/// p[now].lazy = 0 ;
    if(l==r){
        /// cin>>p[now].val;
        scanf("%d",&p[now].val);
        p[now].mx = p[now].mn = p[now].val ;
        return ;
    }
    int mid = (l+r)>>1;
    build(l,mid,lc);build(mid+1,r,rc);pushup(now);
}
void ask(int l,int r,int now)
{
    if(l<=p[now].l && r>=p[now].r){
        u = max(u,p[now].mx);
        v = min(v,p[now].mn);
        return ;
    }
    int mid = (p[now].l+p[now].r)>>1;
    if(l<=mid) ask(l,r,lc);
    if(r>mid) ask(l,r,rc);
}
int main()
{
    tle;
    ///while(cin>>n>>m)
    while(~scanf("%d %d",&n,&m))
    {
        build(1,n,1);
        int l,r;
        while(m--)
        {
            /// cin>>l>>r;
            scanf("%d %d",&l,&r);
            u = -1 , v = mod ;ask(l,r,1);
            printf("%d\n",u-v);
            /// cout<<u-v<<endl;
        }
    }
}
Segment Tree

 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 1e5 +7;
int _,n,m,t,u,v,ans,cnt,ok,lim;
ll w[mxn] , cost[mxn] ,  far[mxn] , siz[mxn];
double num[mxn];
#define lc now<<1
#define rc now<<1|1
string str ;
struct node {int l,r,val,lazy,mx,mn;}p[mxn<<4];
int dp[mxn][20][2];
void rmq()
{
    for(int j=1;j<=19/*log(n)/log(2)+1*/;j++)
    {
        for(int i=1;i+(1<<j)-1<=n;i++)
        {
            dp[i][j][0] = max(dp[i][j-1][0] , dp[i+(1<<(j-1))][j-1][0] );
            dp[i][j][1] = min(dp[i][j-1][1] , dp[i+(1<<(j-1))][j-1][1] );
        }
    }
}
int main()
{
    tle;
    while(~scanf("%d %d",&n,&m))
    {
        for(int i=1,k;i<=n;i++)
            scanf("%d",&k) ,dp[i][0][0] = dp[i][0][1] = k ;
        rmq();
        int l,r;
        while(m--)
        {
            /// cin>>l>>r;
            scanf("%d %d",&l,&r);
            int k = log((double)(r-l+1))/log(2.0);
            printf("%d\n", max(dp[l][k][0],dp[r-(1<<k)+1][k][0]) - min(dp[l][k][1],dp[r-(1<<k)+1][k][1]) );
            /// cout<<u-v<<endl;
        }
    }
}
RMQ

Can you answer these queries?  HDU - 4027 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 1e5 +7;
int _,n,m,t,u,v,ans,cnt,ok,lim;
ll w[mxn] , cost[mxn] ,  far[mxn] , siz[mxn];
double num[mxn];
#define lc now<<1
#define rc now<<1|1
string str ;
struct node {ll l,r,val,lazy,mx,mn;}p[mxn<<4];
void pushup(int now){
    p[now].val = p[lc].val+p[rc].val;
    /// p[now].mx = max(p[lc].mx , p[rc].mx);
    /// p[now].mn = min(p[lc].mn , p[rc].mn);
}
void pushdown(int now)
{
    if(p[now].lazy)
    {
        p[lc].lazy = p[rc].lazy = p[now].lazy;
        p[lc].val = p[now].lazy*(p[lc].r-p[lc].l+1);
        p[rc].val = p[now].lazy*(p[rc].r-p[rc].l+1);
        p[now].lazy = 0 ;
    }
}
void build(int l,int r,int now)
{
    p[now].l = l,p[now].r = r ;///, p[now].lazy = 0 ;
    if(l==r){
        /// cin>>p[now].val;
        scanf("%lld",&p[now].val);
        return ;
    }
    int mid = (l+r)>>1;
    build(l,mid,lc);build(mid+1,r,rc);pushup(now);
}
void add(int l,int r,int now)
{
    if(p[now].r-p[now].l+1 == p[now].val) return ;
    if(p[now].l==p[now].r)
    {
        p[now].val = sqrt(p[now].val);
        return ;
    }
    ///pushdown(now);
    int mid = (p[now].l+p[now].r)>>1;
    if(l<=mid) add(l,r,lc);
    if(r>mid) add(l,r,rc);
    pushup(now);
}
ll ask(int l,int r,int now)
{
    if(p[now].l>=l && p[now].r<=r)
    {
        return p[now].val;
    }
    int mid = (p[now].l+p[now].r)>>1;
    ll ans = 0 ;
    if(l<=mid) ans+=ask(l,r,lc);
    if(r>mid) ans+=ask(l,r,rc);
    return ans;
}
int main()
{
    tle;int Case = 1 ;
    while(~scanf("%d",&n))
    {
        printf("Case #%d:\n",Case++);
        build(1,n,1);
        scanf("%d",&m);
        int k,l,r;
        while(m--)
        {
            cin>>k>>l>>r;
            if(l>r) swap(l,r);
            if(k) printf("%lld\n",ask(l,r,1));
            else add(l,r,1);
        }
        printf("\n");
    }
}
View Code

 

Tunnel Warfare HDU - 1540  (线段树区间合并)

题意:有N个连续的城市,现有三种操作,D X 摧毁X城市,Q X 和X村庄是联通的城市有多少个,R 修复最后一个摧毁的城市

题解:第一种:区间合并。第二种,维护线段树每个城市X的左边的最大城市编号 L 和右边的最小城市编号 R,这样R - L + 1 就是和城市X的联通的城市的数量

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 2e5 +7;
int _,n,m,t,k,u,v,ans,cnt,ok,lim;
ll w[mxn] , cost[mxn] ,  far[mxn] , siz[mxn];
char ch;
#define lc now<<1
#define rc now<<1|1
string str ;
struct node {ll l,r,val,lazy,mx,mn;}p[mxn<<4];
int lmx[mxn] , rmx[mxn] , num[mxn] ;
void pushup(int l,int r,int now)
{
    lmx[now] = lmx[lc] ; rmx[now] = rmx[rc];
    int mid = (l+r)>>1;
    if(lmx[now]==mid-l+1) lmx[now]+=lmx[rc];
    if(rmx[now]==r-mid)   rmx[now]+=rmx[lc];
    num[now] = max( rmx[lc]+lmx[rc] , max(num[lc],num[rc]) );
}
void build(int l,int r,int now)
{
    if(l==r){
        lmx[now] = rmx[now] = num[now] = 1 ; return ;
    }
    int mid = (l+r)>>1;
    build(l,mid,lc);build(mid+1,r,rc);pushup(l,r,now);
}
void up(int l,int r,int k,int now,int lim)
{
    if(l==r){
        if(l==k){
            num[now] = lmx[now] = rmx[now] = (lim?0:1);
        }
        return ;
    }
    int mid = (l+r)>>1;
    if(k<=mid) up(l,mid,k,lc,lim);
    if(k>mid)  up(mid+1,r,k,rc,lim);
    pushup(l,r,now);
}
int ask(int l,int r,int k,int now)
{
    if(num[now] == r-l+1 || !num[now]) return num[now] ;
    int mid = (l+r)>>1;
    if(k<=mid)
    {
        if(k>=mid-rmx[lc]+1) return ask(l,mid,k,lc) + ask(mid+1,r,mid+1,rc);
        else return ask(l,mid,k,lc);
    }
    else
    {
        if(k<=mid+1+lmx[rc]-1) return ask(l,mid,mid,lc) + ask(mid+1,r,k,rc);
        else return ask(mid+1,r,k,rc);
    }
}
int main()
{
    while(cin>>n>>m)
    {
        stack<int>s;
        build(1,n,1);
        while(m--)
        {
            cin>>ch;
            if(ch=='D'){
                cin>>k; s.push(k);
                up(1,n,k,1,1);
            }
            else if(ch=='Q'){
                cin>>k;
                cout<<ask(1,n,k,1)<<endl;
            }
            else {
                up(1,n,s.top(),1,0);s.pop();
            }
        }
    }
}
View Code

 

Mayor's posters POJ - 2528 (离散化)

 

https://ac.nowcoder.com/acm/problem/15164

posted @ 2020-05-02 10:25  __MEET  阅读(150)  评论(0编辑  收藏  举报