zoukankan      html  css  js  c++  java
  • BZOJ 2038 小Z的袜子(hose) (莫队算法)

    题意:中文题。

    析:很著名的莫队算法,先把这个求概率的式子表达出来,应该是分子:C(x1, 2) + C(x2, 2) + C(x3, 2) + ... + C(xn, 2)  分母:C(n, 2),然后化成分数的表达形式,[x1(x1-1)+x2(x2-1)+...+xn(xn-1)] / (n*(n-1))  然后再化简得到 (sigma(xi*xi)  - n) / (n*(n-1)) ,然后就是对每个区间进行运算,离线,把所以的序列分成sqrt(n)块,然后用两个指针,进行对数据的计算。

    注意不要用I64d,用的话WA到死。。。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <assert.h>
    #include <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 0xffffffffffLL;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-6;
    const int maxn = 50000 + 10;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    LL ansx[maxn], ansy[maxn];
    int pos[maxn], val[maxn];
    int cnt[maxn];
    
    struct Node{
      int l, r, id;
      bool operator < (const Node &p) const{
        return pos[l] < pos[p.l] || pos[l] == pos[p.l] && r < p.r;
      }
    };
    
    Node a[maxn];
    
    LL x, y;
    
    void update(int l, int ok){
      x -= cnt[val[l]] * (LL)cnt[val[l]];
      cnt[val[l]] += ok;
      x += cnt[val[l]] * (LL)cnt[val[l]] - ok;
      y += ok;
    }
    
    void update(int i){
      ansx[i] = x;
      ansy[i] = y * (y - 1);
    }
    
    int main(){
      scanf("%d %d", &n, &m);
      for(int i = 1; i <= n; ++i) scanf("%d", val+i);
      for(int i = 0; i < m; ++i){
        scanf("%d %d", &a[i].l, &a[i].r);
        a[i].id = i;
      }
      int t = sqrt(n + 0.5);
      for(int i = 1; i <= n; ++i)
        pos[i] = i / t;
      sort(a, a + m);
      for(int l = 1, i = 0 ,r = 0; i < m; ++i){
        int L = a[i].l, R = a[i].r;
        while(l < L)  update(l++, -1);
        while(l > L)  update(--l, 1);
        while(r < R)  update(++r, 1);
        while(r > R)  update(r--, -1);
        update(a[i].id);
      }
      for(int i = 0; i < m; ++i){
        LL g = gcd(ansx[i], ansy[i]);
        printf("%lld/%lld
    ", ansx[i]/g, ansy[i]/g);
      }
      return 0;
    }
    

      

  • 相关阅读:
    经典多线程问题(四)-轮流打印字母和数字
    经典多线程问题 (一)-多线程售票
    买卖股票的最佳时机 II
    最长递增(严格递增)子序列-可以不连续
    环形链表 II
    最小栈
    买卖股票的最佳时机
    二叉树的层序遍历
    字符串相加
    最大子序和
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7368422.html
Copyright © 2011-2022 走看看