Gym 100114H Milestones(离线树状数组)
来源:互联网 发布:php电影网站免费源码 编辑:程序博客网 时间:2024/05/17 09:02
思路:和之前那题一样,找区间不重复的数出现多少个,离线树状数组即可
#include<bits/stdc++.h>using namespace std;const int maxn = 100000+500;struct Node{int l,r,id,ans;}que[maxn];int c[maxn],p[maxn],next[maxn],a[maxn];int lowbit(int x){return x&(-x);}void update(int i,int d){while(i<=maxn){c[i]+=d;i+=lowbit(i);}}int query(int i){int ans = 0;while(i){ans+=c[i];i-=lowbit(i);}return ans;}bool cmp1(Node a,Node b){if(a.l==b.l)return a.r<b.r;return a.l<b.l;}bool cmp2(Node a,Node b){return a.id<b.id;}int main(){ freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);int Max = 0; int n,k;while(~scanf("%d",&n)){memset(c,0,sizeof(c));memset(next,0,sizeof(next));memset(p,0,sizeof(p));scanf("%d",&k);for(int i = 1;i<=n;i++)scanf("%d",&a[i]),Max = max(a[i],Max);for(int i = n;i>=1;i--)next[i]=p[a[i]],p[a[i]]=i;for(int i = 1;i<=Max;i++)if(p[i])update(p[i],1);for(int i = 1;i<=k;i++){scanf("%d%d",&que[i].l,&que[i].r);que[i].id=i;}sort(que+1,que+1+k,cmp1);int l = 1;for(int i = 1;i<=k;i++){while(l<que[i].l){if(next[l])update(next[l],1);l++;}que[i].ans = query(que[i].r)-query(que[i].l-1);}sort(que+1,que+1+k,cmp2);for(int i = 1;i<=k;i++)printf("%d\n",que[i].ans);}}
The longest road of the Fairy Kingdom has n milestones. A long-established
tradition defines a specific color for milestones in each region, with a total of m colors
in the kingdom. There is a map describing all milestones and their colors.
A number of painter teams are responsible for milestone maintenance and
painting. Typically, each team is assigned a road section spanning from milestone #l
to milestone #r. When optimizing the assignments, the supervisor often has to determine
how many different colors it will take to paint all milestones in the section l…r.
Example. Suppose there are five milestones #1, #2, #3, #4, #5 to be painted
with colors 1, 2, 3, 2, 1, respectively. In this case, only two different paints are necessary
for milestones 2…4: color 2 for milestones #2 and #4, and color 3 for milestone
#3.
Write a program that, given a map, will be able to handle multiple requests of
the kind described above.
Limitations
1 ≤ n ≤ 10 000; 1≤ m ≤ 255; 1 ≤ li ≤ ri ≤ n; 1 ≤ k ≤ 100 000.
Input
The first line contains two integers, n and k – the number of milestones and the
number of requests, respectively. The second line consists of n integers separated by
spaces and defines the sequence of colors for milestones from #1 to #n. The following
k lines contain pairs of integers, one pair per line. Each pair consists of two numbers
– li and ri – and defines a range of milestones for request i.
Output
The output file should contain k integers separated by line breaks. Result number
i should present the result of the i-th request, i. e. the number of colors required to
paint all milestones in the road section li…ri.
Example
Input.txt Output.txt
5 3
1 2 3 2 1
1 5
1 3
2 4
3
3
2
- Gym 100114H Milestones(离线树状数组)
- Gym 101572G Galactic Collegiate Programming Contest(离线 树状数组)
- 1018(离线树状数组)
- HDU 4417 Super Mario(12年杭州网络赛-H题-离线 + 树状数组)
- 2013 Asia Regional Contest Problem H --- Number Squence (树状数组 + 离线)
- SCU2016-04 H题 质因数处理 + 离线区间树状数组
- D - Data Mining-Gym 100496D-离线处理+树状数组+离散化
- hdu4630(树状数组+离线处理)
- hdu 4638 Group (树状数组+离线)
- HDU 4638 Group(离线 + 树状数组)
- hdu 5057(树状数组+离线处理)
- hdu-5057(离线的树状数组)
- HDU--3333(树状数组+离线处理)
- hdu 3874(树状数组+离线算法)
- [BZOJ 2743]采花(树状数组+离线)
- HDU3874 (树状数组+离线操作)
- 2743: [HEOI2012]采花(离线树状数组)
- 1006(离线操作+树状数组)
- Poj 3648 Wedding【2-Sat--------Tarjan强连通+缩点染色+拓扑排序】
- Jenkins插件开发入门学习
- 配置SSH无密码登录
- 理解Java基础之注解Annotation
- Web应用程序状态跟踪
- Gym 100114H Milestones(离线树状数组)
- jquery根据value修改select选中项
- 支持向量机SVM(一)
- zabbix监控web
- 智能指针shared_ptr的实现
- JPEG编解码过程详解
- Solr中的Field、CopyField、DynamicField与其他Field
- codeforces 366 C Dima and Salad dp
- leetcode【67】-Bulb Switcher