区间动态统计的好题。

 /*  */
#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的更多相关文章

  1. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  2. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  3. 【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 ...

  4. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  5. php基础知识【函数】(1)数组array

    一.排序 1.sort -- 从最低到最高排序,删除原有的键名,赋予新的键名[字母比数字高] 2.rsort -- 逆向排序(最高到最低),删除原有的键名,赋予新的键名[字母比数字高] 3.asort ...

  6. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  7. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  8. 【leetcode】Search in Rotated Sorted Array

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  9. 【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 ...

随机推荐

  1. 以非root权限安装nginx及运行

    本章主要讲如何在无root权限(包含无sudo权限)条件下于centos命令行中安装nginx以及在大于1024的端口(这里用8080)上运行. 1. 安装 两种方式,一是下载预编译好的rpm包安装, ...

  2. http实现方式概念学习笔记

    web概念:web1.0:静态页面为主,门户新闻.企业宣传web2.0:动态页面为主,用户参与,bbs,blog,sns,微博            web3.0:web2.0基础上,智能化,人性化, ...

  3. Linux只iptables

    1. 查看<strong>网络</strong>监听的端口: netstat -tunlp 2. 查看本机的路由规则: route stack@ubuntu:~$ route ...

  4. .NET 设计模式之简单工厂模式(二)

    1:建立接口 namespace Factory { public interface IPerson { } } 2:建立Worker.Student来继承IPerson接口 namespace F ...

  5. C# json与对象之间的相互转换

    1. 添加命名空间 using System.Runtime.Serialization.Json; 2. WriteObject方法 // 从一个对象信息生成Json串 public static ...

  6. CSS制作hover下划线动画

    .demo1{ position: relative; text-decoration: none; font-size: 20px; color: #333; } .demo1:before{ co ...

  7. Memcached认知[分布式]

    Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. Memcached的服务器客户端通信使用简单的基于文本行的协议. Memcached基于一个存储键/值对的 ...

  8. 从外国html5网站上扒来一个鼠标经过的css3 效果,感觉很不错

    鼠标经过的时候,感觉有点像一张纸卷上去的感觉. 下面是代码 <div class="main-container types"> <div class=" ...

  9. C++ 类访问控制(public/protected/private)

    第一:private, public, protected 访问标号的访问范围. private:只能由1.该类中的函数.2.其友元函数访问. 不能被任何其他访问,该类的对象也不能访问. protec ...

  10. Linux下彻底卸载LibreOffice方法

    Linux下彻底卸载LibreOffice方法 终端中输入命令: 对所有基于 Debian 的发行版(Debian.Ubuntu.Kubuntu.Xubuntu.*buntu.Sidux 等): su ...