(map) zoj 1633

A - Alice‘s present

Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status Practice ZOJ 3633

Appoint description: 
System Crawler  (2015-01-27)

Description

As a doll master, Alice owns a wide range of dolls, and each of them has a number tip on it‘s back, the tip can be treated as a positive integer. (the number can be repeated). One day, Alice hears that her best friend Marisa‘s birthday is coming , so she decides to sent Marisa some dolls for present. Alice puts her dolls in a row and marks them from 1 to n. Each time Alice chooses an interval from i to jin the sequence ( include i and j ) , and then checks the number tips on dolls in the interval from right to left. If any number appears more than once , Alice will treat this interval as unsuitable. Otherwise, this interval will be treated as suitable.

This work is so boring and it will waste Alice a lot of time. So Alice asks you for help .

Input

There are multiple test cases. For each test case:

The first line contains an integer n ( 3≤ n ≤ 500,000) ,indicate the number of dolls which Alice owns.

The second line contains n positive integers , decribe the number tips on dolls. All of them are less than 2^31-1. The third line contains an interger m ( 1 ≤ m ≤ 50,000 ),indicate how many intervals Alice will query. Then followed by m lines, each line contains two integeruv ( 1≤ uv≤ n ),indicate the left endpoint and right endpoint of the interval. Process to the end of input.

Output

For each test case:

For each query, If this interval is suitable , print one line "OK". Otherwise, print one line ,the integer which appears more than once first.

Print an blank line after each case.

Sample Input

5
1 2 3 1 2
3
1 4
1 5
3 5
6
1 2 3 3 2 1
4
1 4
2 5
3 6
4 6

Sample Output

1
2
OK

3
3
3
OK

Hint

Alice will check each interval from right to left, don‘t make mistakes.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>
using namespace std;
map<int,int> mp;
#define N 500010
int n,num[N],m;
int main()
{
      int x,y;
      while(scanf("%d",&n)!=EOF)
      {
            int pos;
            for(int i=1;i<=n;i++)
                  scanf("%d",&num[i]);
            scanf("%d",&m);
            while(m--)
            {
                 bool flag=false;
                 mp.clear();
                 scanf("%d%d",&x,&y);
                 for(int i=y;i>=x;i--)
                 {
                       if(!mp.count(num[i]))
                       {
                             mp[num[i]]=1;
                       }
                       else
                       {
                             pos=i;
                             flag=true;
                             break;
                       }
                 }
                 if(!flag)
                 {
                       printf("OK\n");
                 }
                 else
                 {
                       printf("%d\n",num[pos]);
                 }
            }
            printf("\n");
      }
      return 0;
}

  

时间: 2024-08-29 02:49:44

(map) zoj 1633的相关文章

ZOJ 1633 Big String

Big String Time Limit: 2 Seconds      Memory Limit: 65536 KB We will construct an infinitely long string from two short strings: A = "^__^" (four characters), and B = "T.T" (three characters). Repeat the following steps: Concatenate A

ZOJ 1633

迭代 每个数对应前面的一个数 #include<stdio.h> #include<iostream> using namespace std; #define max 88 long long s[max], n,p; char *first="T.T^__^"; void Init() { s[0]=7; s[1]=10; int i; for(i=2; i <= max; i++) s[i]=s[i-1]+s[i-2]; } int getp() {

ZOJ 3209 Treasure Map (Dancing Links)

Treasure Map Time Limit: 2 Seconds      Memory Limit: 32768 KB Your boss once had got many copies of a treasure map. Unfortunately, all the copies are now broken to many rectangular pieces, and what make it worse, he has lost some of the pieces. Luck

ZOJ 3644 Kitty&#39;s Game dfs,记忆化搜索,map映射 难度:2

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的因数,所以不会形成环,只需从1直接走,走到n即可. 但是如果这样的话时空复杂度就都是nk,明显不满足题意,而这个时候我们可以想到,每个状态都必然是k的约数,(点数不是k的约数的节点不在路上,可以无视),而约数的个数也就k^0.5个,可以直接用map映射,这样时空复杂度都是n*k^0.5,可以解出答案

ZOJ 3348 Schedule(map运用+网络流之最大流)(竞赛问题升级版)

题目地址:ZOJ 3348 仍然是一道竞赛问题的网络流问题,但是这道题再用上次的竞赛建图方法就不行了,5000场比赛,明显会超时,于是需要换种建图思路了.上一道经典竞赛问题戳这里 上一道的胜负转换是利用专门给比赛建一个点,通过对比赛双方的流向来控制胜负关系,这里的建图方法更加巧妙(膜拜想出这个方法的大牛...),是先假设其中一方获胜,用mp[a][b]来表示a赢b的次数,将a与b连边,权值为mp[a][b],这样的话,前面的假设就仅仅只是假设而已,因为在这里,如果a的流量流向了b,说明a的胜利果

ZOJ 3209 Treasure Map DLX

用最少的矩阵覆盖n*m的地图,注意矩阵不能互相覆盖. 这里显然是一个精确覆盖,但由于矩阵拼接过程中,有公共的边,这里需要的技巧就是把矩阵的左边和下面截去一个单位. #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #

ZOJ 3209 Treasure Map(舞蹈链)

题目链接:[kuangbin带你飞]专题三 Dancing Links B - Treasure Map 题意 给一矩形和k个小矩形,问选取最小数量为多少的小矩形可以对大矩形进行精确覆盖. 思路 仍然是个模版题,把二维的n*m的大矩形看作是一维的n*m的一条线.k个小矩形同理,那么就转化成01矩阵精确覆盖的问题了. 代码 #include <iostream> #include <algorithm> #include <cstring> #include <cs

ZOJ 3209 Treasure Map(DLX精确覆盖)

Your boss once had got many copies of a treasure map. Unfortunately, all the copies are now broken to many rectangular pieces, and what make it worse, he has lost some of the pieces. Luckily, it is possible to figure out the position of each piece in

ZOJ 1755 &amp;&amp; POJ 1547 Clay Bully(简单排序+map容器)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1755 题意:Ms. Terry is a pre-school art teacher who likes to have her students work with clay. One of her assignments is to form a lump of clay into a block and then measure the dimensions