BZOJ 1878: [SDOI2009]HH的項鏈

1878: [SDOI2009]HH的項鏈

Time Limit:?4 Sec??Memory Limit:?64 MB
Submit:?3548??Solved:?1757
[Submit][Status][Discuss]

Description

HH有一串由各種漂亮的貝殼組成的項鏈。HH相信不同的貝殼會帶來好運,所以每次散步 完后,他都會隨意取出一段貝殼,思考它們所表達的含義。HH不斷地收集新的貝殼,因此, 他的項鏈變得越來越長。有一天,他突然提出了一個問題:某一段貝殼中,包含了多少種不同 的貝殼?這個問題很難回答。。。因為項鏈實在是太長了。于是,他只好求助睿智的你,來解 決這個問題。

Input

第一行:一個整數N,表示項鏈的長度。 第二行:N個整數,表示依次表示項鏈中貝殼的編號(編號為0到1000000之間的整數)。 第三行:一個整數M,表示HH詢問的個數。 接下來M行:每行兩個整數,L和R(1 ≤ L ≤ R ≤ N),表示詢問的區間。

Output

M行,每行一個整數,依次表示詢問對應的答案。

Sample Input

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

Sample Output

2
2
4

HINT


對于20%的數據,N ≤ 100,M ≤ 1000;
對于40%的數據,N ≤ 3000,M ≤ 200000;
對于100%的數據,N ≤ 50000,M ≤ 200000。

Source

Day2

[Submit][Status][Discuss]

?

OTZ NEIGHTHORN

樹狀數組O(NlogN) 或 莫隊算法O(N^1.5)

  1 #include <bits/stdc++.h>
  2 
  3 /* SCANNER */
  4 
  5 #define siz 1024
  6 
  7 inline int get_c(void)
  8 {
  9     static char buf[siz];
 10     static char *head = buf + siz;
 11     static char *tail = buf + siz;
 12     
 13     if (head == tail)
 14         fread(head = buf, 1, siz, stdin);
 15         
 16     return *head++;
 17 }
 18 
 19 inline int get_i(void)
 20 {
 21     register int ret = 0;
 22     register int neg = false;
 23     register int bit = get_c();
 24     
 25     for (; bit < 48; bit = get_c())
 26         if (bit == '-')neg ^= true;
 27         
 28     for (; bit > 47; bit = get_c())
 29         ret = ret * 10 + bit - 48;
 30         
 31     return neg ? -ret : ret;
 32 }
 33 
 34 #define N 50005
 35 #define M 200005
 36 #define V 1000005
 37 
 38 int n, m;
 39 int num[N];
 40 
 41 /* PREWORK */
 42 
 43 int nxt[N];
 44 int lst[V];
 45 int fst[V];
 46 
 47 /* QUERY */
 48 
 49 int lt[M];
 50 int rt[M];
 51 int ans[M];
 52 int ord[M];
 53 
 54 inline bool cmp(int a, int b)
 55 {
 56     return lt[a] < lt[b];
 57 }
 58 
 59 /* B I T */
 60 
 61 int tree[N];
 62 
 63 inline void add(int p, int k)
 64 {
 65     for (; p <= n; p += p&-p)
 66         tree[p] += k;
 67 }
 68 
 69 inline int ask(int p)
 70 {
 71     int ret = 0;
 72     for (; p >= 1; p -= p&-p)
 73         ret += tree[p];
 74     return ret;
 75 }
 76 
 77 /* MAIN */
 78 
 79 signed main(void)
 80 {
 81     n = get_i();
 82     
 83     for (int i = 1; i <= n; ++i)
 84         num[i] = get_i();
 85         
 86     for (int i = 0; i < V; ++i)
 87         lst[i] = n + 1;
 88         
 89     for (int i = n; i >= 1; --i)
 90         nxt[i] = lst[num[i]], lst[num[i]] = i;
 91         
 92     for (int i = 1; i <= n; ++i)
 93         if (!fst[num[i]])
 94         {
 95             add(i, 1);
 96             fst[num[i]] = 1;
 97         }
 98         
 99     m = get_i();
100     
101     for (int i = 1; i <= m; ++i)
102     {
103         ord[i] = i;
104         lt[i] = get_i();
105         rt[i] = get_i();
106     }
107     
108     std::sort(ord + 1, ord + 1 + m, cmp);
109     
110     int left = 1;
111     
112     for (int i = 1; i <= m; ++i)
113     {
114         while (left < lt[ord[i]])
115         {
116             add(left, -1);
117             add(nxt[left], 1);
118             ++left;
119         }
120         
121         ans[ord[i]] = ask(rt[ord[i]]);
122     }
123     
124     for (int i = 1; i <= m; ++i)
125         printf("%d\n", ans[i]);
126 }

