[線段樹練習5] 線段的條數 - 統計點重複標記數


題目描述

X軸上從下向上依次疊放一定長度某種顏色的線段。問在某個單位區間上一共疊放了多少條線段?


輸入格式

第1行:2個整數XC,N。XC表示X軸的顏色,1<=N<=100000,表示線段的條數。
接下來N行,每行3個整數L,R,C,-100000 <=L<=R<= 100000,表示一線段的左、右端點;1<=C<=100000,表示線段的顏色。
最後1行:1個整數P,表示單位區間的起點。-100000 <=P<100000


輸出格式

第1行:1個整數M,表示[P, P+1]區間疊放了多少條線段。


樣例數據

樣例輸入

1 4
2 6 2
1 5 3
3 4 2
7 8 4
3

樣例輸出

3


題目分析

相對於前幾道題,這道題就水了。
區間修改 單點查詢
直接統計就可以了
注意偏移量


源代碼

#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
inline const int Get_Int() {
    int num=0,bj=1;
    char x=getchar();
    while(x<'0'||x>'9') {
        if(x=='-')bj=-1;
        x=getchar();
    }
    while(x>='0'&&x<='9') {
        num=num*10+x-'0';
        x=getchar();
    }
    return num*bj;
}
const int maxn=600000;
struct Tree { //修改區間 查詢點 
    int left,right,sum;
};
struct Segment_Tree { //統計標記次數(顏色有用麼) 
    Tree tree[maxn*4];
    int sum;
    void build(int index,int Left,int Right) {
        tree[index].left=Left;
        tree[index].right=Right;
        tree[index].sum=0;
        if(Left==Right)return;
        int mid=(Left+Right)/2;
        build(2*index,Left,mid);
        build(2*index+1,mid+1,Right);
    }
    void modify(int index,int Left,int Right) {
        if(Right<tree[index].left||Left>tree[index].right)return; //不相交
        if(Left<=tree[index].left&&Right>=tree[index].right) { //完全包含
            tree[index].sum++;
            return;
        }
        modify(index*2,Left,Right);
        modify(index*2+1,Left,Right);
    }
    void init() {
        sum=0;
    }
    void Query(int index,int target) {
        if(target<tree[index].left||target>tree[index].right)return; //不相交
        sum+=tree[index].sum;
        Query(index*2,target);
        Query(index*2+1,target);
    }
};
Segment_Tree st;
int n,Delta=100001,color;
int main() {
    color=Get_Int();
    st.build(1,1,200001);
    n=Get_Int();
    for(int i=1; i<=n; i++) {
        int l=Get_Int()+Delta,r=Get_Int()+Delta-1,color=Get_Int();
        st.modify(1,l,r);
    }
    st.init();
    st.Query(1,Get_Int()+Delta);
    printf("%d\n",st.sum);
    return 0;
}

發佈了169 篇原創文章 · 獲贊 31 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章