Luogu Problem Solution P2184 [Greedy Continent]

  • Topic link:

https://www.luogu.org/problemnew/show/P2184

  • Ideas:

First of all, I want to spit out why the solutions in the existing problems are the same, and it is more difficult to understand;

I will talk about my approach, which is essentially the same, but easier to understand.

According to the meaning of the title, each time a mine is added, there will be one more type, right, we use a cnt to record the number of mines added, and use two arrays to record the positions of the left and right endpoints. Then when querying [l, r], we query how many right endpoints [1, l-1] have, and how many left endpoints [r+1, n] have, and what does the sum of these two numbers mean? ? It is how many times the mines have not been laid in the [l,r] interval we query.

Finally, the sum of the cnt-just two queries is how many times the mines have been laid in the [l,r] interval, and the output can be output. Of course, we use the line segment tree to maintain these operations.

  • Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cctype>
using namespace std;
const int maxn=100005;
int sum_r[maxn<<2],sum_l[maxn<<2],add[maxn<<2];
int n,m;
int L,R;
void update_l(int now,int l,int r,int t){
    if(l==r){
        sum_l[now]++;
        return ;
    }
    int mid=(l+r)>>1;
    if(t<=mid)update_l(now<<1,l,mid,t);
    else update_l(now<<1|1,mid+1,r,t);
    sum_l[now]=sum_l[now<<1]+sum_l[now<<1|1];
    return ;
}
void update_r(int now,int l,int r,int t){
    if(l==r){
        sum_r[now]++;
        return ;
    }
    int mid=(l+r)>>1;
    if(t<=mid)update_r(now<<1,l,mid,t);
    else update_r(now<<1|1,mid+1,r,t);
    sum_r[now]=sum_r[now<<1]+sum_r[now<<1|1];
    return ;
}
int query_l(int now,int l,int r){
    if(L<=l&&r<=R){
        return sum_l[now];
    }
    int mid=(l+r)>>1;
    int ans=0; 
    if(L<=mid)ans+=query_l(now<<1,l,mid);
    if(mid<R)ans+=query_l(now<<1|1,mid+1,r);
    return ans;
}
int query_r(int now,int l,int r){
    if(L<=l&&r<=R){
        return sum_r[now];
    }
    int mid=(l+r)>>1;
    int ans=0; 
    if(L<=mid)ans+=query_r(now<<1,l,mid);
    if(mid<R)ans+=query_r(now<<1|1,mid+1,r);
    return ans;
}
int main()
{
    int op,l,r;
    int cnt=0,tmp=0;
    scanf("%d %d",&n,&m);
    for(register int i=1;i<=m;i++){
        scanf("%d %d %d",&op,&l,&r);
        if(op==1){
            update_l(1,1,n,l);
            update_r(1,1,n,r);
            cnt++;
        }
        else {
            L=r+1,R=n;
            if(L<=R)tmp+=query_l(1,1,n);        
            L=1,R=l-1;
            if(L<=R)tmp+=query_r(1,1,n);
            cout<<cnt-tmp<<endl;
            tmp=0;
        }
    }
    return 0;
} 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324940941&siteId=291194637