洛谷 2184 贪婪大陆

【题解】

  这是一道区间覆盖的题目,我们可以用树状数组或者线段树来解决。显然,每个区间 [L,R] 被多少条线段覆盖这样计算:R左边的线段的左端点数-L左边的线段的右端点数

  这样,我们分别维护1~Pos位置上左端点、右端点的个数即可。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define lowbit (x&(-x))
 4 using namespace std;
 5 const int maxn=200010;
 6 int suml[maxn],sumr[maxn],n,m,opt,l,r;
 7 inline int read(){
 8     int k=0,f=1; char c=getchar();
 9     while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
10     while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
11     return k*f;
12 }
13 void add(int type,int x){
14     if(type==1) for(;x<=n;x+=lowbit) suml[x]++;
15     else for(;x<=n;x+=lowbit) sumr[x]++;
16 }
17 int query(int type,int x){
18     if(type==1){ int ret=0; for(;x;x-=lowbit) ret+=suml[x]; return ret;}
19     else{int ret=0; for(;x;x-=lowbit) ret+=sumr[x]; return ret;}
20 }
21 int main(){
22     n=read(); m=read();
23     while(m--){
24         if(read()==1){
25             l=read(); r=read(); 
26             add(1,l); add(2,r);
27         }
28         else{
29             l=read(); r=read();
30             printf("%d\n",query(1,r)-query(2,l-1));
31             //printf("QAQ%d %d\n",query(2,l-1),query(1,r));
32         }
33     }
34     return 0;
35 }
View Code

 

转载于:https://www.cnblogs.com/DriverLao/p/8206577.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值