• 【CF】220B Little Elephant and Array


    区间动态统计的好题。

      1 /*  */
      2 #include <iostream>
      3 #include <string>
      4 #include <map>
      5 #include <queue>
      6 #include <set>
      7 #include <stack>
      8 #include <vector>
      9 #include <deque>
     10 #include <algorithm>
     11 #include <cstdio>
     12 #include <cmath>
     13 #include <ctime>
     14 #include <cstring>
     15 #include <climits>
     16 #include <cctype>
     17 #include <cassert>
     18 #include <functional>
     19 using namespace std;
     20 //#pragma comment(linker,"/STACK:102400000,1024000")
     21 
     22 #define rep(i, a, n)     for (int i=a;i<n;++i)
     23 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
     24 #define pb                 push_back
     25 #define mp                 make_pair
     26 #define all(x)             (x).begin(),(x).end()
     27 #define SZ(x)             ((int)(x).size())
     28 #define lson            l, mid, rt<<1
     29 #define rson            mid+1, r, rt<<1|1
     30 
     31 typedef struct inter_t {
     32     int l, r, id, ans;
     33     inter_t() {}
     34     inter_t(int _l, int _r, int _id, int _ans=0):
     35         l(_l), r(_r), id(_id), ans(_ans) {}
     36     friend bool operator< (const inter_t& a, const inter_t& b) {
     37         if (a.r == b.r)
     38             return a.l > b.l;
     39         else
     40             return a.r < b.r;
     41     }
     42 } inter_t;
     43 
     44 const int maxn = 1e5+5;
     45 vector<int> vc[maxn];
     46 int sz[maxn];
     47 vector<inter_t> Q;
     48 map<int,int> tb;
     49 int d[maxn];
     50 int a[maxn], l = 1;
     51 int v[maxn];
     52 
     53 bool comp(const inter_t& a, const inter_t& b) {
     54     return a.id < b.id;
     55 }
     56 
     57 int main() {
     58     int i, j, k;
     59     int n, m;
     60 
     61     #ifndef ONLINE_JUDGE
     62         freopen("data.in", "r", stdin);
     63         freopen("data.out", "w", stdout);
     64     #endif
     65 
     66     scanf("%d %d", &n, &m);
     67     for (i=1; i<=n; ++i)
     68         scanf("%d", &a[i]);
     69 
     70     int li, ri;
     71     for (i=0; i<m; ++i) {
     72         scanf("%d %d", &li, &ri);
     73         Q.push_back(inter_t(li, ri, i));
     74     }
     75     sort(Q.begin(), Q.end());
     76     Q.push_back(inter_t(0,n+1,m));
     77     memset(d, 0, sizeof(d));
     78     memset(sz, 0, sizeof(sz));
     79 
     80     int r = 1, mr, x, id, p, cnt;
     81     i = 0;
     82     while (i < m) {
     83         mr = Q[i].r;
     84         while (r <= mr) {
     85             x = a[r];
     86             id = tb[x];
     87             if (id == 0) {
     88                 tb[x] = id = l;
     89                 l++;
     90                 v[l] = x;
     91             }
     92             vc[id].push_back(r);
     93             ++sz[id];
     94             if (sz[id] >= x) {
     95                 p = vc[id][sz[id]-x];
     96                 ++d[p];
     97             }
     98             if (sz[id] >= x+1) {
     99                 p = vc[id][sz[id]-x-1];
    100                 d[p] -= 2;
    101             }
    102             if (sz[id] > x+1) {
    103                 p = vc[id][sz[id]-x-2];
    104                 ++d[p];
    105             }
    106             ++r;
    107         }
    108         cnt = 0;
    109         k = mr+1;
    110         while (Q[i].r == mr) {
    111             for (j=Q[i].l; j<k; ++j)
    112                 cnt += d[j];
    113             Q[i].ans = cnt;
    114             k = Q[i].l;
    115             ++i;
    116         }
    117     }
    118 
    119     sort(Q.begin(), Q.end(), comp);
    120     for (i=0; i<m; ++i)
    121         printf("%d
    ", Q[i].ans);
    122 
    123     #ifndef ONLINE_JUDGE
    124         printf("time = %d.
    ", (int)clock());
    125     #endif
    126 
    127     return 0;
    128 }
  • 相关阅读:
    Uva 10719 Quotient Polynomial
    UVa 11044 Searching for Nessy
    Uva 10790 How Many Points of Intersection?
    Uva 550 Multiplying by Rotation
    Uva 10916 Factstone Benchmark
    Uva 10177 (2/3/4)D Sqr/Rects/Cubes/Boxes?
    Uva 591 Box of Bricks
    Uva 621 Secret Research
    Uva 10499 The Land of Justice
    Uva 10014 Simple calculations
  • 原文地址:https://www.cnblogs.com/bombe1013/p/4537388.html
Copyright © 2020-2023  润新知