zoukankan      html  css  js  c++  java
  • codeforce ABBYY Cup 3.0

    B2. Shave Beaver!
     

    The Smart Beaver has recently designed and built an innovative nanotechnologic all-purpose beaver mass shaving machine, "Beavershave 5000". Beavershave 5000 can shave beavers by families! How does it work? Very easily!

    There are n beavers, each of them has a unique id from 1 to n. Consider a permutation a1, a2, ..., an of n these beavers. Beavershave 5000 needs one session to shave beavers with ids from x to y (inclusive) if and only if there are such indices i1 < i2 < ... < ik, thatai1 = xai2 = x + 1, ..., aik - 1 = y - 1, aik = y. And that is really convenient. For example, it needs one session to shave a permutation of beavers 1, 2, 3, ..., n.

    If we can't shave beavers from x to y in one session, then we can split these beavers into groups [x, p1], [p1 + 1, p2], ..., [pm + 1, y](x ≤ p1 < p2 < ... < pm < y), in such a way that the machine can shave beavers in each group in one session. But then Beavershave 5000 needs m + 1 working sessions to shave beavers from x to y.

    All beavers are restless and they keep trying to swap. So if we consider the problem more formally, we can consider queries of two types:

    • what is the minimum number of sessions that Beavershave 5000 needs to shave beavers with ids from x to y, inclusive?
    • two beavers on positions x and y (the beavers ax and ay) swapped.

    You can assume that any beaver can be shaved any number of times.

    Input

    The first line contains integer n — the total number of beavers, 2 ≤ n. The second line contains n space-separated integers — the initial beaver permutation.

    The third line contains integer q — the number of queries, 1 ≤ q ≤ 105. The next q lines contain the queries. Each query i looks as pi xiyi, where pi is the query type (1 is to shave beavers from xi to yi, inclusive, 2 is to swap beavers on positions xi and yi). All queries meet the condition: 1 ≤ xi < yi ≤ n.

    • to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem B1);
    • to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems B1+B2).

    Note that the number of queries q is limited 1 ≤ q ≤ 105 in both subproblem B1 and subproblem B2.

    Output

    For each query with pi = 1, print the minimum number of Beavershave 5000 sessions.

    Examples
    input
    5
    1 3 4 2 5
    6
    1 1 5
    1 3 4
    2 2 3
    1 1 5
    2 1 5
    1 1 5
    output
    2
    1
    3
    5

     题意:

      给你长度n的序列,m次询问

      1:x -> y 的花费  满足 每次 选择 以一个a值  能到大其右边任意位置 (即最长连续上升子序列)算一次路径,问从x值到达y值,需要几次

      2:x,y  交换a[x],a[y];

    题解:

      假设x+1在 x的右边 那么此x的位置值为 1,即任意的区间求和

      有交换操作,线段树维护a[x],a[y]对序列的影响即可

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define ls i<<1
    #define rs ls | 1
    #define mid ((ll+rr)>>1)
    #define pii pair<int,int>
    #define MP make_pair
    typedef long long LL;
    const long long INF = 1e18;
    const double Pi = acos(-1.0);
    const int N = 3e5+10, M = 2e5+35000+11, mod = 1e9+7, inf = 0x3fffffff;
    
    int id[N],a[N],n,m,v[N*20];
    void update(int i,int ll,int rr,int x,int c) {
            if(ll == rr) {
                v[i] = c;
                return ;
            }
            if(x <= mid) update(ls,ll,mid,x,c);
            else update(rs,mid+1,rr,x,c);
            v[i] = v[ls] + v[rs];
    }
    int ask(int i,int ll,int rr,int x,int y) {
            if(ll == x && y == rr) {
                return v[i];
            }
            if(y <= mid) return ask(ls,ll,mid,x,y);
            else if(x > mid) return ask(rs,mid+1,rr,x,y);
            else return ask(ls,ll,mid,x,mid) + ask(rs,mid+1,rr,mid+1,y);
    }
    int main() {
            scanf("%d",&n);
            for(int i = 1; i <= n; ++i) scanf("%d",&a[i]),id[a[i]] = i;
            for(int i = 1; i < n; ++i) {
                if(id[i] > id[i+1]) update(1,1,n,i,1);
            }
            scanf("%d",&m);
            for(int i = 1; i <= m; ++i) {
                int op,x,y;
                scanf("%d%d%d",&op,&x,&y);
                if(op == 1) {
                    printf("%d
    ",ask(1,1,n,x,y-1) + 1);
                } else {
                    int tmp1 = a[x];
                    int tmp2 = a[y];
                    int tt = id[a[x]];
                    id[a[x]] = id[a[y]];
                    id[a[y]] = tt;
                    swap(a[x],a[y]);
                    if(tmp1+1 <= n && id[tmp1] > id[tmp1+1]) update(1,1,n,tmp1,1);
                    if(tmp1-1 >= 1 && id[tmp1-1] < id[tmp1]) update(1,1,n,tmp1-1,0);
    
                    if(tmp2+1 <= n && id[tmp2] < id[tmp2+1]) update(1,1,n,tmp2,0);
                    if(tmp2-1 >= 1 && id[tmp2-1] > id[tmp2]) update(1,1,n,tmp2-1,1);
                }
            }
            return 0;
    }

      

  • 相关阅读:
    Java实现Labeling Balls(拓扑排序的应用)
    Java实现Labeling Balls(拓扑排序的应用)
    Java实现Labeling Balls(拓扑排序的应用)
    Java实现Labeling Balls(拓扑排序的应用)
    string与QString转换(string既可以是utf8,也可以是gbk)
    Qt4.8.6详细安装步骤(使用了i686-4.8.2-release-posix-dwarf-rt_v3-rev3,手动设置gcc和gdb)非常清楚 good
    qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method
    小试X64 inline HOOK,hook explorer.exe--->CreateProcessInternalW监视进程创建
    RtlAdjustPrivilege进程提权,权限ID对照表
    jQuery AJAX 网页无刷新上传示例
  • 原文地址:https://www.cnblogs.com/zxhl/p/5896251.html
Copyright © 2011-2022 走看看