?

@Author: YouSiki

轉載于:https://www.cnblogs.com/yousiki/p/6203159.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/394336.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394336.shtml
英文地址,請注明出處:http://en.pswp.cn/news/394336.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

分布式 知乎 github_如何使用GitHub本機功能來幫助管理中型分布式團隊

分布式 知乎 githubby Alex Ewerlf由AlexEwerlf 如何使用GitHub本機功能來幫助管理中型分布式團隊 (How to use GitHub native features to help manage a mid-size distributed team) My team created a wiki page in our private Github repo about how we work on a common…

開始時間小于 結束時間 js_DNF分享紅包開始及結束時間 紅包有什么獎勵相關介紹...

[閩南網]DNF分享紅包分享快樂時間從2019年的1月3日開始到1月21日前結束&#xff0c;活動期間玩家每天登錄游戲可以得到一個新年紅包&#xff0c;使用后可以為同一個頻道的玩家送去祝福&#xff0c;根據送出紅包的數量得到不同的獎勵。(dnf幸運餃子鋪活動)(DNF95版新副本攻略)本…

文件的相關操作

將輸出的內容直接輸出到文件中去 &#xff1a;freopen( “1.txt” , "w" , stdout &#xff09;轉載于:https://www.cnblogs.com/ccut-ry/p/7456190.html

leetcode1504. 統計全 1 子矩形(動態規劃)

給你一個只包含 0 和 1 的 rows * columns 矩陣 mat &#xff0c;請你返回有多少個 子矩形 的元素全部都是 1 。 示例 1&#xff1a; 輸入&#xff1a;mat [[1,0,1], [1,1,0], [1,1,0]] 輸出&#xff1a;13 解釋&#xff1a; 有 6 個 1x1 的矩形。 有 2 個 1x2 的矩形。 有 3…

學plc好還是python好_PLC是學西門子的好還是學三菱的?

有人回復的很經典&#xff1a;“小孩子才會選擇&#xff0c;大人肯定是都要。”如果你是學生&#xff0c;或者正準備踏入這個行業&#xff0c;建議你先學西門子的博途&#xff0c;畢竟這個在國內用的人多些。但是&#xff0c;你要時刻記得&#xff0c;你的目標是星辰大海~~~不要…

wps如何自己制作流程圖_怎么制作流程圖,wps自動生成流程圖方法

在職場中我們要會熟練使用各種辦公軟件&#xff0c;才能提高我們的工作效率&#xff0c;下面我為大家分享三種制作流程圖的方法&#xff0c;非常簡單哦&#xff01;一&#xff0c;在Word中制作流程圖1&#xff0c;首先點擊“插入”再點擊“形狀”,點擊新建繪圖畫布&#xff0c;…

doom 源碼_Cartpole和Doom的策略梯度簡介

doom 源碼by Thomas Simonini通過托馬斯西蒙尼(Thomas Simonini) Cartpole和Doom的策略梯度簡介 (An introduction to Policy Gradients with Cartpole and Doom) This article is part of Deep Reinforcement Learning Course with Tensorflow ??. Check the syllabus here…

SQL 郵件配置篇

在我們運維工作中&#xff0c;經常要對備份&#xff0c;ETL等作業進行監控&#xff0c;這時我們需要用到SQL SERVER自帶的郵件服務器&#xff0c;其原理&#xff0c;我在這么里不多說&#xff0c;直接來實戰&#xff0c;下面是我對服務器配置源碼&#xff0c;分享給大家&#x…

選定用戶與用戶組啟動流程(學習筆記)

