Vasya is interested in arranging dominoes. He is fed up with common dominoes and he uses the dominoes of different heights. He putn dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base. Thei-th domino has the coordinate xi and the height hi. Now Vasya wants to learn for every domino, how many dominoes will fall if he pushes it to the right. Help him do that.
Consider that a domino falls if it is touched strictly above the base. In other words, the fall of the domino with the initial coordinatex and height h leads to the fall of all dominoes on the segment[x + 1, x + h - 1].
The first line contains integer n (1 ≤ n ≤ 105) which is the number of dominoes. Then follown lines containing two integers xi and hi ( - 108 ≤ xi ≤ 108, 2 ≤ hi ≤ 108) each, which are the coordinate and height of every domino. No two dominoes stand on one point.
Print n space-separated numbers zi — the number of dominoes that will fall if Vasya pushes thei-th domino to the right (including the domino itself).
4 16 5 20 5 10 10 18 2
3 1 4 1
4 0 10 1 5 9 10 15 10
4 1 2 1 思路: 先按牌的坐标排序,从后往前,如果第 K 张牌可以推到第 i 张, 那么理所当然 第K张牌可以推到 I 的推到的牌 + 第 I 到第K张中间的牌数;把I从栈里面pop出去; 当第K张牌前的牌,如果它能推到K,那肯定能推到I,如果不能推到K,理所当然也不能推到I;#include<stdio.h> #include<string.h> #include<math.h> #include<string> #include<iostream> #include<algorithm> #include<vector> #include<queue> #include<list> #include<map> #include<set> #include<stack> using namespace std; int n; struct node{ int id; int x,h; int len; int i; void init(int i){ id = i; scanf("%d%d",&x,&h); len = x + h - 1; } }; node a[100010]; int ans[100010]; bool cmp(node a, node b){ return a.x < b.x; } stack<node>que; void deal(int i){ int id = a[i].id; int x = a[i].len; ans[id] = 1; while(!que.empty()){ node tp = que.top(); if(x >= tp.x) que.pop(); else break; ans[id] = max(ans[id],tp.i - a[i].i + ans[tp.id]); } que.push(a[i]); } void work(){ while(!que.empty()) que.pop(); ans[a[n-1].id] = 1; a[n-1].i = n - 1; que.push(a[n-1]); for(int i = n-2; i >= 0 ;i--){ a[i].i=i; deal(i); } } int main() { // freopen("in.in","r",stdin); while(~scanf("%d",&n)){ for(int i=0; i<n; i++) a[i].init(i); sort(a,a+n,cmp); work(); for(int i =0; i< n-1;i++) printf("%d ", ans[i]); printf("%d\n",ans[n-1]); } return 0; }