JDFZOJ 2175忠誠2 - セグメントツリー

JDFZOJ 2175忠誠2

トピックリンク:JDFZOJ 2175忠誠2

アルゴリズムタグ: 线段树

タイトル

タイトル説明

サーバントはスマートでできる人です。彼は明確に自分のアカウントを作成するために、完全な金持ち、金持ちとして10年間働きました。要件バトラーは毎日思い出す\(K \)スマートで有能な執事、家政婦として回アカウントをので、常に満足豊かにします。しかし、いくつかの挑発に、金持ちや家政婦は疑問を持っていました。彼は家政婦の忠誠を決定するために、特別な方法を使用することを決めたので、彼は数字の各アカウントに応じて1、2、...、その後随時家政婦の問題を聞いて、問題はこれです:中\(\)\(B \)数はどのくらい以上のアカウントの合計ですか?家政婦は、彼は常にfalse問題だっ複数回依頼する時間がなかったようにするために。
本の内容は、問い合わせ処理の変更が可能です

入力形式

最初の行は、2つの入力数値有する\(M、N \)表現を\(M(M \ le100000) \) Tのアカウントは、\(N \)は、N番目の質問を表す\(N \ le100000 \)
3桁、第一次の各行\(P \)は、2番目の数字は数1又は数2である\(X \) 第3の数である\(Y \)
場合、P = 1つのクエリX、Y間隔
P = 2、xは数yに変更します

出力フォーマット

各質問への回答の出力ファイル。具体的な例を見ます。

サンプル入力と出力

入力#1

10 3
1 2 3 4 5 6 7 8 9 10
1 2 7
2 2 0
1 1 10

出力#1

2 0

ソリューション:

ロイヤリティポータル、参加する上でクエリに基づいて元のタイトルの拡張バージョン変更を事業セグメントツリーの変更に応じて、

void fix(int pos,int l,int r,int x,int y)
{
    int mid=(l+r)>>1;
    if(l==r) {f[pos]=y;return;}
    if(x<=mid) fix(ls,l,mid,x,y);
    else if(x>mid) fix(rs,mid+1,r,x,y);
    f[pos]=min(f[ls],f[rs]);
}

これは、パスパラメータことを、ここで注目すべきである\(X、Y \)は、クエリー間隔と同じではありません、それはと述べた\(X \)の代わりに\(Y- \)

他の忠実なセグメントツリーアプローチ、間隔クエリと同じです。

ACコード

#include <iostream>
#include <cstdio>
#include <algorithm>

#define lson pos << 1
#define rson pos << 1 | 1

using namespace std;
const int maxn = 100010;
int n, m, a[maxn], tree[maxn << 2], ans[maxn], cnt;
void build(int pos, int  l, int  r)
{
    int mid = (l + r) >> 1;
    if (l == r)
    {
        tree[pos] = a[l];
        return ;
    }
    build(lson, l, mid);
    build(rson, mid + 1, r);
    tree[pos] = min(tree[lson], tree[rson]);
}
void fix(int pos, int l, int r, int x, int y)
{
    int mid = (l + r) >> 1;
    if (l == r)
    {
        tree[pos] = y;
        return ;
    }
    if (x <= mid) fix(lson, l, mid, x, y);
    else if (x > mid) fix(rson, mid + 1, r, x, y);
    tree[pos] = min(tree[lson], tree[rson]);
}
int query(int pos, int l, int r, int x, int y)
{
    int res=1 << 30;
    int mid = (l + r) >> 1;
    if(x <= l && r <= y) return tree[pos];
    if(y <= mid)
        res = min(res, query(lson, l, mid, x, y));
    else if(x > mid)
        res = min(res, query(rson, mid + 1, r, x, y));
    else
    {
        int rea = query(lson, l, mid, x, y);
        int reb = query(rson, mid + 1, r, x, y);
        res = min(res, min(rea, reb));
    }
    return res;
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i ++ )
        cin >> a[i] ;
    build(1, 1, n);
    for (int i = 1; i <= m; i ++ )
    {
        int a, b, c;
        cin >> a >> b >> c;
        if (a == 1)
            ans[++cnt] = query(1, 1, n, b, c);
        else 
            fix(1, 1, n, b, c);
    }
    if (cnt >= 1) cout << ans[1];
    for (int i = 2; i <= cnt; i ++ )
    {
        cout << ' ' << ans[i];
    }
    return 0;
}

おすすめ

転載: www.cnblogs.com/littleseven777/p/11845648.html