HDU 5372 Segment Game
그래서 쓸 수 있어 요. 왼쪽 점 > = 현재 선분 왼쪽 점 의 선분 수 - 오른쪽 점 > 현재 선분 오른쪽 점 의 선분 수, 바로 답 입 니 다.
Segment Game
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 583 Accepted Submission(s): 146
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
#include <bits/stdc++.h>
using namespace std;
#define prt(k) cerr<<#k" = "<<k<<endl
typedef long long ll;
const ll inf = 0x3f3f3f3f;
const int N = 202002 << 1;
/****************/
struct Tree
{
int tree[N];
int low(int x) { return x & -x; }
void clear() { memset(tree,0, sizeof tree); }
void add(int p, int x)
{
if (p<=0) return;
for (int i=p;i<N;i+=low(i))
tree[i]+=x;
}
int sum(int p)
{
int r = 0;
for (int i=p;i>0;i-=low(i)) r += tree[i];
return r;
}
}TL, TR;
/****************/
int n;
struct data
{
int op, id;
}a[N];
struct P
{
int l, r;
}p[N];
int t[N<<1], tt;
int id[N];
int main()
{
int ca = 1;
while (scanf("%d",&n)==1) {
int i = 0;
for (int i=1;i<=n;i++) scanf("%d%d", &a[i].op, &a[i].id);
tt = 0;
memset(id, -1, sizeof id);
for (int j=1;j<=n;j++) {
int op = a[j].op;
if (op==0) {
++i;
p[i].l = a[j].id;
p[i].r = a[j].id + i;
id[j] = i;
t[tt++] = p[i].l;
t[tt++] = p[i].r;
}
else {
;
}
}
int m = i;
sort(t, t+tt);
tt = unique(t, t+tt) - t;
map<int, int> Lisan;
for (int i=0;i<tt;i++) {
Lisan[t[i]] = i + 1;
}
for (int i=1;i<=m;i++) {
p[i].l = Lisan[p[i].l];
p[i].r = Lisan[p[i].r];
}
TL.clear();
TR.clear();
printf("Case #%d:
", ca++);
for (int i=1;i<=n;i++) {
if (a[i].op == 0) {
int j = id[i];
int ans = j - 1 - TL.sum(p[j].l - 1);
ans -= j - 1 - TR.sum(p[j].r);
printf("%d
", ans);
TL.add(p[j].l, 1);
TR.add(p[j].r, 1);
}
if (a[i].op == 1) {
int j = a[i].id;
TL.add(p[j].l, -1);
TR.add(p[j].r, -1);
}
}
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.