码迷,mamicode.com
首页 > 编程语言 > 详细

【模板】HDU 1541 树状数组

时间:2018-05-24 21:13:21      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:func   string   als   clu   inf   ret   ++i   queue   main   

http://acm.hdu.edu.cn/showproblem.php?pid=1541

题意:给你一堆点,每个点右一个level,为其右下方所有点的数量之和,求各个level包含的点数。

题解:由于输入是有序的,每读进一对x,y 只需要考虑之前读入的数据就行了(之后的必定在其右上方)。如何计算他的level?只需从其X坐标开始往右所有的X坐标上的点数求和即可。然后再让自己的X坐标点数++。这是单点更新,求前缀和的操作,可以用树状数组。

 

坑:题目条件有误。

  add函数里x<=maxn而不是n,具体原理应该是会处理到n外面吧

#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<string>
#include<climits>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<fstream>
#include<iostream>
#include<functional>
#include<algorithm>
#include<memory.h>
//#define INF LONG_MAX
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n)  for(int i =(t);i<=(n);++i)
#define per(i,n,t)  for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
#define mmm(a,b) memset(a,b,sizeof(a))
//std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void smain();
#define ONLINE_JUDGE
int main() {
    //ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("C:\\Users\\SuuTT\\Desktop\\test\\in.txt", "r", stdin);
    freopen("C:\\Users\\SuuTT\\Desktop\\test\\out.txt", "w", stdout);
    //ifstream in;
    //string filename;
    //getline(cin, filename,‘\n‘);
    //in.open(filename);

    long _begin_time = clock();
#endif
    smain();
#ifndef ONLINE_JUDGE
    long _end_time = clock();
    printf("time = %ld ms.", _end_time - _begin_time);
#endif
    return 0;
}
int dir[4][2] = { 1,0,0,1,-1,0,0,-1 };
const int maxn = 4e5 + 5;
int n, m;
ll a[maxn];
int d[maxn];
int level[maxn];
int lowbit(int x) { return x & (-x); }
void add(int x, int v) {//a[x]+=v;
    while (x <= maxn) {
        d[x] += v;
        x += lowbit(x);
    }

}
int query(int x) {
    int res = 0;
    while (x) {
        res += d[x];
        x -= lowbit(x);
    }
    return res;
}
struct node {
    
    int x, y;
    node(int x = 0, int y = 0) :x(x), y(y) {}


};

void Run() {

}

void smain() {
    while (cin >> n)
    {
        mmm(d, 0); mmm(level, 0);
        
        rep(i, 1, n) {
            int x, y;
            cin >> x >> y;
            x++;
            level[query(x)]++;
            //cout << query(x) << endl;
            add(x, 1);
            
        }
        rep(i, 0, n - 1)cout << level[i] << endl;
    }
}

 

【模板】HDU 1541 树状数组

标签:func   string   als   clu   inf   ret   ++i   queue   main   

原文地址:https://www.cnblogs.com/SuuT/p/9085011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!