【CF】220B Little Elephant and Array
区间动态统计的好题。
/* */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct inter_t {
int l, r, id, ans;
inter_t() {}
inter_t(int _l, int _r, int _id, int _ans=):
l(_l), r(_r), id(_id), ans(_ans) {}
friend bool operator< (const inter_t& a, const inter_t& b) {
if (a.r == b.r)
return a.l > b.l;
else
return a.r < b.r;
}
} inter_t; const int maxn = 1e5+;
vector<int> vc[maxn];
int sz[maxn];
vector<inter_t> Q;
map<int,int> tb;
int d[maxn];
int a[maxn], l = ;
int v[maxn]; bool comp(const inter_t& a, const inter_t& b) {
return a.id < b.id;
} int main() {
int i, j, k;
int n, m; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%d %d", &n, &m);
for (i=; i<=n; ++i)
scanf("%d", &a[i]); int li, ri;
for (i=; i<m; ++i) {
scanf("%d %d", &li, &ri);
Q.push_back(inter_t(li, ri, i));
}
sort(Q.begin(), Q.end());
Q.push_back(inter_t(,n+,m));
memset(d, , sizeof(d));
memset(sz, , sizeof(sz)); int r = , mr, x, id, p, cnt;
i = ;
while (i < m) {
mr = Q[i].r;
while (r <= mr) {
x = a[r];
id = tb[x];
if (id == ) {
tb[x] = id = l;
l++;
v[l] = x;
}
vc[id].push_back(r);
++sz[id];
if (sz[id] >= x) {
p = vc[id][sz[id]-x];
++d[p];
}
if (sz[id] >= x+) {
p = vc[id][sz[id]-x-];
d[p] -= ;
}
if (sz[id] > x+) {
p = vc[id][sz[id]-x-];
++d[p];
}
++r;
}
cnt = ;
k = mr+;
while (Q[i].r == mr) {
for (j=Q[i].l; j<k; ++j)
cnt += d[j];
Q[i].ans = cnt;
k = Q[i].l;
++i;
}
} sort(Q.begin(), Q.end(), comp);
for (i=; i<m; ++i)
printf("%d\n", Q[i].ans); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【CF】220B Little Elephant and Array的更多相关文章
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】Search in Rotated Sorted Array——旋转有序数列找目标值
[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- php基础知识【函数】(1)数组array
一.排序 1.sort -- 从最低到最高排序,删除原有的键名,赋予新的键名[字母比数字高] 2.rsort -- 逆向排序(最高到最低),删除原有的键名,赋予新的键名[字母比数字高] 3.asort ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 【leetcode】Search in Rotated Sorted Array
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- 【leetcode】Search in Rotated Sorted Array (hard)
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- vs2012 aspx 没有设计视图了?
vs2012的html设计视图没有了!重新安装一次都不行!现在已经通过简单办法来解决了 其实当你打开 HTML设计器 设置时, “启用 HTML设计器" 这里是打勾的!这时千万不要放弃.先 ...
- 父页面iframe自动适应子页面的宽高度
<script type="text/javascript"> function load(){ parent.document.getElementById(&quo ...
- Unity3D 商店下载的package存放位置
如果你需要将下载下来的包保存下来,以后使用的话 那这篇文章,将对你有用. w7系统: C:\Users\Administrator\AppData\Roaming\Unity\Asset Store
- mybatis中几种typeHandler的定义使用
1.存储到数据库, 将LONG数组转换成字符串;从数据库获取数据, 将字符串转为LONG数组 package com.winturn.utils.handler; import java.sql.Ca ...
- chrome偶尔弹出新窗口的解决方案
最近使用谷歌浏览器,在搜索页点击搜索结果时,偶尔会弹出新窗口,而不是新标签,试验发现,只要将chrome里面安装的google drive app卸载就行了. 当然了,如果此方法不适合你的情况,还可以 ...
- Android-The specified child already has a parent. You must call removeView() on the child's parent first.
这个问题搞了我半天了,网上有很多人说需要找到该控件的parent后,让该parent 先remove需要添加的控件,然后在添加,如: if (view != null) { ViewGroup par ...
- 三步走起 提升 iOS 审核通过率 上篇
<ignore_js_op> Bugly 技术干货系列内容主要涉及移动开发方向,是由 Bugly 邀请腾讯内部各位技术大咖,通过日常工作经验的总结以及感悟撰写而成,内容均属原创,转载请标明 ...
- Objective-C发展历史
Objective-C发展历史 苹果图标由来: 被咬了一口苹果的LOGO是为了纪念计算机科学的创始人阿兰· 麦席森· 图灵.当年图灵由于身为同性恋者,被强行 "治疗",在被迫注射大 ...
- C# DataTable的詳細用法 - hcw_peter的专栏 - 博客频道 - CSDN
C# DataTable的詳細用法 - hcw_peter的专栏 - 博客频道 - CSDN.NET 在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够 ...
- AngularJS 路由:ui-router
UI-Router是Angular-UI提供的客户端路由框架,它解决了原生的ng-route的很多不足:视图不能嵌套.这意味着$scope会发生不必要的重新载入.这也是我们在Onboard中引入ui- ...