hdu 5372 Segment Game 2015多校联合训练赛#7 树状数组
来源:互联网 发布:淘宝网休闲鞋女 编辑:程序博客网 时间:2024/06/05 09:57
Segment Game
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 244 Accepted Submission(s): 42
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)
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.
The first line of each case contains a integer n — the number of operations(1<=n<=
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|<
(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.
Then for each add operation,ouput how many entire segments on the segment which Lillian newly adds.
Sample Input
30 00 30 150 10 01 10 10 0
Sample Output
Case #1:000Case #2:0102HintFor 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
先离散化
然后由于长度是递增的。两个树状数组分别记录,起点和终点的个数。
那么加入线段时l,r(起始,终止位置), <= r的终止个数 - < l 的起始位置就是答案了。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<vector>using namespace std;#define maxn 400007void add(int *tree,int p,int n){ while(p < maxn){ tree[p] += n; p += (p&(-p)); }}int query(int *tree,int p){ int ans = 0; while(p > 0){ ans += tree[p]; p -= (p&(-p)); } return ans;}int w[maxn],ls[maxn],rs[maxn];int num[maxn];int en[maxn],be[maxn];vector<int> seg;map<int,int>haha;int main(){ int n,tt=1; //freopen("1004.in","r",stdin); //freopen("1004x.out","w",stdout); while(scanf("%d",&n) != EOF){ haha.clear(); seg.clear(); memset(be,0,sizeof(be)); memset(en,0,sizeof(en)); int ty,p; for(int i = 0;i < n; i++){ scanf("%d%d",&ty,&p); if(ty == 0){ w[i] = 1; ls[i] = p; rs[i] = p+seg.size()+1; haha[p] = 0; haha[p+seg.size()+1] = 0; seg.push_back(i); } else { p = seg[p-1]; w[i] = -1; ls[i] = ls[p]; rs[i] = rs[p]; } } int cnt = 2; map<int,int>::iterator it = haha.begin(); while(it != haha.end()){ it->second = cnt++; it++; } for(int i = 0;i < n; i++){ ls[i] = haha[ls[i]]; rs[i] = haha[rs[i]]; } printf("Case #%d:\n",tt++); for(int i = 0;i < n; i++){ if(w[i] == 1){ printf("%d\n",query(en,rs[i]) - query(be,ls[i]-1) ); add(en,rs[i],1); add(be,ls[i],1); } else { add(en,rs[i],-1); add(be,ls[i],-1); } } } return 0;}
0 0
- hdu 5372 Segment Game 2015多校联合训练赛#7 树状数组
- HDU 5372 Segment Game(树状数组)——多校练习7
- HDU 5372 Segment Game 树状数组
- 【树状数组】 HDU 5372 Segment Game
- hdu 5372 Segment Game(树状数组)
- HDU 5372 - Segment Game(树状数组)
- 【HDU 5372】Segment Game(树状数组)
- HDU 5372 Segment Game 树状数组
- HDU 5372 Segment Game(树状数组)
- [HDU 5372] Segment Game (树状数组)
- [树状数组] hdu 5372 Segment Game
- HDU 5372 Segment Game (MUT#7 树状数组+离散化)
- hdu 5338 ZZX and Permutations 2015多校联合训练赛,贪心,线段树,树状数组
- 多校第七场 1004 hdu 5372 Segment Game(树状数组)
- hdu-5372 Segment Game(离散树状数组)(2015多校contest 7)
- 2015多校第7场 HDU 5372 Segment Game 树状数组,思维
- Segment Game (hdu 5372 树状数组+离散化)
- hdu 5372 Segment Game(树状数组+离散化)
- python学习笔记3
- iOS __weak与__block修饰符到底有什么区别
- Java基础-多线程
- 32位和64位系统区别及常用数据类型所占字节数
- Unity3D音效问题
- hdu 5372 Segment Game 2015多校联合训练赛#7 树状数组
- HDU 5379 Mahjong tree
- shell基础(二)
- 网页编码
- 简单谈谈3D打印培训的发展和前景
- poj1664 放苹果 (母函数)
- struts2学习笔记(八)国际化
- C语言的static关键字
- Java InputStreamReader和OutputStreamWriter