zoukankan      html  css  js  c++  java
  • uva 12003 分块

     大白上的原题,我就练练手。。。
    
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 3e5 + 10;
    const int SIZE = 4096;
    ll block[N / SIZE + 1][SIZE + 1];
    ll A[N];
    
    int query(int L, int R, int v)
    {
        int k = 0;
        int lb = L / SIZE, rb = R / SIZE;
        if(lb == rb) { for(int i = L; i <= R; ++i) if(A[i] < v) k++; }
        else {
                for(int i = L; i < (lb + 1) * SIZE; ++i) if(A[i] < v) k++;
                for(int i = rb * SIZE; i <= R; ++i) if(A[i] < v) k++;
                for(int i = lb + 1; i < rb; ++i) {
                    k += lower_bound(block[i], block[i] + SIZE, v) - block[i];
                }
        }
        return k;
    }
    void change(int k, int u, int p, int L, int R)
    {
        ll x = (ll)u * k / (R - L + 1);
        if(A[p] == x) return;
    
        int la = p / SIZE;
        ll* B = &block[la][0];
        int pos = 0;
        ll old = A[p];
        while(B[pos] < old) pos++;
        A[p] = x; B[pos] = x;
        if(x > old) {
            while(pos < SIZE - 1 && B[pos] > B[pos + 1]) { swap(B[pos + 1], B[pos]); pos++; }
        }
        else {
            while(pos > 0 && B[pos] < B[pos - 1]) { swap(B[pos - 1], B[pos]); pos--; }
        }
    }
    int main()
    {
        int n, m, u;
        while(~scanf("%d%d%d", &n, &m, &u))
        {
            int j = 0, k = 0;
            for(int i = 0; i < n; ++i)
            {
                scanf("%lld", &A[i]);
                block[k][j++] = A[i];
                if(j == SIZE) { k++; j = 0; }
            }
            for(int i = 0; i < k; ++i) sort(block[i], block[i] + SIZE);
            if(j) sort(block[k], block[k] + j);
            int L, R, v, p;
            while(m --)
            {
                int ans = 0;
                scanf("%d%d%d%d", &L, &R, &v, &p);
                L--; R--; p--;
                ans = query(L, R, v);
                change(ans, u, p, L, R);
            }
            for(int i = 0; i < n; ++i) printf("%lld
    ", A[i]);
        }
        return 0;
    }
      
  • 相关阅读:
    [CEOI2008] order
    Atcoder 2373 Cookie Exchanges
    [POI2011]ROT-Tree Rotations
    bzoj 5216: [Lydsy2017省队十连测]公路建设
    bzoj 5215: [Lydsy2017省队十连测]商店购物
    Codeforces 961 E Tufurama
    [九省联考2018] 秘密袭击coat
    Codeforces 961 D Pair Of Lines
    [八省联考2018] 劈配
    [九省联考2018]一双木棋chess
  • 原文地址:https://www.cnblogs.com/orchidzjl/p/4905541.html
Copyright © 2011-2022 走看看