博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2015 Multi-University Training Contest 7 hdu 5372 Segment Game
阅读量:4599 次
发布时间:2019-06-09

本文共 3500 字,大约阅读时间需要 11 分钟。

Segment Game

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 446    Accepted Submission(s): 95

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<=
2105,n<=7105)
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
 1004
 
解题:树状数组,神奇。。。哎。。这么简单怎么当场没想到
 
1 #include 
2 using namespace std; 3 const int maxn = 400010; 4 int op[maxn],L[maxn],R[maxn],n; 5 int C[2][maxn],Li[maxn]; 6 inline int lowbit(int x) { 7 return x&(-x); 8 } 9 void update(int i,int val,int o) {10 for(; i < maxn; i += lowbit(i))11 C[o][i] += val;12 }13 int calc(int i,int o) {14 int sum = 0;15 for(; i; i -= lowbit(i))16 sum += C[o][i];17 return sum;18 }19 int main() {20 int add,tot,cnt,cs = 1;21 while(~scanf("%d",&n)) {22 memset(C,0,sizeof C);23 for(int i = add = tot = cnt = 0; i < n; ++i) {24 scanf("%d%d",op+i,L+i);25 if(op[i] == 0) {26 R[i] = L[i] + (++add);27 Li[tot++] = L[i];28 Li[tot++] = R[i];29 }30 }31 sort(Li,Li + tot);32 tot = unique(Li,Li + tot) - Li;33 printf("Case #%d:\n",cs++);34 for(int i = 0; i < n; ++i) {35 if(!op[i]) {36 int LL = lower_bound(Li,Li + tot,L[i]) - Li + 1;37 int RR = lower_bound(Li,Li + tot,R[i]) - Li + 1;38 printf("%d\n",calc(RR,1) - calc(LL-1,0));39 L[cnt] = LL;40 R[cnt++] = RR;41 update(LL,1,0);42 update(RR,1,1);43 } else {44 update(L[L[i]-1],-1,0);45 update(R[L[i]-1],-1,1);46 }47 }48 }49 return 0;50 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4722632.html

你可能感兴趣的文章
33. Search in Rotated Sorted Array
查看>>
461. Hamming Distance
查看>>
Python垃圾回收机制详解
查看>>
{面试题1: 赋值运算符函数}
查看>>
Node中没搞明白require和import,你会被坑的很惨
查看>>
Python 标识符
查看>>
Python mysql 创建连接
查看>>
企业化的性能测试简述---如何设计性能测试方案
查看>>
centos7 安装中文编码
查看>>
POJ - 3683 Priest John's Busiest Day
查看>>
正则表达式start(),end(),group()方法
查看>>
vuejs 学习旅程一
查看>>
javascript Date
查看>>
linux常用命令2
查看>>
狼图腾
查看>>
13、对象与类
查看>>
Sublime Text3 个人使用心得
查看>>
jquery 编程的最佳实践
查看>>
MeetMe
查看>>
IP报文格式及各字段意义
查看>>