當前插入的線段能完整覆蓋存在的幾條線段 樹狀數組 HDU 5372 Segment Game
阿新 • • 發佈:2017-07-02
刪除 som normal 時間 time ace others ram uniq
Total Submission(s): 1284 Accepted Submission(s): 375
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
Sample Output
Author UESTC
Source 2015 Multi-University Training Contest 7
題意:
http://acm.hdu.edu.cn/showproblem.php?
pid=5372
Segment Game
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1284 Accepted Submission(s): 375
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<=
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.
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
Author UESTC
Source 2015 Multi-University Training Contest 7
題意:
每次插入一個線段。或刪除一個已存在的線段,每次插入後輸出當前插入的線段能完整覆蓋存在的幾條線段。
題解:對於新插入的線段。查詢有多少個線段左端點大於等於該線段的左端點。
再查詢有多少個線段的右端點大於該線段右端點, 兩者之差就是答案。用兩個樹狀數組搞定。
時間復雜度nlogn
一共就4種情況。畫繪圖應該能發現。
#include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cmath> #include <cstring> #include <queue> #include <set> #include <map> #include <vector> using namespace std; template <class T> inline bool rd(T &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; } template <class T> inline void pt(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) pt(x / 10); putchar(x % 10 + '0'); } typedef pair<int, int> pii; typedef long long ll; const int N = 450007; struct Tree { int c[N], maxn; void init(int n) { maxn = n; for (int i = 0; i <= n; i++)c[i] = 0; } int lowbit(int x) { return x&-x; } int sum(int x) { int ans = 0; while (x)ans += c[x], x -= lowbit(x); return ans; } void update(int pos, int val) { while (pos <= maxn)c[pos] += val, pos += lowbit(pos); } }A, B; int n; set<pii> s; int op[N], l[N], r[N]; pii a[N]; vector<int>G; int main() { int cas = 0; while (cin>>n) { G.clear(); int top = 0; for (int i = 1; i <= n; i++) { rd(op[i]), rd(l[i]); if (op[i] == 0) { G.push_back(l[i]); r[i] = l[i] + (++top); G.push_back(r[i]); } } printf("Case #%d:\n", ++cas); sort(G.begin(), G.end()); G.erase(unique(G.begin(), G.end()), G.end()); top = 0; for (int i = 1; i <= n; i++) if (op[i] == 0) { l[i] = lower_bound(G.begin(), G.end(), l[i]) - G.begin() + 1; r[i] = lower_bound(G.begin(), G.end(), r[i]) - G.begin() + 1; a[++top] = { l[i], r[i] }; } A.init(G.size()); B.init(G.size()); int all = 0; for (int i = 1; i <= n; i++) { if (op[i] == 0) { int ans = B.sum(r[i]); ans -= A.sum(l[i]-1); pt(ans); putchar('\n'); A.update(l[i], 1); B.update(r[i], 1); all++; } else { A.update(a[l[i]].first, -1); B.update(a[l[i]].second, -1); all--; } } } return 0; }
當前插入的線段能完整覆蓋存在的幾條線段 樹狀數組 HDU 5372 Segment Game