zoukankan      html  css  js  c++  java
  • UVa 12003 Array Transformer (分块)

    题意:给定一个序列,然后有 m 个修改,问你最后的序列是什么,修改是这样的 l r v p 先算出从 l 到 r 这个区间内的 小于 v 的个数k,然后把第 p 个的值改成 k * u / (r - l + 1)。

    析:分块,每块长度是sz,把每一块都排序。然后在每次修改的时候,只要计算出 l 和 r 所在块,中间的用二分可以算出来。注意同时要把分块中的数也改掉。

    代码如下:

    #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 <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #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 fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    //#define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define all 1,n,1
    #define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
    #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 double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 3e5 + 10;
    const int maxm = 1e5 + 10;
    const int mod = 50007;
    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;
    }
    
    int a[maxn];
    int b[560][560];
    const int sz = 548;
    
    int main(){
      int u;
      while(scanf("%d %d %d", &n, &m, &u) == 3){
        int idx = 0, cnt = 0;
        for(int i = 0; i < n; ++i){
          scanf("%d", a + i);
          b[idx][cnt] = a[i];
          if(++cnt == sz){ ++idx; cnt = 0; }
        }
        for(int i = 0; i < idx; ++i)  sort(b[i], b[i] + sz);
        if(cnt)  sort(b[idx], b[idx] + cnt);
        while(m--){
          int l, r, v, p;
          scanf("%d %d %d %d", &l, &r, &v, &p);
          --l, --r, --p;
          int lx = l / sz, rx = r / sz;
          int k = 0;
          if(lx == rx){
            for(int i = l; i <= r; ++i)  if(v > a[i])  ++k;
          }
          else {
            for(int i = l; i < (lx+1)*sz; ++i)  if(v > a[i])  ++k;
            for(int i = rx*sz; i <= r; ++i)  if(v > a[i])  ++k;
            for(int i = lx+1; i < rx; ++i)
              k += lower_bound(b[i], b[i] + sz, v) - b[i];
          }
          int c = (LL)u * k / (r - l + 1);
          int old = a[p];
          a[p] = c;
          int mx = p / sz;
          p = 0;
          while(b[mx][p] < old)  ++p;
          b[mx][p] = c;
          while(p - 1 >= 0 && b[mx][p] < b[mx][p-1])  swap(b[mx][p], b[mx][p-1]), --p;
          while(p + 1 < sz && b[mx][p] > b[mx][p+1])  swap(b[mx][p], b[mx][p+1]), ++p;
        }
        for(int i = 0; i < n; ++i)  printf("%d
    ", a[i]);
      }
      return 0;
    }
    

      

  • 相关阅读:
    LeetCode153 Find Minimum in Rotated Sorted Array. LeetCode162 Find Peak Element
    LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word
    LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number
    LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four
    LeetCode225 Implement Stack using Queues
    LeetCode150 Evaluate Reverse Polish Notation
    LeetCode125 Valid Palindrome
    LeetCode128 Longest Consecutive Sequence
    LeetCode124 Binary Tree Maximum Path Sum
    LeetCode123 Best Time to Buy and Sell Stock III
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7652819.html
Copyright © 2011-2022 走看看