public class RepostoryServiceTest {private static final Logger LOGGER LoggerFactory.getLogger(RepostoryServiceTest.class);Rulepublic ActivitiRule activitiRule new ActivitiRule();Testpublic void testRepository(){//repositoryService最重要的功能就是對流程定…

python關于包的題怎么做_Python自定義包引入

python中的Module是比較重要的概念。常見的情況是&#xff0c;事先寫好一個.py文 件&#xff0c;在另一個文件中需要import時&#xff0c;將事先寫好的.py文件拷貝 到當前目錄&#xff0c;或者是在中增加事先寫好的.py文件所在的目錄&#xff0c;然后import。這樣的做法&#x…

汽車之家的安全框架,是如何從0到1搭建的?

“別人家的安全”是安全威脅情報&#xff08;微信ID&#xff1a;Threatbook&#xff09;近期推出的一檔專欄。 合規、管理、構建、應急……安全問題千千萬&#xff0c;層出不窮。我們沒辦法給出這些問題的標準答案&#xff0c;但我們可以用Case Study的形式&#xff0c;讓你看看…

leetcode264. 丑數 II

編寫一個程序&#xff0c;找出第 n 個丑數。 丑數就是質因數只包含 2, 3, 5 的正整數。 示例: 輸入: n 10 輸出: 12 解釋: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 個丑數。 說明: 1 是丑數。 n 不超過1690。 解題思路 直接用treeset去重和排序 代碼 class Solution …

vr多人_如何構建多人VR網絡應用

vr多人by Srushtika Neelakantam通過Srushtika Neelakantam 如何構建多人VR網絡應用 (How to build a multiplayer VR web app) In this article, we’ll learn about three great frameworks/libraries that allow any web developer to build a VR app that works on any de…

量子測量 -- 確定性的死神

一、測量 -- 確定性的死神 前文已反復提及在量子世界中測量這一過程會產生很多奇異的、反直覺的現象。在第一篇文章中我舉的例子是&#xff1a;用同樣的配方&#xff0c;同樣的火候&#xff0c;同樣的廚具&#xff08;所有你能想到的變量均相同&#xff09;煎雞蛋&#xff0c;結…

python增刪改查csv文件_Python--作業2--對員工信息文件,實現增刪改查操作

#!/usr/bin/env python#-*- coding:utf-8 -*-#Author:Huanglinshengimportos#查詢方式一&#xff1a;select * from data_staff.txt where age > 22#查詢方式二&#xff1a;select * from data_staff.txt where dept "IT"#查詢方式三&#xff1a;select * from d…

ios注銷所有通知_您一直想了解的有關iOS中通知的所有信息

ios注銷所有通知by Payal Gupta通過Payal Gupta 您一直想了解的有關iOS中通知的所有信息 (Everything you’ve always wanted to know about notifications in iOS) 漂亮的小警報..&#xff1f; (Pretty Little Alerts..?) Notifications are a way to inform users when new…

vue-x

https://my.oschina.net/wangnian/blog/2055631轉載于:https://www.cnblogs.com/ylblogs/p/10694849.html

leetcode97. 交錯字符串(動態規劃)

給定三個字符串 s1, s2, s3, 驗證 s3 是否是由 s1 和 s2 交錯組成的。 示例 1: 輸入: s1 “aabcc”, s2 “dbbca”, s3 “aadbbcbcac” 輸出: true 解題思路 數組含義&#xff1a;dp[i][j]s1的前i個和s2的前j個能否組成字符串s3的前ij長度的子串 狀態轉移&#xff1a; d…

【LeetCode】19. Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n 2.After removing the second node from the end, the linked list becomes 1->2->3->5.題意&#xff1a;…

《網絡空間欺騙:構筑欺騙防御的科學基石》一1.1 主動網絡空間防御中網絡空間抵賴與欺騙的視圖...

1.1 主動網絡空間防御中網絡空間抵賴與欺騙的視圖 本文講的是網絡空間欺騙&#xff1a;構筑欺騙防御的科學基石一1.1 主動網絡空間防御中網絡空間抵賴與欺騙的視圖,將抵賴與欺騙納入標準操作規程&#xff08;SOP&#xff09;&#xff1a;隨著攻擊技術的不斷演進&#xff0c;網…