diff --git a/S2OJ/1447/1447.cpp b/S2OJ/1447/1447.cpp new file mode 100644 index 00000000..45bdf5dd --- /dev/null +++ b/S2OJ/1447/1447.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 100005; + +int n, t, a[N], st[N], ed[N], pos[N], f[1005][1005], cnt[N]; +std::vector nums, v[N]; + +inline int find(int x) { + return std::lower_bound(nums.begin(), nums.end(), x) - nums.begin(); +} + +void pre(int x) { + memset(cnt, 0x00, sizeof(cnt)); + std::pair res; + + for (int i = st[x]; i <= n; i++) { + cnt[a[i]]++; + + if (cnt[a[i]] > res.first || (cnt[a[i]] == res.first && nums[a[i]] < nums[res.second])) { + res = std::make_pair(cnt[a[i]], a[i]); + } + + f[x][pos[i]] = res.second; + } +} + +inline int get(int x, int l, int r) { + return std::upper_bound(v[x].begin(), v[x].end(), r) - std::lower_bound(v[x].begin(), v[x].end(), l); +} + +int query(int l, int r) { + int p = pos[l], + q = pos[r]; + std::pair res; + + if (p == q) { + for (int i = l; i <= r; i++) { + int _ = get(a[i], l, r); + if (_ > res.first || (_ == res.first && nums[a[i]] < nums[res.second])) { + res = std::make_pair(_, a[i]); + } + } + + return nums[res.second]; + } + + res.second = f[p + 1][q - 1]; + res.first = get(res.second, l, r); + + for (int i = l; i <= ed[p]; i++) { + int _ = get(a[i], l, r); + if (_ > res.first || (_ == res.first && nums[a[i]] < nums[res.second])) { + res = std::make_pair(_, a[i]); + } + } + for (int i = st[q]; i <= r; i++) { + int _ = get(a[i], l, r); + if (_ > res.first || (_ == res.first && nums[a[i]] < nums[res.second])) { + res = std::make_pair(_, a[i]); + } + } + + return nums[res.second]; +} + +int main() { + std::ios::sync_with_stdio(false); + + cin >> n; + + t = std::sqrt(n); + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + pos[i] = (i - 1) / t + 1; + nums.push_back(a[i]); + } + + std::sort(nums.begin(), nums.end()); + nums.erase(std::unique(nums.begin(), nums.end()), nums.end()); + nums.insert(nums.begin(), 0); + + for (int i = 1; i <= n; i++) v[a[i] = find(a[i])].push_back(i); + + for (int i = 1; i <= pos[n]; i++) { + st[i] = (i - 1) * t + 1; + ed[i] = std::min(n, i * t); + } + + for (int i = 1; i <= pos[n]; i++) pre(i); + + for (int i = 1, l, r; i <= n; i++) { + cin >> l >> r; + cout << query(l, r) << endl; + } + + return 0; +} diff --git a/S2OJ/1447/data/a1.in b/S2OJ/1447/data/a1.in new file mode 100644 index 00000000..b1972814 --- /dev/null +++ b/S2OJ/1447/data/a1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:99c37a9b8a9c9ed91b989bb63b7188c0aadf9f7f7bb89aeab6632f964d809b9e +size 102843 diff --git a/S2OJ/1447/data/a1.out b/S2OJ/1447/data/a1.out new file mode 100644 index 00000000..45348f04 --- /dev/null +++ b/S2OJ/1447/data/a1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:48a9b12c70005f8f16917a4e0d5581096014ddbff3af92ea91a5ea12ee075806 +size 55000 diff --git a/S2OJ/1447/data/a10.in b/S2OJ/1447/data/a10.in new file mode 100644 index 00000000..700bd32b --- /dev/null +++ b/S2OJ/1447/data/a10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f7199b5b7a72b8fa3f7ef48358a0c9b0b492acaa33cde96796fb8ee5f65d80c0 +size 1127758 diff --git a/S2OJ/1447/data/a10.out b/S2OJ/1447/data/a10.out new file mode 100644 index 00000000..7838c514 --- /dev/null +++ b/S2OJ/1447/data/a10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:923c5f0d8eb6704c4a0031382fc6c0ac413cf779c42967f20a105b799aabcc26 +size 550000 diff --git a/S2OJ/1447/data/a2.in b/S2OJ/1447/data/a2.in new file mode 100644 index 00000000..b1972814 --- /dev/null +++ b/S2OJ/1447/data/a2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:99c37a9b8a9c9ed91b989bb63b7188c0aadf9f7f7bb89aeab6632f964d809b9e +size 102843 diff --git a/S2OJ/1447/data/a2.out b/S2OJ/1447/data/a2.out new file mode 100644 index 00000000..45348f04 --- /dev/null +++ b/S2OJ/1447/data/a2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:48a9b12c70005f8f16917a4e0d5581096014ddbff3af92ea91a5ea12ee075806 +size 55000 diff --git a/S2OJ/1447/data/a3.in b/S2OJ/1447/data/a3.in new file mode 100644 index 00000000..e5b71508 --- /dev/null +++ b/S2OJ/1447/data/a3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d074454275131f817eba7dd13c9c3d235e23cd57289275bc56999885617b3886 +size 102724 diff --git a/S2OJ/1447/data/a3.out b/S2OJ/1447/data/a3.out new file mode 100644 index 00000000..bb028e6a --- /dev/null +++ b/S2OJ/1447/data/a3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:440a29dff36336c69b121bc8fd867427da37d0f889cda6e25518dd6473bd0824 +size 55000 diff --git a/S2OJ/1447/data/a4.in b/S2OJ/1447/data/a4.in new file mode 100644 index 00000000..eeadb957 --- /dev/null +++ b/S2OJ/1447/data/a4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ae3ce1e7d1261ce456b7f7f3bc469280845e693177c0a6094fad9f2b961978bf +size 1127823 diff --git a/S2OJ/1447/data/a4.out b/S2OJ/1447/data/a4.out new file mode 100644 index 00000000..ca3dea3e --- /dev/null +++ b/S2OJ/1447/data/a4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:29231ab1bc1e71c518ea75442835aa8afa61aa448f3b4aa634b98550c9ecaa9c +size 550000 diff --git a/S2OJ/1447/data/a5.in b/S2OJ/1447/data/a5.in new file mode 100644 index 00000000..3c33dd03 --- /dev/null +++ b/S2OJ/1447/data/a5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8d27d159a1414ab1454ee0eeaeee616c852097b251cfe0b4b353226ffe660d7d +size 1127756 diff --git a/S2OJ/1447/data/a5.out b/S2OJ/1447/data/a5.out new file mode 100644 index 00000000..c8ad47c8 --- /dev/null +++ b/S2OJ/1447/data/a5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:75b240fb102660cfa34750187385fa4a2ff3bc3c443382e74665db222915e75b +size 550000 diff --git a/S2OJ/1447/data/a6.in b/S2OJ/1447/data/a6.in new file mode 100644 index 00000000..11b8621e --- /dev/null +++ b/S2OJ/1447/data/a6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:68eedd4851d68565237ca1105e54fc829553f0338f793140565148b23aa29e62 +size 1127973 diff --git a/S2OJ/1447/data/a6.out b/S2OJ/1447/data/a6.out new file mode 100644 index 00000000..bf44c04a --- /dev/null +++ b/S2OJ/1447/data/a6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2a0011c052a384ba1588c64ef82219165b363ac45bea51ef42d129e85bcd6c42 +size 550000 diff --git a/S2OJ/1447/data/a7.in b/S2OJ/1447/data/a7.in new file mode 100644 index 00000000..deacc0cc --- /dev/null +++ b/S2OJ/1447/data/a7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a6249e97f0ff6f3a9f1c39ad2508ddd707c71421c1d80c23e37fc06454a50ee0 +size 1127655 diff --git a/S2OJ/1447/data/a7.out b/S2OJ/1447/data/a7.out new file mode 100644 index 00000000..b214283d --- /dev/null +++ b/S2OJ/1447/data/a7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d8668a777f26efeda1383f1dd4de83d780beedabe27231fd48358a1505ed89fc +size 550000 diff --git a/S2OJ/1447/data/a8.in b/S2OJ/1447/data/a8.in new file mode 100644 index 00000000..d3ffb3fa --- /dev/null +++ b/S2OJ/1447/data/a8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9c192247d4325f1a5f473c218dbb1f1f1cff95f03e1f66e46c8c91df3d5aa938 +size 1128097 diff --git a/S2OJ/1447/data/a8.out b/S2OJ/1447/data/a8.out new file mode 100644 index 00000000..f84c71e9 --- /dev/null +++ b/S2OJ/1447/data/a8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0818e4347c667c19b3339ab19042d526b29e94c547dbc8339e3b786547f6e0a0 +size 550000 diff --git a/S2OJ/1447/data/a9.in b/S2OJ/1447/data/a9.in new file mode 100644 index 00000000..93e8d5ca --- /dev/null +++ b/S2OJ/1447/data/a9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d62a5f646a81a3472e67f67675f5c5b3484412c070d1fa17177b222819ad3677 +size 1127949 diff --git a/S2OJ/1447/data/a9.out b/S2OJ/1447/data/a9.out new file mode 100644 index 00000000..7ff00a3e --- /dev/null +++ b/S2OJ/1447/data/a9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9176ccd8e7afa6eb1cdf3948c23810b792b55d578473523528d12b390e88ebec +size 550000 diff --git a/S2OJ/1447/data/ex_a1.in b/S2OJ/1447/data/ex_a1.in new file mode 100644 index 00000000..d4d1a31e --- /dev/null +++ b/S2OJ/1447/data/ex_a1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3cd158e4e307f77dc916354975100d8749ddd0d4335a96213ba97fe4f2f0defd +size 1766997 diff --git a/S2OJ/1447/data/ex_a1.out b/S2OJ/1447/data/ex_a1.out new file mode 100644 index 00000000..c2633a9d --- /dev/null +++ b/S2OJ/1447/data/ex_a1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:22970b9557720b84e2b08c7eaabc08e8314ff22dd95c9a571d4fce23d6a44989 +size 541496 diff --git a/S2OJ/1447/data/ex_a2.in b/S2OJ/1447/data/ex_a2.in new file mode 100644 index 00000000..9e009237 --- /dev/null +++ b/S2OJ/1447/data/ex_a2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:edbbf657c60466bd64c0284fecf803bd2abeab409522b7ca857e1315a4286292 +size 1755426 diff --git a/S2OJ/1447/data/ex_a2.out b/S2OJ/1447/data/ex_a2.out new file mode 100644 index 00000000..063f8b91 --- /dev/null +++ b/S2OJ/1447/data/ex_a2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:959f0a108dd3a7a398ef31b586c17d8c3430fcec856b2c5e3843dff76f5c4a0a +size 544384 diff --git a/S2OJ/1447/data/ex_a3.in b/S2OJ/1447/data/ex_a3.in new file mode 100644 index 00000000..d4a94fa1 --- /dev/null +++ b/S2OJ/1447/data/ex_a3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c886fdf2567a3709d980397f7a66dc73644479aaeb18783b8822d5ad09a9a2ed +size 1544116 diff --git a/S2OJ/1447/data/ex_a3.out b/S2OJ/1447/data/ex_a3.out new file mode 100644 index 00000000..8aeab491 --- /dev/null +++ b/S2OJ/1447/data/ex_a3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c08d1977dc8d6efedefec075523173ba06c88f12e28cce215be91ca0489f34ef +size 351994 diff --git a/S2OJ/1447/data/ex_a4.in b/S2OJ/1447/data/ex_a4.in new file mode 100644 index 00000000..b7674084 --- /dev/null +++ b/S2OJ/1447/data/ex_a4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fb6c5b2b0bf3eb5204c9f1bdb1c51fca750894382c70b6b6cb0fece643e6e4bf +size 1421550 diff --git a/S2OJ/1447/data/ex_a4.out b/S2OJ/1447/data/ex_a4.out new file mode 100644 index 00000000..36851f5c --- /dev/null +++ b/S2OJ/1447/data/ex_a4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:65583397d8ad4683a2f78db325a483701c0034c2e21a82e92dd7d2f951e7ec4d +size 280263 diff --git a/S2OJ/1447/data/ex_a5.in b/S2OJ/1447/data/ex_a5.in new file mode 100644 index 00000000..fb37e5f2 --- /dev/null +++ b/S2OJ/1447/data/ex_a5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:480aac4abb64b1757e767710fe4e3a97f3b3a442cac390e8f00faa86cb152fe6 +size 1377920 diff --git a/S2OJ/1447/data/ex_a5.out b/S2OJ/1447/data/ex_a5.out new file mode 100644 index 00000000..f97fd0fa --- /dev/null +++ b/S2OJ/1447/data/ex_a5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6d03b827dd6c0898e82bdd7329d8b99e022118194ab8543d4dbe771b7749ee8a +size 200000