zoukankan      html  css  js  c++  java
  • query

    Given a permutation pp of length nn, you are asked to answer mm queries, each query can be represented as a pair (l ,r )(l,r), you need to find the number of pair(i ,j)(i,j) such that l le i < j le rli<jr and min(p_i,p_j) = gcd(p_i,p_j )min(pi,pj)=gcd(pi,pj).

    Input

    There is two integers n(1 le n le 10^5)n(1n105), m(1 le m le 10^5)m(1m105) in the first line, denoting the length of pp and the number of queries.

    In the second line, there is a permutation of length nn, denoting the given permutation pp. It is guaranteed that pp is a permutation of length nn.

    For the next mm lines, each line contains two integer l_ili and r_i(1 le l_i le r_i le n)ri(1lirin), denoting each query.

    Output

    For each query, print a single line containing only one integer which denotes the number of pair(i,j)(i,j).

    样例输入

    3 2
    1 2 3
    1 3
    2 3

    样例输出

    2
    0
    #include <bits/stdc++.h>
    
    #define lowbit(x) x&(-x)
    using namespace std;
    typedef long long ll;
    const int maxn = 3e6 + 10;
    const ll mod = 1e9 + 7;
    
    struct BIT {
        int n, c[maxn];
    
        inline void init(int _n_) {
            memset(c, 0, sizeof(c));
            n = _n_ + 10;
        }
    
        inline void update(int pos, int val) {
            while (pos <= n) {
                c[pos] += val;
                pos += lowbit(pos);
            }
        }
    
        inline int query(int pos) {
            int res = 0;
            while (pos) {
                res += c[pos];
                pos -= lowbit(pos);
            }
            return res;
        }
    } bit;
    
    int res[maxn];
    
    struct event {
    
        int op, l, r, id;
    
        bool operator<(const event &cur) const {
            if (l != cur.l)return l < cur.l;
            return op < cur.op;
        }
    
        inline void precess() {
            if (op == 0) {
                bit.update(r, 1);
            }
            else {
                int w = bit.query(r);
                if (op == 1) {
                    res[id] += w;
                }
                else {
                    res[id] -= w;
                }
            }
        }
    };
    
    int n, m;
    int o[maxn], pos[maxn];
    vector<event> e;
    
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("1.txt", "r", stdin);
    #endif
        scanf("%d%d", &n, &m);
        for (register int i = 1; i <= n; ++i) {
            scanf("%d", &o[i]);
            pos[o[i]] = i;
        }
        bit.init(n);
        for (register int i = 1; i <= n; ++i) {
            for (register int j = i + i; j <= n; j += i) {
                e.emplace_back(event{0, pos[i], pos[j], 0});
            }
        }
        for (register int i = 1, l, r; i <= m; ++i) {
            scanf("%d%d", &l, &r);
            --l;
            e.emplace_back(event{1, l, l, i});
            e.emplace_back(event{1, r, r, i});
            e.emplace_back(event{2, l, r, i});
            e.emplace_back(event{2, r, l, i});
        }
        sort(e.begin(), e.end());
        for (auto &cur:e) {
            cur.precess();
        }
        for (register int i = 1; i <= m; ++i) {
            printf("%d
    ", res[i]);
        }
        return 0;
    }
  • 相关阅读:
    could not load file or assembly "System.Web.Mvc...
    .Net利用cwbx.dll call AS400 program得到数据
    fastadmin 如何构建组合题--Cannot read property '0' of undefined
    fastadmin的前端js文件中api和event的区别,formatter的意思
    fastadmin是如何使用art-template的,以及如何在js模板中,嵌套JS模板
    学习fastadmin的新技巧:去git里面看文件的修改
    thinphp5,模型调用模型,和控制器调用模型的区别
    fa使用技巧+tp5技巧总结
    input autocomplete的作用是什么?
    fastadmin 实现标签的多选研究---基于fa的test案例,已经CMS中的标签写法
  • 原文地址:https://www.cnblogs.com/czy-power/p/11483056.html
Copyright © 2011-2022 走看看