您的位置:首页 > 其它

HDU3727--Jewel (主席树 静态区间第k大)

2014-11-28 19:57 176 查看

Jewel

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 985 Accepted Submission(s): 247


[align=left]Problem Description[/align]
Jimmy wants to make a special necklace for his girlfriend. He bought many beads with various sizes, and no two beads are with the same size. Jimmy can't remember all the details about the beads, for the necklace is so long. So he turns to you for help.

Initially, there is no bead at all, that is, there is an empty chain. Jimmy always sticks the new bead to the right of the chain, to make the chain longer and longer. We number the leftmost bead as Position 1, and the bead to its right as Position 2, and so on. Jimmy usually asks questions about the beads' positions, size ranks and actual sizes. Specifically speaking, there are 4 kinds of operations you should process:

Insert x
Put a bead with size x to the right of the chain (0 < x < 231, and x is different from all the sizes of beads currently in the chain)
Query_1 s t k
Query the k-th smallest bead between position s and t, inclusive. You can assume 1 <= s <= t <= L, (L is the length of the current chain), and 1 <= k <= min (100, t-s+1)
Query_2 x
Query the rank of the bead with size x, if we sort all the current beads by ascent order of sizes. The result should between 1 and L (L is the length of the current chain)
Query_3 k
Query the size of the k-th smallest bead currently (1 <= k <= L, L is the length of the current chain)

[align=left]Input[/align]
There are several test cases in the input. The first line for each test case is an integer N, indicating the number of operations. Then N lines follow, each line contains one operation, as described above.

You can assume the amount of "Insert" operation is no more than 100000, and the amounts of "Query_1", "Query_2" and "Query_3" are all less than 35000.
There are several test cases in the input. The first line for each test case is an integer N, indicating the number of operations. Then N lines follow, each line contains one operation, as described above.

You can assume the amount of "Insert" operation is no more than 100000, and the amounts of "Query_1", "Query_2" and "Query_3" are all less than 35000.Query the rank of the bead with size x, if we sort all the current beads by ascent order of sizes. The result should between 1 and L (L is the length of the current chain)
Query_3 k
Query the size of the k-th smallest bead currently (1 <= k <= L, L is the length of the current chain)

[align=left]Output[/align]
Output 4 lines for each test case. The first line is "Case T:", where T is the id of the case. The next 3 lines indicate the sum of results for Query_1, Query_2 and Query_3, respectively.

[align=left]Sample Input[/align]

10

Insert 1
Insert 4

Insert 2

Insert 5
Insert 6

Query_1 1 5 5
Query_1 2 3 2

Query_2 4

Query_3 3

Query_3 1

[align=left]Sample Output[/align]

Case 1:
10
3
5
貌似很水的一道题,,主席树裸题,不知道为什么现场就几个队了这个题。。对于查询某个数的排名,我是用树状数组搞的。。和主席树没有联系
注意HDU上的题目有一点问题,,不是231 而是2的31次方


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 2e5+100;
int lson[maxn*20],rson[20*maxn],c[maxn*20];
int tree[maxn],tot,idx,maxv;
int build (int l,int r)
{
int root = tot++;
c[root] = 0;
if (l != r)
{
int mid =(l + r) >> 1;
lson[root] = build(l,mid);
rson[root] = build(mid+1,r);
}
return root;
}
int update(int root,int pos,int val)
{
int newroot = tot++;
int tmp = newroot;
int l = 1,r = maxv;
c[newroot] = c[root] + val;
while (l < r)
{
int mid = (l + r) >> 1;
if (pos <= mid)
{
rson[newroot] = rson[root];
root = lson[root];
lson[newroot] = tot++;
newroot = lson[newroot];
r = mid;
}
else
{
lson[newroot] = lson[root];
root = rson[root];
rson[newroot] = tot++;
newroot = rson[newroot];
l = mid + 1;
}
c[newroot] = c[root] + val;
}
return tmp;
}
int query(int left,int right,int k)
{
int l_root = tree[left-1];
int r_root = tree[right];
int l = 1, r = maxv;
while (l < r)
{
int mid = (l + r) >> 1;
if (c[lson[r_root]] - c[lson[l_root]] >= k)
{
r = mid;
l_root = lson[l_root];
r_root = lson[r_root];
}
else
{
l = mid + 1;
k -=  c[lson[r_root]] - c[lson[l_root]];
l_root = rson[l_root];
r_root = rson[r_root];
}
}
return l;
}
int query(int root,int l,int r,int k)
{
if (l == r)
return l;
int mid = (l + r) >> 1;
if (k <= mid)
return query(lson[root],l,mid,k);
else
return c[lson[root]] + query(rson[root],mid+1,r,k);
}
ll bit_arr[maxn];
inline int lowbit (int x)
{
return x & -x;
}
void ADD(int x)
{
while (x < maxn)
{
bit_arr[x]++;
x += lowbit (x);
}
}
ll get_sum(int x)
{
ll res = 0;
while (x)
{
res += bit_arr[x];
x -= lowbit (x);
}
return res;
}
struct
{
int x,y,k,flag;
}Q[maxn];
ll vec[maxn];
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int n,cas = 1;
while (~scanf ("%d",&n))
{
memset(bit_arr,0,sizeof(bit_arr));
printf("Case %d:\n",cas++);
tot = idx = 0;
for (int i = 0; i < n; i++)
{
char op[15];
scanf ("%s",op);
if (strcmp(op,"Insert") == 0)
{
int x;
scanf ("%d",&x);
Q[i].flag = 0;
Q[i].x = x;
vec[idx++] = x;
}
if (strcmp(op,"Query_1") == 0)
{
int u,v,k;
scanf ("%d%d%d",&u,&v,&k);
Q[i].flag = 1;
Q[i].x = u,Q[i].y = v,Q[i].k = k;
}
if (strcmp(op,"Query_2") == 0)
{
int x;
scanf ("%d",&x);
Q[i].flag = 2;
Q[i].x = x;
}
if (strcmp(op,"Query_3") == 0)
{
int k;
scanf ("%d",&k);
Q[i].flag = 3;
Q[i].x = k;
}
}
sort(vec,vec+idx);
idx = unique(vec,vec+idx) - vec;
maxv = idx ;
tree[0] = build(1,maxv);
int now = 1;
ll ans1 = 0, ans2 = 0, ans3 = 0;
for (int i = 0; i < n; i++)
{
if (Q[i].flag == 0)
{
int tmp = lower_bound(vec,vec+idx,Q[i].x) - vec + 1;
tree[now] = update(tree[now-1],tmp,1);
ADD(tmp);
now++;
}
if (Q[i].flag == 1)
{
ans1 += vec[query(Q[i].x,Q[i].y,Q[i].k)-1];
}
if (Q[i].flag == 2)
{
int tmp = lower_bound(vec,vec+idx,Q[i].x) - vec + 1;
ans2 += get_sum(tmp);
}
if (Q[i].flag == 3)
{
ans3 += vec[query(1,now-1,Q[i].x)-1];
}
}
printf("%I64d\n%I64d\n%I64d\n",ans1,ans2,ans3);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: