【模板】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,'\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 ;
}
int dir[][] = { ,,,,-,,,- };
const int maxn = 4e5 + ;
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 = ;
while (x) {
res += d[x];
x -= lowbit(x);
}
return res;
}
struct node { int x, y;
node(int x = , int y = ) :x(x), y(y) {} }; void Run() { } void smain() {
while (cin >> n)
{
mmm(d, ); mmm(level, ); rep(i, , n) {
int x, y;
cin >> x >> y;
x++;
level[query(x)]++;
//cout << query(x) << endl;
add(x, ); }
rep(i, , n - )cout << level[i] << endl;
}
}
上一篇:68.纯 CSS 创作一本色卡


下一篇:install root certificate failed, Please run as administrator/root/sudo