当先锋百科网

首页 1 2 3 4 5 6 7

In this problem at each moment you have a set of intervals. You can move from interval (a, b) from our set to interval (c, d) from our set if and only if c < a < d or c < b < d. Also there is a path from interval I1 from our set to interval I2 from our set if there is a sequence of successive moves starting from I1 so that we can reach I2.

Your program should handle the queries of the following two types:

“1 x y” (x < y) — add the new interval (x, y) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals.
“2 a b” (a ≠ b) — answer the question: is there a path from a-th (one-based) added interval to b-th (one-based) added interval?
Answer all the queries. Note, that initially you have an empty set of intervals.

Input
The first line of the input contains integer n denoting the number of queries, (1 ≤ n ≤ 100). Each of the following lines contains a query as described above. All numbers in the input are integers and don’t exceed 109 by their absolute value.

It’s guaranteed that all queries are correct.

Output
For each query of the second type print “YES” or “NO” on a separate line depending on the answer.

Examples
inputCopy
5
1 1 5
1 5 11
2 1 2
1 2 9
2 1 2
outputCopy
NO
YES

区间的可达及联通问题;
从一个区间到另一个区间,回溯的时候不必将原先标记清零,因为只要有路径可达即可,

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<string>
typedef long long ll;
using namespace std;
#define maxn 1005
#define inf 0x3f3f3f3f
const long long int mod =  + ;

ll read() {
    ll x = , f = ;
    char ch = getchar();
    while (ch < '0' || ch>'9') {
        if (ch == '-') {
            f = -;
        }
        ch = getchar();
    }
    while (ch >= '0'&&ch <= '9') {
        x = x *  + ch - '0';
        ch = getchar();
    }
    return x * f;
}

ll quickpow(ll a, ll b) {
    ll ans = ;
    while (b > ) {
        if (b % )ans = ans * a;
        b = b / ;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b ==  ? a : gcd(b, a%b);
}

int n;
int tot;
struct point {
    int l, r;
}p[maxn];

int num[maxn*maxn];
int  vis[maxn];
int id;
char mp[maxn][maxn];
int ans;

bool check(int a, int b) {
    if (p[a].l > p[b].l&&p[a].l < p[b].r)return true;
    else if (p[a].r > p[b].l&&p[a].r < p[b].r)return true;
    return false;
}

bool dfs(int st, int ed) {
    if (st == ed)return true;
    for (int i = ; i < tot; i++) {
        if (check(st, i) && !vis[i]) {
            vis[i] = true;
            if (dfs(i, ed))return true;
        }
    }
    return false;
}

int main() {
    ios::sync_with_stdio(false);
    //memset(vis, 0, sizeof(vis));
    cin >> n;
    tot = ;

    while (n--) {
        int k;
        cin >> k;
        if (k == ) {
            cin >> p[tot].l >> p[tot].r;
            tot++;
        }
        else {
            int x, y;
            cin >> x >> y;
            for (int i = ; i < tot; i++)vis[i] = false;
            vis[x] = true;
            if (dfs(x, y))cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
}