变化的区间树状数组,单点查询

hdu  1556 Color the ball

要想区间改动的话,那么节点就必须往上更新,查询时往上累加。(区间改动。单点查询)
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100000+5;
int C[maxn];
int n;
int lowbit(int x)
{
    return (-x)&x;
}
void update(int x,int y)
{
    for(int i=x; i>0;i-=lowbit(i))
        C[i]+=y;
}
int query(int x)
{
    int s=0;
    for(int k=x; k<=n; k+=lowbit(k))
        s+=C[k];
    return s;
}
int main()
{

    int a,b;
    while(cin>>n)
    {
       for(int i=1; i<=n; i++)
        C[i]=0;
       for(int h=0;h<n;h++)
      {
       scanf("%d%d",&a,&b);
       update(b,1);
       update(a-1,-1);
      }
       for(int j=1;j<=n;j++)
        if(j==n)   printf("%d\n",query(j));
       else
          printf("%d ",query(j));
          //printf("%d%c",query(i),i==n?'\n':' ');
    }
    return 0;
}

csu 1335 高桥和低桥(树状数组+二分)

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1e5+10;
int c[N],m,n,k,a[N];
int x[N],y[N];
int lowbit(int k)
{
    return k&(-k);
}
void add(int k,int he)
{
    while(k>0)
    {
        c[k]+=he;
        k-=lowbit(k);
    }
}

int  Q(int k)
{
    int query=0;
    while(k<=n)
    {
     query+=c[k];
        k+=lowbit(k);
    }
    return query;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.cpp","r",stdin);
    #endif // ONLINE_JUDGE
    int t,from,to,he,kkk=1;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
      memset(c,0,sizeof(c));
      for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
        sort(a,a+n);
        int cur=1;
        int s,t;
       for(int i=1;i<=m;i++)
       {
           scanf("%d %d",&s,&t);
           int from=upper_bound(a,a+n,cur)-a;
           int to=upper_bound(a,a+n,s)-a;
           add(from,-1);
           add(to,+1);
           cur=t;
       }
        int ans =0 ;
        for(int i=1;i<=n;i++)
        {
            int kk =Q(i);
            if(kk>=k)
            ans = ans+1;
        }
        printf("Case %d: %d\n",kkk++,ans);
    }
    return 0;
}

相关题:  hdu  A Simple Problem with Integers

版权声明:本文博主原创文章。博客,未经同意不得转载。

时间: 2024-07-31 17:31:05

变化的区间树状数组,单点查询的相关文章

HDU 3333 Turing Tree 树状数组 离线查询

题意: 给你一个数列,然后有n个查询,问你给定区间中不同数字的和是多少. 思路还是比较难想的,起码对于蒟蒻我来说. 将区间按照先右端点,后左端点从小到大排序之后,对于每个查询,我只要维护每个数字出现的最后一次就可以了(这个结论稍微想一下就可以证明是正确的). 然后就是简单的点更新,区间求和问题了- #include <cstdio> #include <cstring> #include <iostream> #include <map> #include

TOJ 2725 See you~(二维树状数组单点更新区间查询)

描述 Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algorithm and Programming, and I met so many good friends. I want to say sorry to Mr, Yin, I must leave now ~~>.<~~. I am very sorry, we could not advanc

【POJ 3321】 Apple Tree (dfs重标号设区间+树状数组求和)

[POJ 3321] Apple Tree (dfs重标号设区间+树状数组求和) Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21966   Accepted: 6654 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. K

Libre OJ 130、131、132 (树状数组 单点修改、区间查询 -&gt; 区间修改,单点查询 -&gt; 区间修改,区间查询)

#130. 树状数组 1 :单点修改,区间查询 题目链接:https://loj.ac/problem/130 题目描述 这是一道模板题. 给定数列 a[1], a[2], \dots, a[n]a[1],a[2],…,a[n],你需要依次进行 qq 个操作,操作有两类: 1 i x:给定 i,xi,x,将 a[i]a[i] 加上 xx: 2 l r:给定 l,rl,r,求 \sum_{i=l}^ra[i]∑i=lr?a[i] 的值(换言之,求 a[l]+a[l+1]+\dots+a[r]a[l

一维 + 二维树状数组 + 单点更新 + 区间更新 详解

树状数组详解: 假设一维数组为A[i](i=1,2,...n),则与它对应的树状数组C[i](i=1,2,...n)是这样定义的: C1 = A1 C2 = A1 + A2 C3 = A3 C4 = A1 + A2 + A3 + A4 C5 = A5 C6 = A5 + A6 ................. C8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 ................ 如图可知: 为奇数的时候他是代表他本身,而为偶数的时候则是代表着自

hdu-3333 Turing Tree 离线区间+树状数组(区间不同数的和)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 题目大意: 给出一数组,以及m个查询区间,每次查询该区间不同数字的和.相同数字只加一次. 解题思路: 离线区间,按照区间右端点进行排序. 这样可以从左到右扫一遍,用尺取法一个一个将数字放入树状数组中. 如果这个数字已经在树状数组里面,记录之前的下标,再从树状数组中删去之前下标的这个数字,在当前下标添加该数字.这样可以保证每一步操作,都会使得树状数组中没有重复元素.这样可以直接用树状数组求不同

【算法系列学习】线段树vs树状数组 单点修改,区间查询 [kuangbin带你飞]专题七 线段树 A - 敌兵布阵

https://vjudge.net/contest/66989#problem/A 单点修改,区间查询 方法一:线段树 http://www.cnblogs.com/kuangbin/archive/2011/08/15/2139834.html 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<cmath> 6 #

Stars(树状数组单点更新)

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers wa

【树状数组+离线查询】HDU 3333 Turing Tree

https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/H [题意] 给定一个数组,查询任意区间内不同数字之和. (n<=30000,Q<=100000,每个数字<=1 000 000 000) [思路] 要算任意区间内不同数字之和,如果我们从左到右依次处理,每次只保留最右边出现的数字,处理以当前数字为右端点的查询,就能做到"不同数字之和",即不重不漏.因此我们要离线处理查询,按记录每个数作为右端点的所有查