zoukankan      html  css  js  c++  java
  • 树状数组POJ2352星星

    http://poj.org/problem?id=2352

    这道题的题意对于住学者应该比较难理解,但是如果弄明白他的意思的话,你就会发现这就是赤裸裸的树状数组,哎,欺负我不懂是吧,当时读题读啦好久,好啦,下面说一下他的意思吧。。

    由于题目已经说明y的坐标是递增顺序,所以直接考虑x的坐标就可以啦。每次当有x输入时,都求出sum(x+1)的值(注意呦,在树状数组中,可是从1开始的,而题目中的输入是包含0的),并把result[sum(x+1)]++;再更新一下,就好啦啦啦啦。

    还是要注意一点,用c++提交超时,还是用c吧!!!!!!!!!!!!!!!!

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 #include<iostream>
     5 const int N=32005;
     6 using namespace std;
     7 int c[N+1];
     8 int n;
     9 int lowbit(int i){
    10 return i&(-i);
    11 }
    12 void add(int i,int x)
    13 {
    14     while(i<=N){
    15         c[i]+=x;
    16         i+=lowbit(i);
    17     }
    18 }
    19 int sum(int i)
    20 {
    21     int s=0;
    22     while(i>0){
    23         s+=c[i];
    24         i-=lowbit(i);
    25     }
    26     return s;
    27 }
    28 int main(){
    29 int a,b;
    30 int result[N];
    31 scanf("%d",&n);
    32        //memset(result,0,sizeof(result));
    33         //memset(c,0,sizeof(c));
    34     for(int i=0;i<n;i++){
    35         scanf("%d%d",&a,&b);
    36         int temp=sum(a+1);
    37         result[temp]++;
    38          add(a+1,1);
    39     }
    40     for(int i=0;i<n;i++){
    41         cout<<result[i]<<endl;
    42     }
    43 }
    View Code
    你若盛开,清风自来...
  • 相关阅读:
    python闭包&深浅拷贝&垃圾回收&with语句
    Python基本数据类型
    面向对象
    四则运算2
    周总结01
    软件工程个人作业01
    java web中乱码的种类和一些解决方式
    java web 增加信息课堂测试00
    课程00作业
    动手动脑07
  • 原文地址:https://www.cnblogs.com/shangjindexiaoqingnian/p/5762612.html
Copyright © 2011-2022 走看看