【♂Not The Triumph♂O(∩_∩)O哈哈~But The Struggle♂】

竞赛决不是捷径,它只是另一种艰辛的生活方式。得到与失去,只有时间会去评判;成功与失败,只有历史能去仲裁。我不会永远成功,正如我不会永远失败一样

  C++博客 :: 首页 :: 联系 ::  :: 管理
  6 Posts :: 239 Stories :: 25 Comments :: 0 Trackbacks

常用链接

留言簿(7)

我参与的团队

搜索

  •  

积分与排名

  • 积分 - 107071
  • 排名 - 231

最新评论

阅读排行榜

评论排行榜

描述 Description
校门外有很多树,有苹果树,香蕉树,有会扔石头的,有可以吃掉补充体力的……
如今学校决定在某个时刻在某一段种上一种树,保证任一时刻不会出现两段相同种类的树,现有两个操作:
  K=1,读入l,r表示在l~r之间种上的一种树
  K=2,读入l,r表示询问l~r之间能见到多少种树(l,r>0)

输入格式 Input Format
第一行n,m表示道路总长为n,共有m个操作
接下来m行为m个操作

输出格式 Output Format
对于每个k=2输出一个答案

样例输入 Sample Input
5 4 1 1 3 2 2 5 1 2 4 2 3 5

样例输出 Sample Output  
1 2

注释 Hint
范围:
20%的数据保证,n,m<=100
60%的数据保证,n <=1000,m<=50000
100%的数据保证,n,m<=50000

【参考程序】://树状数组

#include<cstring>
#include
<cstdio>
#include
<iostream>
using namespace std;

typedef 
int array[50010
];
array a,b;
int
 n,m;
int lowbit(int
 x)
{
    
return x^(x&(x-1
));
}
void add(int x,array &
c)
{
    
while (x<=
n)
    {
        c[x]
++
;
        x
+=
lowbit(x);
    }
}
int getsum(int
 x,array c)
{
    
int sum=0
;
    
while
 (x)
    {
        sum
+=
c[x];
        x
-=
lowbit(x);
    }
    
return
 sum;
}
int
 main()
{
    scanf(
"%d%d",&n,&
m);
    memset(a,
0,sizeof
(a));
    memset(b,
0,sizeof
(b));
    
int
 t,l,r;
    
for (int i=1;i<=m;i++
)
    {
        scanf(
"%d%d%d",&t,&l,&
r);
        
if (t==1
)
        {
            add(l,a); add(r,b);
        }
        
else

        {
            
int s=getsum(r,a)-getsum(l-1,b);
            printf(
"%d\n"
,s);
        }
    }
    
return 0
;
}
posted on 2009-08-21 10:59 开拓者 阅读(543) 评论(0)  编辑 收藏 引用 所属分类: 数据结构算法&例题Vijos OJ

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理