Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

y. 第一道树状数组

Posted by 201030720525 at 2011-07-20 16:09:35 on Problem 2352
#include "iostream"
#include "cstring"
using namespace std;
#define MAX 32005
int n, c[MAX], cnt[MAX];

int lowbit(int x) {
    return x & (-x);
}

void update(int x, int v) {
    for(int i = x; i <= MAX; i += lowbit(i)) c[i] += v;
}

int sum(int x) {
    int res = 0;
    for(int i = x; i; i -= lowbit(i)) res += c[i];
    return res;
}

int main() {
    int x, y;
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> x >> y;
        int rank = sum(x + 1);
        cnt[rank]++;
        update(x + 1, 1);
    }
    for(int i = 0; i < n; i++)
        cout << cnt[i] << endl;
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator