Problem Description

#include <iostream>

#include <algorithm>

using namespace std;



int n,a[110000],b[110000],c[110000],d[110000];

int main()

{

      while(cin>>n)

      {

            for(int i=0;i<n;++i)  cin>>a[i];

            for(int i=0;i<n;++i)  cin>>b[i];

            for(int i=0;i<n;++i)  cin>>c[i];

            for(int i=0;i<n;++i)  cin>>d[i];



            for(int i=0;i<n;++i)

                  if(a[i]>b[i])  swap(a[i],b[i]);

            for(int i=0;i<n;++i)

                  if(c[i]>d[i])  swap(c[i],d[i]);



            for(int i=0;i<n;++i)

            {

                  int ans=0;

                  for(int j=0;j<n;++j)

                        if(a[i]<=c[j]&&d[j]<=b[i])  ans++;

                  cout<<ans<<endl;

            }

      }

      return 0;

}





Even you are brave enough still, I really not recommend you to copy the code and just submit it.

Input

Input contains several cases.

Each case begins with an integer n (0<n<=100000).

Then follow with 4 lines representing a[ ],b[ ],c[ ] and d[ ], all values are located in [1,100000].

Output

For each case, just write out the answer as what the code do.

Sample Input

4
1 1 1 2
1 1 1 2
1 1 1 1
1 1 1 1

Sample Output

4
4
4
0

题目大意:

给你二组线段A,B 询问A组中每一个线段包含了多少B组线段

将A,B按左端点排序;

那么当B前面线段若不满足当前A了 ,就可以废弃不用了,即B.x<A[now].x。

所以未被删除的B线段都是满足了左端点,现在只需要求是否满足右端点即可。

用线段树来维护B的右端点。若B一条废弃不用,更新线段树即可。每次查询【0-A[now].y】有多少元素即可

nlogn的复杂度。

最后还要按序号排序回来,因为要按输入顺序输出,代码如下:

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn=100000+5;
struct node
{
int x,y,num,ans;
}A[maxn],B[maxn];
int n;
int tree[maxn<<2];
bool cmp(node a,node b)
{
return a.x<b.x;
}
bool cmp1(node a,node b)
{
return a.num<b.num;
}
void pushup(int rt)
{
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
int build(int l,int r,int rt)
{
if(l==r) {tree[rt]=0;return 0;}
int m=(l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
int updata(int p,int k,int l,int r,int rt)//单点更新
{
int m;
if(l==r) {tree[rt]+=k;return 0;}
m=(l+r)>>1;
if(p<=m) updata(p,k,lson);
else updata(p,k,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
int temp=0,m;
if(L<=l&&r<=R) return tree[rt];
m=(l+r)>>1;
if(L<=m) temp=temp+query(L,R,lson);
if(R>m) temp=temp+query(L,R,rson);
return temp;
}
void input()
{
for(int i=0;i<n;++i) {scanf("%d",&A[i].x);A[i].num=i;}
for(int i=0;i<n;++i) scanf("%d",&A[i].y);
for(int i=0;i<n;++i) scanf("%d",&B[i].x);
for(int i=0;i<n;++i) scanf("%d",&B[i].y);
for(int i=0;i<n;++i)
if(A[i].x>A[i].y) swap(A[i].x,A[i].y);
for(int i=0;i<n;++i)
if(B[i].x>B[i].y) swap(B[i].x,B[i].y);
sort(B,B+n,cmp);
sort(A,A+n,cmp);
}
void solve()
{
int tot=0;
for(int i=0;i<n;i++)
{
updata(B[i].y,1,1,maxn-1,1);
}
for(int i=0;i<n;i++)
{
int ans=0;
while(B[tot].x<A[i].x&&tot<n) {
updata(B[tot].y,-1,1,maxn-1,1);
tot++;
}
ans=query(1,A[i].y,1,maxn-1,1);
A[i].ans=ans;
}
sort(A,A+n,cmp1);
for(int i=0;i<n;i++) printf("%d\n",A[i].ans);
}
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
int main()
{
// init();
while(scanf("%d",&n)!=EOF)
{
input();
build(1,maxn-1,1);
solve();
}
}

【线段树】【4-6组队赛】Problem H的更多相关文章

  1. 线段树:CDOJ1591-An easy problem A (RMQ算法和最简单的线段树模板)

    An easy problem A Time Limit: 1000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

  2. HDU 4417.Super Mario-可持久化线段树(无修改区间小于等于H的数的个数)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. 线段树:CDOJ1592-An easy problem B (线段树的区间合并)

    An easy problem B Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

  4. 线段树:CDOJ1597-An easy problem C(区间更新的线段树)

    An easy problem C Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

  5. 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)

    A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...

  6. 【POJ3468】【zkw线段树】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  7. 计蒜客 31460 - Ryuji doesn't want to study - [线段树][2018ICPC徐州网络预赛H题]

    题目链接:https://nanti.jisuanke.com/t/31460 Ryuji is not a good student, and he doesn't want to study. B ...

  8. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  9. 【算法系列学习】线段树 区间修改,区间求和 [kuangbin带你飞]专题七 线段树 C - A Simple Problem with Integers

    https://vjudge.net/contest/66989#problem/C #include<iostream> #include<cstdio> #include& ...

  10. (线段树 区间查询)The Water Problem -- hdu -- 5443 (2015 ACM/ICPC Asia Regional Changchun Online)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java/ ...

随机推荐

  1. Ext 怎么发ajax请求

    Ext3.3完整包 Ext3.3中文文档 数据表的结构是:数据表table  > 记录record > 字段 store的结构是:  Ext.data.Store > Ext.dat ...

  2. 创建渐进式jpeg图片

    <?php // Create an image instance $im = imagecreatefromjpeg('test.jpg');   // Enable interlancing ...

  3. spring 3配置文件中如何注入map list set等类型

    首先写个 javabean类吧,如下 package com.bean; import java.util.List; import java.util.Map; import java.util.P ...

  4. paip.oracle query export to insert sql

    paip.oracle query export to insert sql 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http:/ ...

  5. iOS 三维变换

    1:平移 一个4*4的单位矩阵乘以一个P(x,y,z,1)的行向量,则表示此矩阵向x轴移动了x的单位,向Y轴移动了y个单位,向Z轴移动了z个单位,最后获得移动后的目标矩阵是 [ 1, 0, 0, 0  ...

  6. Activity(二)

    多个Activity之间的调用 建立一个Activity 配置layout文件夹下fragment_main.xml文件 在layout下新建other.xml文件 xml文件创建的id需要编译才能生 ...

  7. OS Kernel Parameter.semopm

    安装Oracle11g内核参数semopm未校验通过,点击Fix&Check Again后,会提示执行修改脚本,在/tmp/CVU_11.2.0.1.0_oracle下,找到并执行该脚本run ...

  8. LeetCode Day5——House Robber

    问题描述: 意思就是说:给定一个数组,寻找一种选取方法,使得在保证任何两个相邻元素不同时被选的条件下得到的数值总和最大. 1. 递归 设nums为数组首地址,numsSize为数组的大小,max[i] ...

  9. session劫持以及预防

    session劫持是一种广泛存在的比较严重的安全威胁,在session技术中,客户端和服务端通过session的标识符来维护会话, 但这个标识符很容易就能被嗅探到,从而被其他人利用.它是中间人攻击的一 ...

  10. 字符串聚合技术(String Aggregation Techniques)

    from: http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php String Aggregation ...