zoukankan      html  css  js  c++  java
  • [BZOJ2120] 数颜色 && [bzoj2453] 维护队列(莫队 || 分块)

    传送门

    只有第一个,第二个权限题。

    分块,然而wa,没看出来错在哪里,有时间再看。

      1 #include <cmath>
      2 #include <cstdio>
      3 #include <iostream>
      4 #include <algorithm>
      5 
      6 const int N = 10001, M = 1000001;
      7 
      8 int n, m, S, C;
      9 int a[N], st[N], ed[N], belong[N], pre[N], last[M], c[N];
     10 
     11 inline int read()
     12 {
     13     int x = 0, f = 1;
     14     char ch = getchar();
     15     for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
     16     for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
     17     return x * f;
     18 }
     19 
     20 inline int min(int x, int y)
     21 {
     22     return x < y ? x : y;
     23 }
     24 
     25 inline int find(int x, int y, int z)
     26 {
     27     int mid, l = x;
     28     while(x < y)
     29     {
     30         mid = (x + y) >> 1; 
     31         if(c[mid] >= z) y = mid;
     32         else x = mid + 1;
     33     }
     34     return x - l;
     35 }
     36 
     37 inline void reset(int x)
     38 {
     39     int i;
     40     for(i = st[x]; i <= ed[x]; i++) c[i] = pre[i];
     41     std::sort(c + st[x], c + ed[x] + 1);
     42 }
     43 
     44 inline void init()
     45 {
     46     int i, j;
     47     n = read();
     48     m = read();
     49     S = sqrt(n);
     50     for(i = 1; i <= n; i++)
     51     {
     52         a[i] = read();
     53         pre[i] = last[a[i]];
     54         last[a[i]] = i;
     55     }
     56     for(i = 1; i <= n; i += S)
     57         st[++C] = i, ed[C] = min(i + S - 1, n);
     58     for(i = 1; i <= C; i++)
     59     {
     60         reset(i);
     61         for(j = st[i]; j <= ed[i]; j++) belong[j] = i;
     62     }
     63 }
     64 
     65 inline int query(int x, int y)
     66 {
     67     int i, l = belong[x], r = belong[y], ans = 0;
     68     if(l == r)
     69     {
     70         for(i = x; i <= y; i++) if(c[i] < x) ans++;
     71         return ans;
     72         //return std::lower_bound(c + x, c + y + 1, x) - c - x;
     73     }
     74     //ans += std::lower_bound(c + x, c + ed[l] + 1, x) - c - x;
     75     for(i = x; i <= ed[l]; i++) if(c[i] < x) ans++;
     76     for(i = l + 1; i <= r - 1; i++) ans += find(st[i], ed[i] + 1, x);
     77     //ans += std::lower_bound(c + st[r], c + y + 1, x) - c - st[r];
     78     for(i = st[r]; i <= y; i++) if(c[i] < x) ans++;
     79     return ans;
     80 }
     81 
     82 inline void update(int x, int d)
     83 {
     84     int i, t;    
     85     for(i = 1; i <= n; i++) last[a[i]] = 0;
     86     a[x] = d;
     87     for(i = 1; i <= n; i++)
     88     {
     89         t = pre[i];
     90         pre[i] = last[a[i]];
     91         if(t ^ pre[i]) reset(belong[i]);
     92         last[a[i]] = i;
     93     }
     94 }
     95 
     96 inline void work()
     97 {
     98     int i, x, y;
     99     char ch[5];
    100     for(i = 1; i <= m; i++)
    101     {
    102         scanf("%s", ch);
    103         x = read();
    104         y = read();
    105         if(ch[0] == 'Q') printf("%d
    ", query(x, y));
    106         else update(x, y);
    107     }
    108 }
    109 
    110 int main()
    111 {
    112     init();
    113     work();
    114     return 0;
    115 }
    View Code

    莫队

    比分块不知道高到哪里去了。

    带修改之后真是。。。。

     1 #include <cmath>
     2 #include <cstdio>
     3 #include <iostream>
     4 #include <algorithm>
     5 
     6 const int N = 10001, M = 1e6 + 5;
     7 int n, Q, S, cnt1, cnt2, cur, l = 1, r, now;
     8 int a[N], t[N], belong[N], ans[N], c[M];
     9 
    10 struct ovo
    11 {
    12     int l, r, tim, id;
    13     ovo(int l = 0, int r = 0, int tim = 0, int id = 0) : l(l), r(r), tim(tim), id(id) {}
    14 }q[N];
    15 struct qwq
    16 {
    17     int p, v, last;
    18     qwq(int p = 0, int v = 0, int last = 0) : p(p), v(v), last(last) {}
    19 }cq[N];
    20 
    21 inline int read()
    22 {
    23     int x = 0, f = 1;
    24     char ch = getchar();
    25     for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
    26     for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
    27     return x * f;
    28 }
    29 
    30 inline bool cmp(ovo x, ovo y)
    31 {
    32     return belong[x.l] == belong[y.l] ? (belong[x.r] == belong[y.r] ? x.tim < y.tim : belong[x.r] < belong[y.r]) : belong[x.l] < belong[y.l];
    33 }
    34 
    35 inline void add(int x)
    36 {
    37     now += (++c[x]) == 1;
    38 }
    39 
    40 inline void del(int x)
    41 {
    42     now -= (--c[x]) == 0;
    43 }
    44 
    45 inline void update(int x, int d)
    46 {
    47     if(l <= x && x <= r) add(d), del(a[x]);
    48     a[x] = d;
    49 }
    50 
    51 int main()
    52 {
    53     int i, x, y;
    54     char ch[3];
    55     n = read();
    56     Q = read();
    57     S = sqrt(n);
    58     for(i = 1; i <= n; i++) a[i] = t[i] = read(), belong[i] = i / S + 1;
    59     for(i = 1; i <= Q; i++)
    60     {
    61         scanf("%s", ch);
    62         x = read();
    63         y = read();
    64         if(ch[0] == 'Q') q[++cnt1] = ovo(x, y, cnt2, cnt1);
    65         else cq[++cnt2] = qwq(x, y, t[x]), t[x] = y;
    66     }
    67     std::sort(q + 1, q + cnt1 + 1, cmp);
    68     for(i = 1; i <= cnt1; i++)
    69     {
    70         while(cur < q[i].tim) cur++, update(cq[cur].p, cq[cur].v);
    71         while(cur > q[i].tim) update(cq[cur].p, cq[cur].last), cur--;
    72         while(l < q[i].l) del(a[l]), l++;
    73         while(l > q[i].l) l--, add(a[l]);
    74         while(r < q[i].r) r++, add(a[r]);
    75         while(r > q[i].r) del(a[r]), r--;
    76         ans[q[i].id] = now;
    77     }
    78     for(i = 1; i <= cnt1; i++) printf("%d
    ", ans[i]);
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    [转]群控电梯调度算法
    [转] 电梯调度算法总结
    [转]grub2.0和之前版本修复解决方案
    [转]Ubuntu 10.04 编译安装最新版本Linux2.6.34内核
    [转]PS2 键盘工作方式
    [转]个人管理 - 目标管理之前,你会时间管理吗
    [转]ubuntu 下编译内核简单步骤
    [转]关闭Google安全搜索,实现无限制搜索
    [转]Vim 复制粘贴探秘
    [转]Linux文件搜索
  • 原文地址:https://www.cnblogs.com/zhenghaotian/p/6904799.html
Copyright © 2011-2022 走看看