首页 > 其他分享 >Code Forces 652D Nested Segments(离散化+树状数组)

Code Forces 652D Nested Segments(离散化+树状数组)

时间:2022-10-18 14:05:56浏览次数:70  
标签:cnt Code 652D int MAX Segments num include sum

 Nested Segments

time limit per test

memory limit per test

input

output

n

Input

n (1 ≤ n ≤ 2·105) — the number of segments on a line.

n lines contains two integers li and ri(9 ≤ li < ri ≤ 109) — the coordinates of the left and the right ends of the i-th segment. It is guaranteed that there are no ends of some segments that coincide.

Output

n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.

Examples

input

4
1 8
2 3
4 7
5 6

output

3
0
1
0

input

3
3 4
1 5
2 6

output

0
1
1
离散化+树状数组。
把所有区间按照右端点排序,然后统计左端点和右端点之间的已经包含的左端点个数,用树状数组求区间和会很快
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>

using namespace std;
#define MAX 2*100000
struct Node
{
int l,r;
int pos;
}a[MAX+5];
int n;
int num[2*MAX+5];
int c[2*MAX+5];
int ans[MAX+5];
int s;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int num)
{
while(x<=s)
{
c[x]+=num;
x+=lowbit(x);
}
}
int sum(int x)
{
int _sum=0;
while(x>0)
{
_sum+=c[x];
x-=lowbit(x);
}
return _sum;
}
int cmp(Node a,Node b)
{
return a.r<b.r;
}
int main()
{
scanf("%d",&n);
memset(c,0,sizeof(c));
int cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a[i].pos=i;
num[cnt++]=a[i].l;
num[cnt++]=a[i].r;
}
sort(num,num+cnt);
for(int i=1;i<=n;i++)
{
a[i].l=lower_bound(num,num+cnt,a[i].l)-num+1;
a[i].r=lower_bound(num,num+cnt,a[i].r)-num+1;
}
sort(a+1,a+n+1,cmp);
s=a[n].r;
for(int i=1;i<=n;i++)
{
int num=sum(a[i].r)-sum(a[i].l-1);
ans[a[i].pos]=num;
update(a[i].l,1);
}
for(int i=1;i<=n;i++)
{
printf("%d\n",ans[i]);
}
return 0;
}



标签:cnt,Code,652D,int,MAX,Segments,num,include,sum
From: https://blog.51cto.com/u_15834522/5766273

相关文章

  • LeetCode 88 Merge Sorted Array
    ​​题目​​classSolution{public:voidmerge(vector<int>&nums1,intm,vector<int>&nums2,intn){vector<int>nums3;intk......
  • LeetCode 86. Partition List
    ​​题目​​操作指针的题目/***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode(intx):val(x),next......
  • LeetCode 35 Search Insert Position
    ​​题目​​classSolution{public:intsearchInsert(vector<int>&nums,inttarget){intstart=0;intend=nums.size()-1;......
  • LeetCode 71. Simplify Path
    ​​题目​​字符串问题classSolution{public:stringsimplifyPath(stringpath){stringpaths[10005];intpos=0;paths[0]="/";......
  • LeetCode 34. Find First and Last Position of Element in Sorted Array
    ​​题目​​二分练习classSolution{public:vector<int>searchRange(vector<int>&nums,inttarget){vector<int>ans;if(nums.size()==......
  • LeetCode 76. Minimum Window Substring
    ​​题目​​从第一个字符串中找到最小的子串,让子串中包含第二个字符串中的每一个字符。我的思路来自滑动窗口思想,之前用来做自动摘要的。把第一个字符串中的在第二个字符串......
  • LeetCode 36. Valid Sudoku
    ​​题目​​classSolution{public:inttag[10];boolisValidSudoku(vector<vector<char>>&board){for(inti=0;i<9;i++){......
  • LeetCode 54. Spiral Matrix
    ​​题目​​水题classSolution{public:vector<int>spiralOrder(vector<vector<int>>&matrix){inti=0,j=0;vector<int>ans;int......
  • LeetCode 33 Search in Rotated Sorted Array
    ​​题目​​c++二分classSolution{public:intsearch(vector<int>&nums,inttarget){if(nums.size()==0)return-1;intsta......
  • LeetCode 70. Climbing Stairs
    ​​题目​​classSolution{public:intdp[10005];intclimbStairs(intn){dp[1]=1;dp[2]=2;for(inti=3;i<=n;i++)......