# BZOJ 3809: Gty的二逼妹子序列

## 3809: Gty的二逼妹子序列

Time Limit: 80 Sec  Memory Limit: 28 MB
Submit: 1387  Solved: 400
[Submit][Status][Discuss]

## Description

Autumn和Bakser又在研究Gty的妹子序列了！但他们遇到了一个难题。

## Sample Input

10 10
4 4 5 1 4 1 5 1 2 1
5 9 1 2
3 4 7 9
4 4 2 5
2 3 4 7
5 10 4 4
3 9 1 1
1 4 5 9
8 9 3 3
2 2 1 6
8 9 1 4

2
0
0
2
1
1
1
0
1
2

5 9 1 2

3 4 7 9

4 4 2 5

2 3 4 7

## Source

[Submit][Status][Discuss]

  1 #include <bits/stdc++.h>
2
3 template <class T>
4 inline void read(T &num) {
5     register int neg = false;
6     register int bit = getchar();
7
8     while (bit <= '0') {
9         if (bit == '-')
10             neg ^= neg;
11         bit = getchar();
12     }
13
14     num = 0;
15
16     while (bit >= '0') {
17         num = num*10
18         + bit - '0';
19         bit = getchar();
20     }
21
22     if (neg)num = -num;
23 }
24
25 const int N = 1e5 + 5;
26 const int M = 1e6 + 6;
27
28 int n, m, s, num[N], cnt[N], sgl[N], sum[N];
29
30 /*<--- QRY --->*/
31
32 struct query {
33     int l, r, a, b, id, ans;
34 }qry[M];
35
36 inline bool cmp_lr(const query &A, const query &B) {
37     if (A.l / s != B.l / s)
38         return A.l < B.l;
39     else
40         return A.r < B.r;
41 }
42
43 inline bool cmp_id(const query &A, const query &B) {
44     return A.id < B.id;
45 }
46
47 /*<--- MO --->*/
48
49 inline int ask(int a, int b) {
50     if (a / s == b / s) {
51         int ret = 0;
52         for (int i = a; i <= b; ++i)ret += sgl[i];
53         return ret;
54     }
55     else {
56         int ret = 0, lt = a / s + 1, rt = b / s - 1;
57         for (int i = lt; i <= rt; ++i)ret += sum[i];
58         for (int i = a; i / s < lt; ++i)ret += sgl[i];
59         for (int i = b; i / s > rt; --i)ret += sgl[i];
60         return ret;
61     }
62 }
63
64 inline void add(int k, int v) {
65     sgl[k] += v, sum[k/s] += v;
66 }
67
68 inline void insert(int k) {
69     if (++cnt[k] == 1)add(k, 1);
70 }
71
72 inline void remove(int k) {
73     if (--cnt[k] == 0)add(k, -1);
74 }
75
76 /*<--- MAIN --->*/
77
78 signed main(void) {
81
82     s = sqrt(n);
83
84     for (int i = 1; i <= n; ++i)
86
87     for (int i = 1; i <= m; ++i) {
88         qry[i].id = i;
93     }
94
95     memset(cnt, 0, sizeof(cnt));
96     memset(sum, 0, sizeof(sum));
97     memset(sgl, 0, sizeof(sgl));
98
99     std::sort(qry + 1, qry + 1 + m, cmp_lr);
100
101     for (int i = 1, x = 1, y = 0; i <= m; ++i) {
102         while (x < qry[i].l)remove(num[x]), ++x;
103         while (y > qry[i].r)remove(num[y]), --y;
104         while (x > qry[i].l)--x, insert(num[x]);
105         while (y < qry[i].r)++y, insert(num[y]);
113 }