zoukankan      html  css  js  c++  java
  • 【洛谷P2184】贪婪大陆

    贪婪大陆

    题目链接

    对于一个区间[l,r],右端点在l左边即[1,l-1]中的区间与区间[l,r]没有交集,

    左端点在r右边即[r,n]中的区间与区间[l,r]没有交集,

    其余区间必与[l,r]有交集,

    因此维护两个树状数组,一个维护从1开始右端点的数量,

    另一个维护从n开始左端点的数量,插入时取值n-x+1插入即可

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 using namespace std;
     5 #define N 200010
     6 #define lowbit(x) (x&(-x))
     7 int n,m,tree[N][2],cnt,ans;
     8 inline int read(){
     9     int x=0; char c=getchar();
    10     while(c<'0'||c>'9') c=getchar();
    11     while('0'<=c&&c<='9') { x=(x<<3)+(x<<1)+c-'0'; c=getchar(); }
    12     return x;
    13 }
    14 inline void add(int p,int f){
    15     for(int i=p;i<=n;i+=lowbit(i))
    16         tree[i][f]++;
    17 }
    18 inline int query(int p,int f){
    19     int ans=0;
    20     for(int i=p;i;i-=lowbit(i))
    21      ans+=tree[i][f];
    22     return ans;
    23 }
    24 int main()
    25 {
    26     scanf("%d%d",&n,&m);
    27     int f,l,r;
    28     while(m--){
    29         f=read();
    30         l=read(); r=read();
    31         if(f==1){
    32             cnt++;
    33             add(r,0);
    34             add(n-l+1,1);
    35         }
    36         else{
    37             ans=cnt-query(l-1,0)-query(n-r,1);
    38             printf("%d
    ",ans);
    39         }
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    函数的设计和使用
    python正则表达式
    Python字符串
    Python序列(十一)集合
    centos 磁盘分区、格式化及挂载
    Nginx下配置SSL证书 调转到IIS、tomcat二级站点
    Mime 类型列表
    WCF学习- 体系结构
    .NET Framework Client Profile 简介
    WCF学习- 基础概念
  • 原文地址:https://www.cnblogs.com/yjkhhh/p/9360218.html
Copyright © 2011-2022 走看看