zoukankan      html  css  js  c++  java
  • 【模板】HDU 1541 树状数组

    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,'
    ');
        //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;
        }
    }
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    jQuery5事件相关
    jQuery4操作表单+属性+样式
    ueditor不能上传mp4格式的视频--解决方案
    笔记本怎么设置WIfi热点
    em rem vw vh
    字体的使用,坑爹啊!
    font的使用
    photoshop简单切图
    HTTP的学习
    call apply bind的联系与区别
  • 原文地址:https://www.cnblogs.com/SuuT/p/9085011.html
Copyright © 2011-2022 走看看