Feed the dogs
Time Limit:6000MS Memory Limit:65536K
Total Submit:1387 Accepted:222
Description
Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on one line, numbered from 1 to n, the leftmost one is 1, the second one is 2, and so on. In each feeding, Jiajia choose an inteval[i,j], select the k-th pretty dog to feed. Of course Jiajia has his own way of deciding the pretty value of each dog. It should be noted that Jiajia do not want to feed any position too much, because it may cause some death of dogs. If so, Wind will be angry and the aftereffect will be serious. Hence any feeding inteval will not contain another completely, though the intervals may intersect with each other.
Your task is to help Jiajia calculate which dog ate the food after each feeding.
Input
The first line contains n and m, indicates the number of dogs and the number of feedings.
The second line contains n integers, describe the pretty value of each dog from left to right. You should notice that the dog with lower pretty value is prettier.
Each of following m lines contain three integer i,j,k, it means that Jiajia feed the k-th pretty dog in this feeding.
You can assume that n<100001 and m<50001.
Output
Output file has m lines. The i-th line should contain the pretty value of the dog who got the food in the i-th feeding.
Sample Input
7 2
1 5 2 6 3 7 4
1 5 3
2 7 1
Sample Output
3
2
Source
POJ Monthly--2006.02.26,zgl & twb
#include
<
iostream
>
#include
<
algorithm
>
using
namespace
std;
const
int
MAXN
=
100001
;
const
int
MAXM
=
50001
;
struct
PosData
{
int
b, e;
int
k;
int
index;
}
;
struct
Adata
{
int
index;
int
incode;
int
data;
}
;
Adata a[MAXN];
int
aa[MAXN];
PosData d[MAXM];
int
f[
3
*
(MAXN
+
1
)]
=
{
0
}
;
int
n, m;
int
out
[MAXM];
inline
int
inorder(PosData x, PosData y)
{
return
x.b
<
y.b;
}
inline
int
inorder2(Adata x, Adata y)
{
return
x.data
<
y.data;
}
inline
int
inorder3(Adata x, Adata y)
{
return
x.index
<
y.index;
}
void
initTree()
{
memset(f,
0
,
sizeof
(f));
}
void
insertTree(
int
v)
{
int
l
=
1
, r
=
MAXN;
int
c
=
1
;
int
mid;
while
(l
<
r)
{
mid
=
(l
+
r)
/
2
;
f[c]
++
;
if
(v
>
mid)
{
c
=
c
*
2
+
1
;
l
=
mid
+
1
;
}
else
{
c
=
c
*
2
;
r
=
mid;
}
}
f[c]
++
;
}
void
delTree(
int
v)
{
int
l
=
1
, r
=
MAXN;
int
c
=
1
;
int
mid;
while
(l
<
r)
{
mid
=
(l
+
r)
/
2
;
f[c]
--
;
if
(v
>
mid)
{
c
=
c
*
2
+
1
;
l
=
mid
+
1
;
}
else
{
c
=
c
*
2
;
r
=
mid;
}
}
f[c]
--
;
}
int
searchTree(
int
k)
{
int
l
=
1
, r
=
MAXN;
int
c
=
1
;
int
mid;
while
(l
<
r)
{
mid
=
(l
+
r)
/
2
;
if
(k
<=
f[
2
*
c])
{
c
=
2
*
c;
r
=
mid;
}
else
{
k
-=
f[
2
*
c];
c
=
2
*
c
+
1
;
l
=
mid
+
1
;
}
}
return
l;
}
int
main()
{
int
i, j;
scanf(
"
%d%d
"
,
&
n,
&
m);
for
(i
=
1
; i
<=
n; i
++
)
{
scanf(
"
%d
"
,
&
a[i].data);
a[i].index
=
i;
}
sort(a
+
1
, a
+
n
+
1
, inorder2);
//
离散化
for
(i
=
1
; i
<=
n; i
++
)
{
a[i].incode
=
i;
aa[i]
=
a[i].data;
}
sort(a
+
1
, a
+
n
+
1
, inorder3);
for
(i
=
0
; i
<
m; i
++
)
{
scanf(
"
%d%d%d
"
,
&
d[i].b,
&
d[i].e,
&
d[i].k);
d[i].index
=
i;
}
sort(d, d
+
m, inorder);
for
(j
=
d[
0
].b; j
<=
d[
0
].e; j
++
)
insertTree(a[j].incode);
out
[d[
0
].index]
=
aa[searchTree(d[
0
].k)];
for
(i
=
1
; i
<
m; i
++
)
{
if
(d[i
-
1
].e
<
d[i].b)
{
for
(j
=
d[i
-
1
].b; j
<=
d[i
-
1
].e; j
++
)
delTree(a[j].incode);
for
(j
=
d[i].b; j
<=
d[i].e; j
++
)
insertTree(a[j].incode);
}
else
{
for
(j
=
d[i
-
1
].b; j
<
d[i].b; j
++
)
{
delTree(a[j].incode);
}
for
(j
=
d[i
-
1
].e
+
1
; j
<=
d[i].e; j
++
)
{
insertTree(a[j].incode);
}
}
out
[d[i].index]
=
aa[searchTree(d[i].k)];
}
for
(i
=
0
; i
<
m; i
++
)
printf(
"
%d\n
"
,
out
[i]);
return
0
;
}
posted on 2006-09-13 23:40
豪 阅读(1910)
评论(6) 编辑 收藏 引用 所属分类:
ACM题目