设为首页 加入收藏

TOP

HDU 5372 Segment Game (树状数组+离散化)
2015-11-21 00:54:49 来源: 作者: 【 】 浏览:1
Tags:HDU 5372 Segment Game 离散

?

?

题面:

?

Segment Game

Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 968 Accepted Submission(s): 270



Problem Description Lillian is a clever girl so that she has lots of fans and often receives gifts from her fans.

One day Lillian gets some segments from her fans Lawson with lengths of 1,2,3... and she intends to display them by adding them to a number line.At the i-th add operation,she will put the segment with length of i on the number line.Every time she put the segment on the line,she will count how many entire segments on that segment.During the operation ,she may delete some segments on the line.(Segments are mutually independent)
Input There are multiple test cases.

The first line of each case contains a integer n — the number of operations(1<=n<= 2?105 , ∑n <= 7?105 )

Next n lines contain the descriptions of the operatons,one operation per line.Each operation contains two integers a , b.

if a is 0,it means add operation that Lilian put a segment on the position b(|b|< 109 ) of the line.
(For the i-th add operation,she will put the segment on [b,b+i] of the line, with length of i.)

if a is 1,it means delete operation that Lilian will delete the segment which was added at the b-th add operation.
Output For i-th case,the first line output the test case number.

Then for each add operation,ouput how many entire segments on the segment which Lillian newly adds.
Sample Input
3
0 0
0 3
0 1
5
0 1
0 0
1 1
0 1
0 0

Sample Output
Case #1:
0
0
0
Case #2:
0
1
0
2
Hint
For the second case in the sample:

At the first add operation,Lillian adds a segment [1,2] on the line.

At the second add operation,Lillian adds a segment [0,2] on the line.

At the delete operation,Lillian deletes a segment which added at the first add operation.

At the third add operation,Lillian adds a segment [1,4] on the line.

At the fourth add operation,Lillian adds a segment [0,4] on the line
 

Source 2015 Multi-University Training Contest 7
解题:

?

树状数组本身并不难,但是能够用得好,想得出来怎么用还是需要很娴熟的。感觉我写的特别麻烦,T了好多次,改了快速读入,快速输出,最后才发现是有个地方写错了,G++600MS,C++1400MS,真是不懂啊。不过也算是调出来了,也算是树状数组博客第一篇吧!

?

代码:

?

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #define maxn 200010 using namespace std; struct operation { //id存这是第几次操作,typ操作类型,val是操作的值 //times是第几次加操作,只对加操作有用,l,r分别是加操作的左右端点 int id; int typ; int val; int l,r; int times; }info[maxn],tmpstore[maxn]; //rgt,lft分别是树状数组的数组 int rgt[maxn],lft[maxn]; //get_sum求1到x的前缀和 int get_sum1(int x) { int res=0; while(x>0) { res+=lft[x]; x-=(x&(-x)); } return res; } int get_sum2(int x) { int res=0; while(x>0) { res+=rgt[x]; x-=(x&(-x)); } return res; } //更新x位置的值,以及其树状数组向上相关的值 void update1(int x,int val) { while(x
      
       9)pt(x/10); putchar(x%10+'0'); } //快速读入一个整数 inline bool rd(int &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c<'0' || c>'9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } int main() { //太多了,用到了再说吧 int n,oper,p,cnt,le,ri,ansl,ansr,curl,curr,cas=1,i,ll,rr,cs,id; while(~scanf(%d,&n)) { //cs计一共多少次加操作 cs=0; printf(Case #%d: ,cas++); memset(rgt,0,sizeof(int)*(n+1)); memset(lft,0,sizeof(int)*(n+1)); //le,ri用来离散化 le=1; ri=1; cnt=1; //初始值设为-1,树状数组下标不能从0开始,否则会死循环 curl=-1; curr=-1; for(i=1;i<=n;i++) { rd(info[i].typ); rd(info[i].val); //标记第几次操作 info[i].id=i; //加操作 if(info[i].typ==0) { //左右区间赋值 info[i].l=info[i].val; info[i].r=info[i].l+cnt++; tmpstore[cs].l=info[i].l; tmpstore[cs].r=info[i].r; tmpstore[cs].id=info[i].id; cs++; //记录是第几次加操作 tmpstore[cs].times=cs; } } //先对l排序,离散化 sort(tmpstore,tmpstore+cs,cmp2); for(i=0;i
       
        

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++调用Lua的性能测试 下一篇c++函数指针与默认参数

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: