hdu 题目4217 Data Structure?(线段树,单点更新)
2013-08-19 21:58
369 查看
Data Structure?
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2133 Accepted Submission(s): 682
[align=left]Problem Description[/align]
Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data in a computer so that it can be used efficiently. Today let me introduce a data-structure-like problem
for you.
Original, there are N numbers, namely 1, 2, 3...N. Each round, iSea find out the Ki-th smallest number and take it away, your task is reporting him the total sum of the numbers he has taken away.
[align=left]Input[/align]
The first line contains a single integer T, indicating the number of test cases.
Each test case includes two integers N, K, K indicates the round numbers. Then a line with K numbers following, indicating in i (1-based) round, iSea take away the Ki-th smallest away.
Technical Specification
1. 1 <= T <= 128
2. 1 <= K <= N <= 262 144
3. 1 <= Ki <= N - i + 1
[align=left]Output[/align]
For each test case, output the case number first, then the sum.
[align=left]Sample Input[/align]
2
3 2
1 1
10 3
3 9 1
[align=left]Sample Output[/align]
Case 1: 3
Case 2: 14
单点更新,
找到该点,路径上个区间的len--;
/* 2013-8-19 15:13:47 Time: 562MS Memory: 6392K Author: zyh */ #define N 262200 #include<stdio.h> struct IntervalTree{ int L,R,len; }tree[N*4]; __int64 sum; //这里又错了一次,,注意要64位 void build(int p,int l,int r){ tree[p].L = l; tree[p].R = r; tree[p].len = r-l+1; if(l==r) return; int mid = (l+r)>>1; build(p<<1,l,mid); build(p<<1|1,mid+1,r); } void change(int p,int cnt){ tree[p].len--; if(tree[p].L==tree[p].R) { sum+=tree[p].L; return ; } if(cnt <= tree[p<<1].len){ change(p<<1,cnt); } else if(cnt>tree[p<<1].len){ change(p<<1|1,cnt-tree[p<<1].len); } } int main() { int t,n,k,i,tmp,j; scanf("%d\n",&t); for(j=1;j<=t;j++) { scanf("%d%d",&n,&k); build(1,1,n); sum=0; for(i=0;i<k;i++){ scanf("%d",&tmp); change(1,tmp); } printf("Case %d: %I64d\n",j,sum); } return 0; }
相关文章推荐
- 线段树 HDU 4217 Data Structure? 单点更新 区间查询
- HDU 4217 Data Structure?(线段树的查找和更新)
- HDU 1166 线段树基础题目单点更新
- hdu 3308 LCIS(线段树单点更新+区间合并)中等难度的题目
- HDU 2795 Billboard (线段树单点更新)
- hdu 1394 Minimum Inversion Number(线段树之 单点更新求逆序数)
- HDU 4819 Mosaic(二维线段树+单点更新)
- hdu_1754,线段树单点更新,求区间最值
- hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并
- HDU1394-Minimum Inversion Number(线段树单点更新)
- HDU 3874 Necklace(线段树啊 单点更新 区间求和)
- hdu1754 线段树(查找和单点更新)
- HDU 1754 I Hate It(线段树单点更新)
- HDU 1166 敌兵布阵 线段树入门_单点更新
- 线段树(单点更新) HDU 1754 I Hate It
- HDU 5316 Magician (线段树区间最值,单点更新)
- HDU 1161 敌兵布阵 线段树单点更新
- HDU 2795 Billboard // 线段树 单点更新
- HDU 1166-敌兵布阵(线段树:单点更新,区间求和)
- HDU 1166 - 敌兵布阵(线段树,单点更新)