zoukankan      html  css  js  c++  java
  • 洛谷 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
    ",query(1,r)-query(2,l-1));
    31             //printf("QAQ%d %d
    ",query(2,l-1),query(1,r));
    32         }
    33     }
    34     return 0;
    35 }
    View Code
  • 相关阅读:
    [转]VS2013自带SQL Server 的启用方法
    [转]CryptographyHelper.cs
    [转]Oracle 经验集
    程序的健壮性和鲁棒性
    死理性派恋爱法:拒绝掉前面37%的人
    Asp.net页面间传值方式汇总
    【操作系统】总结五(I/O管理)
    Windows编程
    【操作系统】磁盘
    【操作系统】文件系统
  • 原文地址:https://www.cnblogs.com/DriverLao/p/8206577.html
Copyright © 2011-2022 走看看