2018年夏のACMの牛オフネットワークマルチ学校の合宿を(第十フィールド)LowbitとAリッカイン(フェンウィック木)

リンク:https://ac.nowcoder.com/acm/contest/148/A
出典:牛オフネットワーク

Lowbitとリッカイン
时间限制:C / C ++ 5秒、其他语言10秒
限制空间:C / C ++ 262144K、其他语言524288K
64ビットIOフォーマット:%LLD
题目描述
今日、リッカインは、いくつかの簡単なを解決するためのBITを使用する方法を学ぶために起こっていますデータ構造の作業。( - x)はBITのテンプレートで勉強している間、彼女は魔法式x&(-x)X&がある見つけました。いくつかの文献を検索した後、リッカインは、関数\テキスト{lowbit(X)} lowbit(X)の実装で実現します。

\テキスト{lowbit}(X)lowbit(x)は、すべての正の整数に定義されています。(X)lowbit(x)は2Kに等しい満足\テキスト{lowbit}のk = 1の値、最小インデックスであり、K、A1は最下位桁であるが、xのバイナリ表記であるA1 ... AMましょう-1。

\テキスト{lowbit}(x)はlowbit(x)は、いくつかの興味深い性質を取得した後、リッカインはあなたのための単純なデータ構造のタスクを設定します。

まず、リッカインそれは非負の整数xを取り、演算子F(X)を定義します。xが0に等しい場合、それはそうでなければ、それは\テキスト{lowbit}(x)はx-lowbit(X)またはX + \テキスト{lowbit}(x)はx + lowbit(x)は、それぞれx軸戻します0が返され\のFRAC {1} {2}の確率で
2
1

次いで、リッカインは、長さnの正の整数配列Aを示しており、彼女はそれにm個の操作を行います。

操作の2種類があります。

  1. 1 LRは、各インデックスiについては、Fに(AI)を愛を変更[L、R]を∈しました。
  2. 2 LR、\のsum_ {I = L} ^ RA_iΣの期待値のクエリ
    I = L
    R A I (あなたがFで使用される確率変数は、他と独立しており、各時間リッカインをfを呼び出すことを仮定してもよい。)输入描述:最初の行は、単一の整数t(1≤T≤3)テストケースの数を含んでいます。






各テストケースの最初の行は二つの整数N、M(1≤nは、105≤M)を含有します。2行目は、N個の整数Al(1≤あい≤108)を含みます。

そしてその後、Mラインが各ラインは三つの整数、T、L、R(Tの∈{1,2}、1≤Lの≤のR≤n)を含む従う。
输出描述:
各クエリについて、wは間隔の期待値であるとします合計、あなたはMOD 998244353(ワット×2 \(ワット\回2 ^ {NM})を出力する必要がある
NM
)mod998244353。

そのWXの2nmのを見つけることは容易である整数でなければならない。
実施例1つ
の入力
コピー
1
。3 6
1 2 3
1 3 3
2 3 1
1 3 3
2 3 1
1 3 1
2 1 3
出力
レプリケーション
1572864
1572864
1572864

質問の意味:アイデア:


アクション1分析は、(モジュロを覚えて)いるので、裸の間隔クエリ(接頭辞として行うことができます)、デジタル期待は変化しないことがわかります。

詳細コードを参照してください。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=100010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/

ll tree[maxn];

ll a[maxn];

int n,m;
int lowbit(int x)
{
    return x&(-x);
}
const ll mod=998244353ll;
void add(int id,ll x)
{
    while(id<=n)
    {
        tree[id]=(tree[id]+x)%mod;
        id+=lowbit(id);
    }
}
ll ask(int id)
{
    ll res=0ll;
    while(id)
    {
        res=(res+tree[id])%mod;
        id-=lowbit(id);
    }
    return res;
}
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    gbtb;
    int t;
    cin>>t;
    while(t--)
    {
        MS0(tree);
        cin>>n>>m;
        repd(i,1,n)
        {
            cin>>a[i];
            add(i,a[i]);
        }
        ll ans;
        int op,l,r;
        repd(i,1,m)
        {
            cin>>op>>l>>r;
            if(op==1)
            {
                continue;
            }
            ans=(ask(r)-ask(l-1)+mod)%mod;
            ans=(ans*powmod(2ll,1ll*n*m,mod))%mod;
            cout<<ans<<endl;
        }
    }
    
    
    
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



おすすめ

転載: www.cnblogs.com/qieqiemin/p/11261667.html