-
Notifications
You must be signed in to change notification settings - Fork 2
/
HDU-hangzhou-1008.cpp
124 lines (111 loc) · 1.91 KB
/
HDU-hangzhou-1008.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
ID: mfs6174
PROG: BIT
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
//ofstream ouf("ti.out");
const int maxlongint=2147483647;
#define MAXN 110000
int f[MAXN];
int rr[MAXN];
#define sf scanf
#define pf printf
struct D
{
int h,d;
bool operator < (const D &x) const
{
return d<x.d;
}
};
struct P
{
int d,a,b,h;
bool operator <(const P &x) const
{
return d<x.d;
}
};
D shu[MAXN];
P cha[MAXN];
//f[x]总是表示x-lowbit+1 到 x的和
//注意up中的n表示数组最大范围,如果离散化就是数字数,在线算法不方便离散化就是数字最大范围,灵活处理
//注意从1开始,如果有0号数组,可以每个加1表示
inline int lowbit(int x)
{
return x&(x^(x-1));
}
void upc(int x,int d,int n) //更新,x是位置,d是增加量,n是上界
{
while (x<=n)
{
f[x]+=d;
x+=lowbit(x);
}
}
int downs(int x) //查找
{
int s=0;
while (x>0)
{
s+=f[x];
x-=lowbit(x);
}
return s;
}
int i,j,k,t,n,m,a,b,c,md,ss;
int zz,zu;
int main()
{
freopen("ti.in","r",stdin);
sf("%d",&zu);
for (zz=1;zz<=zu;zz++)
{
sf("%d%d",&n,&m);
for (i=1;i<=n;i++)
{
sf("%d",&shu[i].d);
shu[i].h=i;
}
for (i=1;i<=m;i++)
{
sf("%d%d%d",&cha[i].a,&cha[i].b,&cha[i].d);
cha[i].h=i;
cha[i].a++;
cha[i].b++;
}
sort(&cha[1],&cha[m+1]);
sort(&shu[1],&shu[n+1]);
memset(f,0,sizeof(f));
k=1;
for (i=1;i<=m;i++)
{
for (j=k;j<=n;j++)
{
if (shu[j].d<=cha[i].d)
upc(shu[j].h,1,n);
else
{
k=j;
break;
}
}
if (j==n+1)k=j;
t=downs(cha[i].b)-downs(cha[i].a-1);
rr[cha[i].h]=t;
}
pf("Case %d:\n",zz);
for (i=1;i<=m;i++)
pf("%d\n",rr[i]);
}
return 0;
}