1. 程式人生 > >Different Integers 牛客多校第一場只會簽到題

Different Integers 牛客多校第一場只會簽到題

sts memset nbsp tdi num contains 簡單 輸出 struct

Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, rq), find count(l1, r1), count(l2, r2), ..., count(lq, rq) where count(i, j) is the number of different integers among a1, a2, ..., ai, aj, aj + 1, ..., an.
輸入描述:
The input consists of several test cases and is terminated by end-of-file.The first line of each test cases contains two integers n and q.The second line contains n integers a1, a2, ..., an.The i-th of the following q lines contains two integers li and ri.
輸出描述:
For each test case, print q integers which denote the result.

示例1

輸入
復制

3 2
1 2 1
1 2
1 3
4 1
1 2 3 4
1 3

輸出
復制

2
1
3


備註:
* 1 ≤ n, q ≤ 105* 1 ≤ ai ≤ n* 1 ≤ li, ri ≤ n* The number of test cases does not exceed 10.

這是唯一一題簽到題 ,現場找的板子,修改一下過的

最簡單粗暴的方法,把數組擴張一倍

修改一下查詢區間。板子題的變形吧

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cmath>
 5
#include <cstdlib> 6 #include <cstring> 7 #include <vector> 8 #include <list> 9 #include <map> 10 #include <stack> 11 #include <queue> 12 using namespace std; 13 #define ll long long 14 const int maxn1 = 2000005; 15 int cur[maxn1]; 16 int then[maxn1]; 17 int ans[maxn1];
18 int limit, n, m; 19 struct node { 20 int l, r, id; 21 } que[maxn1]; 22 bool cmp(node x, node y) { 23 if(x.l / limit == y.l / limit) return x.r < y.r; 24 return x.l / limit < y.l / limit; 25 } 26 void solve() { 27 int L, R, ans1; 28 L = R = 0; 29 ans1 = 0; 30 for(int i = 1; i <= m; i++) { 31 while(que[i].l > L) { 32 then[cur[L]]--; 33 if(then[cur[L]] == 0) 34 ans1--; 35 L++; 36 } 37 while(que[i].r < R) { 38 then[cur[R]]--; 39 if(then[cur[R]] == 0) 40 ans1--; 41 R--; 42 } 43 while(que[i].l < L) { 44 L--; 45 then[cur[L]]++; 46 if(then[cur[L]] == 1) 47 ans1++; 48 } 49 while(que[i].r > R) { 50 R++; 51 then[cur[R]]++; 52 if(then[cur[R]] == 1) 53 ans1++; 54 } 55 ans[que[i].id] = ans1; 56 } 57 for(int i = 1; i <= m; i++) 58 printf("%d\n", ans[i]); 59 } 60 int main() { 61 while(scanf("%d", &n) != EOF) { 62 scanf("%d", &m); 63 memset(cur, 0, sizeof(cur)); 64 memset(then, 0, sizeof(then)); 65 memset(ans, 0, sizeof(ans)); 66 for(int i = 1; i <= n; i++) 67 scanf("%d", &cur[i]); 68 for (int i = n + 1 ; i <= 2 * n ; i++) 69 cur[i] = cur[i - n]; 70 for(int i = 1; i <= m; i++) { 71 scanf("%d%d", &que[i].l, &que[i].r); 72 int temp1 = que[i].l, temp2 = que[i].r; 73 que[i].l = temp2, que[i].r = temp1 + n; 74 que[i].id = i; 75 } 76 limit = (int)(sqrt(2 * n) + 0.5); 77 memset(then, 0, sizeof(then)); 78 sort(que + 1, que + 1 + m, cmp); 79 solve(); 80 } 81 return 0; 82 }

Different Integers 牛客多校第一場只會簽到題