CF1045G. AI robots

Problem - G - Codeforces

G. AI robots

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

In the last mission, MDCS has successfully shipped NN AI robots to Mars. Before they start exploring, system initialization is required so they are arranged in a line. Every robot can be described with three numbers: position (xixi), radius of sight (riri) and IQ (qiqi).

Since they are intelligent robots, some of them will talk if they see each other. Radius of sight is inclusive, so robot can see other all robots in range [xi−ri,xi+ri][xi−ri,xi+ri]. But they don't walk to talk with anybody, but only with robots who have similar IQ. By similar IQ we mean that their absolute difference isn't more than KK.

Help us and calculate how many pairs of robots are going to talk with each other, so we can timely update their software and avoid any potential quarrel.

Input

The first line contains two integers, numbers N(1≤N≤105)N(1≤N≤105) and K(0≤K≤20)K(0≤K≤20).

Next NN lines contain three numbers each xi,ri,qi(0≤xi,ri,qi≤109)xi,ri,qi(0≤xi,ri,qi≤109) — position, radius of sight and IQ of every robot respectively.

Output

Output contains only one number — solution to the problem.

Example

input

Copy

3 2
3 6 1
7 3 10
10 5 8

output

Copy

1

Note

The first robot can see the second, but not vice versa. The first robot can't even see the third. The second and the third robot can see each other and their IQs don't differ more than 2 so only one conversation will happen.

思路:因为数据有3维,(类似求3维逆序对),所以可能要用高级数据结构。用CDQ分治可以替换高级数据结构方法,排序处理掉1维,分治处理掉1维,树状数组处理掉1维。注意树状数组需要离散化。

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String args[]) {new Main().run();}

    FastReader in = new FastReader();
    PrintWriter out = new PrintWriter(System.out);
    void run() {
        work();
        out.flush();
    }
    long mod=1000000007;
    long gcd(long a,long b) {
        return a==0?b:gcd(b%a,a);
    }
    long inf=Long.MAX_VALUE/3;
    int[] sum=new int[300002];
    List<Integer> rec;
    int[][] A;
    int[][] B;
    int n,d;
    long ret;
    void work() {
        n=ni();d=ni();
        A=new int[n][];
        B=new int[n][3];
        rec=new ArrayList<>();
        for(int i=0;i<n;i++){
            A[i]=nia(3);
            rec.add(A[i][0]-A[i][1]);
            rec.add(A[i][0]);
            rec.add(A[i][0]+A[i][1]);
        }
        Collections.sort(rec);
        Arrays.sort(A,(a1,a2)->a1[1]-a2[1]);
        merge(0,n-1);
        out.println(ret);
    }

    private void merge(int s, int e) {
        if(s>=e)return;
        int m=(s+e)/2;
        merge(s,m);
        merge(m+1,e);
        for(int i=s,j=m+1,k=m+1;i<=m;i++){
            while(k<=e&&A[i][2]+d>=A[k][2]){
                add(getIdx(A[k][0]),1);
                k++;
            }
            while(j<=e&&A[i][2]-d>A[j][2]){
                add(getIdx(A[j][0]),-1);
                j++;
            }
            ret+=query(getIdx(A[i][0]-A[i][1]),getIdx(A[i][0]+A[i][1]));
            if(i==m){
                //还原树状数组
                for(;j<k;j++){
                    add(getIdx(A[j][0]),-1);
                }
            }
        }
        for(int i=s,j=m+1,idx=s;i<=m||j<=e;){
            if(i>m){
                setVal(B,A,idx++,j++);
            }else if(j>e){
                setVal(B,A,idx++,i++);
            }else if(A[i][2]<A[j][2]){
                setVal(B,A,idx++,i++);
            }else{
                setVal(B,A,idx++,j++);
            }
        }
        for(int i=s;i<=e;i++){
            setVal(A,B,i,i);
        }
    }

    private void setVal(int[][] A1, int[][] A2, int i, int j) {
        A1[i][0]=A2[j][0];
        A1[i][1]=A2[j][1];
        A1[i][2]=A2[j][2];
    }

    private int getIdx(int val) {
        return Collections.binarySearch(rec,val)+1;
    }

    private int query(int a, int b) {
        return query(b)-query(a-1);
    }

    int lowbit(int f){
        return f&-f;
    }

    int query(int idx){
        int ret=0;
        for(;idx>0;idx-=lowbit(idx)){
            ret+=sum[idx];
        }
        return ret;
    }

    void add(int idx,int val){
        for(;idx<sum.length;idx+=lowbit(idx)){
            sum[idx]+=val;
        }
    }

    @SuppressWarnings("unused")
    private ArrayList<Integer>[] ng(int n, int m) {
        ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n];
        for(int i=0;i<n;i++) {
            graph[i]=new ArrayList<>();
        }
        for(int i=1;i<=m;i++) {
            int s=in.nextInt()-1,e=in.nextInt()-1;
            graph[s].add(e);
            graph[e].add(s);
        }
        return graph;
    }

    private ArrayList<long[]>[] ngw(int n, int m) {
        ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n];
        for(int i=0;i<n;i++) {
            graph[i]=new ArrayList<>();
        }
        for(int i=1;i<=m;i++) {
            long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong();
            graph[(int)s].add(new long[] {e,w});
            graph[(int)e].add(new long[] {s,w});
        }
        return graph;
    }

    private int ni() {
        return in.nextInt();
    }

    private long nl() {
        return in.nextLong();
    }
    private double nd() {
        return in.nextDouble();
    }
    private String ns() {
        return in.next();
    }

    private long[] na(int n) {
        long[] A=new long[n];
        for(int i=0;i<n;i++) {
            A[i]=in.nextLong();
        }
        return A;
    }

    private int[] nia(int n) {
        int[] A=new int[n];
        for(int i=0;i<n;i++) {
            A[i]=in.nextInt();
        }
        return A;
    }
}

class FastReader
{
    BufferedReader br;
    StringTokenizer st;
    InputStreamReader input;//no buffer
    public FastReader()
    {
        br=new BufferedReader(new InputStreamReader(System.in));
    }

    public FastReader(boolean isBuffer)
    {
        if(!isBuffer){
            input=new InputStreamReader(System.in);
        }else{
            br=new BufferedReader(new InputStreamReader(System.in));
        }
    }

    public boolean hasNext(){
        try{
            String s=br.readLine();
            if(s==null){
                return  false;
            }
            st=new StringTokenizer(s);
        }catch(IOException e){
            e.printStackTrace();
        }
        return true;
    }

    public String next()
    {
        if(input!=null){
            try {
                StringBuilder sb=new StringBuilder();
                int ch=input.read();
                while(ch=='\n'||ch=='\r'||ch==32){
                    ch=input.read();
                }
                while(ch!='\n'&&ch!='\r'&&ch!=32){
                    sb.append((char)ch);
                    ch=input.read();
                }
                return sb.toString();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        while(st==null || !st.hasMoreElements())//回车,空行情况
        {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    public int nextInt()
    {
        return (int)nextLong();
    }

    public long nextLong() {
        try {
            if(input!=null){
                long ret=0;
                int b=input.read();
                while(b<'0'||b>'9'){
                    b=input.read();
                }
                while(b>='0'&&b<='9'){
                    ret=ret*10+(b-'0');
                    b=input.read();
                }
                return ret;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Long.parseLong(next());
    }

    public double nextDouble()
    {
        return Double.parseDouble(next());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值