TOJ 4105 Lines Counting(离线树状数组)

4105.    Lines Counting
Time Limit: 2.0 Seconds    Memory Limit: 150000K
Total Runs: 152    Accepted Runs: 47


On the number axis, there are N lines. The two endpoints L and R of each line are integer. Give you M queries, each query contains two intervals: [L1,R1] and [L2,R2], can you count how many lines satisfy this property: L1≤L≤R1 and L2≤R≤R2?

Input

First line will be a positive integer N (1≤N≤100000) indicating the number of lines. Following the coordinates of the N lines' endpoints L and R will be given (1≤L≤R≤100000). Next will be a positive integer M (1≤M≤100000) indicating the number of queries. Following the four numbers L1,R1,L2 and R2 of the M queries will be given (1≤L1≤R1≤L2≤R2≤100000).

Output

For each query output the corresponding answer.

Sample Input

3
1 3
2 4
3 5
2
1 2 3 4
1 4 5 6

Sample Output

2
1

 

 

题目链接:TOJ 4105

题意就是在给你N条在X轴上的线段,求左端点在L1~R1且右端点在L2~R2的线段条数,其实这题跟NBUT上一道题很像,问你在区间L1~R1中,值在L2~R2中有几个数,只是这题在起点计数回退时可能多退几个位子,因为线段的起点和终点坐标可能有重复的,都不能算进去,因此要用while语句来操作。离线树状数组是什么个意思呢?就是要满足区间减法,这样的话就可以这样计数:在遇到询问左端点时时减去$[起点,询问区间左端点-1]$对该询问产生的影响值count1,在遇到询问右端点时加上$[起点,询问区间右端点]$对该询问的影响值count2,这样可以发现count1其实是count2的子集,一加一减一定会把count1抵消掉,只留下刚好符合询问区间的答案了。画了个图助于理解

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
struct Line
{
    int l, r;
    bool operator<(const Line &rhs)const
    {
        if (l != rhs.l)
            return l < rhs.l;
        return r < rhs.r;
    }
};
struct query
{
    int k, r1, r2, flag, id;
    query(int _k = 0, int _r1 = 0, int _r2 = 0, int _flag = 0, int _id = 0): k(_k), r1(_r1), r2(_r2), flag(_flag), id(_id) {}
    bool operator<(const query &rhs)const
    {
        return k < rhs.k;
    }
};
Line line[N];
query Q[N << 1];
int T[N], ans[N];

void init()
{
    CLR(T, 0);
    CLR(ans, 0);
}
void add(int k, int v)
{
    while (k < N)
    {
        T[k] += v;
        k += (k & -k);
    }
}
int getsum(int k)
{
    int ret = 0;
    while (k)
    {
        ret += T[k];
        k -= (k & -k);
    }
    return ret;
}
int main(void)
{
    int n, m, i;
    while (~scanf("%d", &n))
    {
        init();
        for (i = 0; i < n; ++i)
            scanf("%d%d", &line[i].l, &line[i].r);
        sort(line, line + n);
        scanf("%d", &m);
        int qcnt = 0;
        for (i = 0; i < m; ++i)
        {
            int l1, r1, l2, r2;
            scanf("%d%d%d%d", &l1, &r1, &l2, &r2);
            Q[qcnt++] = query(l1, l2, r2, 0, i);
            Q[qcnt++] = query(r1, l2, r2, 1, i);
        }
        sort(Q, Q + qcnt);
        int x = 0;
        for (i = 0; i < qcnt; ++i)
        {
            while (line[x].l <= Q[i].k && x < n)
                add(line[x++].r, 1);
            if (Q[i].flag)
                ans[Q[i].id] += getsum(Q[i].r2) - getsum(Q[i].r1 - 1);
            else
            {
                while (line[x - 1].l >= Q[i].k && x - 1 >= 0)
                {
                    add(line[x - 1].r, -1);
                    --x;
                }
                ans[Q[i].id] -= getsum(Q[i].r2) - getsum(Q[i].r1 - 1);
                while (line[x].l <= Q[i].k && x < n)
                {
                    add(line[x].r, 1);
                    ++x;
                }
            }
        }
        for (i = 0; i < m; ++i)
            printf("%d\n", ans[i]);
    }
    return 0;
}

转载于:https://www.cnblogs.com/Blackops/p/6388717.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图像识别技术在病虫害检测中的应用是一个快速发展的领域,它结合了计算机视觉和机器学习算法来自动识别和分类植物上的病虫害。以下是这一技术的一些关键步骤和组成部分: 1. **数据收集**:首先需要收集大量的植物图像数据,这些数据包括健康植物的图像以及受不同病虫害影响的植物图像。 2. **图像预处理**:对收集到的图像进行处理,以提高后续分析的准确性。这可能包括调整亮度、对比度、去噪、裁剪、缩放等。 3. **特征提取**:从图像中提取有助于识别病虫害的特征。这些特征可能包括颜色、纹理、形状、边缘等。 4. **模型训练**:使用机器学习算法(如支持向量机、随机森林、卷积神经网络等)来训练模型。训练过程中,算法会学习如何根据提取的特征来识别不同的病虫害。 5. **模型验证和测试**:在独立的测试集上验证模型的性能,以确保其准确性和泛化能力。 6. **部署和应用**:将训练好的模型部署到实际的病虫害检测系统中,可以是移动应用、网页服务或集成到智能农业设备中。 7. **实时监测**:在实际应用中,系统可以实时接收植物图像,并快速给出病虫害的检测结果。 8. **持续学习**:随着时间的推移,系统可以不断学习新的病虫害样本,以提高其识别能力。 9. **用户界面**:为了方便用户使用,通常会有一个用户友好的界面,显示检测结果,并提供进一步的指导或建议。 这项技术的优势在于它可以快速、准确地识别出病虫害,甚至在早期阶段就能发现问题,从而及时采取措施。此外,它还可以减少对化学农药的依赖,支持可持续农业发展。随着技术的不断进步,图像识别在病虫害检测中的应用将越来越广泛。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值