P1903 [TEMPLATE] multipart/with modification of Moq (number color), p1903

Source: Internet
Author: User

P1903 [TEMPLATE] multipart/with modification of Moq (number color), p1903
Description

Mo mo purchased a set of N color brushes (some of which may have the same color) and placed them in a row. You need to answer Mo's questions. Mo mo will issue the following command as you do:

1. q l r indicates asking you how many different colors there are from the L paint brush to the R paint brush.

2. r p Col replaces the P paint brush with the color Col.

Do you know what you need to do to meet Momo's requirements?

Input/Output Format

Input Format:

 

Two integers (N and M) in the row 1st represent the number of the initial paint brushes and the number of tasks that ink and ink will do.

N integers in row 2nd, representing the I-th paint brush color in the first paint brush row, respectively.

Lines from 3rd to 2nd + M represent one thing that Mo mo will do. For details about the format, refer to cadre points.

 

Output Format:

 

For each Query, you need to give a number in the corresponding row, representing the number of different colors from the L paint brush to the R paint brush.

 

Input and Output sample input sample #1:
6 51 2 3 4 5 5Q 1 4Q 2 6R 1 2Q 1 4Q 2 6
Output sample #1:
4434
Description

For 100% of data, N ≤ 10000, M ≤ 10000, the modification operation is no more than 1000 times. All integers in all input data are greater than or equal to 1 and cannot exceed 10 ^ 6.

Source: bzoj2120

The data in this question is self-built data of Luogu. It takes 5 minutes to complete data production using CYaRon.

 

Bare mops with modifications

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <algorithm> 6 # include <cstdlib> 7 # include <ctime> 8 using namespace std; 9 const int MAXN = 10001; 10 static void read (int & n) 11 {12 char c = '+'; int x = 0; bool flag = 0; 13 while (c <'0' | c> '9') {c = getchar (); if (c = '-') flag = 1 ;} 14 while (c> = '0' & c <= '9') {x = (x <1) + (x <3) + (c-48 ); c = getchar ();} 15 flag = 1? N =-x: n = x; 16} 17 int n, m; 18 int a [MAXN]; 19 struct CX 20 {21 int l, r, id, tm; // tm last change operation 22} cx [MAXN]; 23 int cxnum; 24 struct GG 25 {26 int pos, val, pre; 27} gg [MAXN]; 28 int ggnum; 29 int head [MAXN]; 30 int where [MAXN]; 31 int base; 32 int vis [MAXN]; // whether there is any change operation 33 int color [MAXN]; 34 int ans = 0; 35 int out [MAXN]; 36 int comp (const CX & a, const CX & B) 37 {38 if (where [. l] = where [B. l]) 39 return. R <B. r; 40 else 41 return where [. l] <where [B. l]; 42} 43 int calc (int x) 44 {45 if (vis [x]) 46 {47 if (-- color [a [x] = 0) 48 ans --; 49} 50 else 51 {52 if (++ color [a [x] = 1) 53 ans ++; 54} 55 vis [x] =! Vis [x]; 56} 57 void change (int p, int v) 58 {59 if (vis [p]) 60 {61 calc (p ); 62 a [p] = v; 63 calc (p); 64} 65 else 66 a [p] = v; 67} 68 69 int main () 70 {71 read (n); read (m); 72 for (int I = 1; I <= n; I ++) 73 read (a [I]), head [I] = a [I]; 74 base = sqrt (n); 75 for (int I = 1; I <= n; I ++) 76 where [I] = (I-1)/base + 1; 77 for (int I = 1; I <= m; I ++) 78 {79 char c; 80 int x, y; 81 cin> c; 82 read (x); read (y); 83 if (c = 'q') // query 84 {8 5 cxnum ++; 86 cx [cxnum]. l = x; 87 cx [cxnum]. r = y; 88 cx [cxnum]. id = cxnum; 89 cx [cxnum]. tm = ggnum; 90} 91 else 92 {93 ggnum ++; 94 gg [ggnum]. pos = x; 95 gg [ggnum]. val = y; 96 gg [ggnum]. pre = head [x]; 97 head [x] = y; 98} 99} 100 sort (cx + 1, cx + cxnum + 1, comp ); 101 int l = 1, r = 0; 102 for (int I = 1; I <= cxnum; I ++) 103 {104 for (int j = cx [I-1]. tm + 1; j <= cx [I]. tm; j ++) 105 change (gg [j]. pos, gg [j]. val); 106 for (int j = cx [I-1]. tm; j> = cx [I ]. Tm + 1; j --) 107 change (gg [j]. pos, gg [j]. pre); // This is pre, not val !!! 108 for (; l <cx [I]. l; l ++) 109 calc (l); 110 for (; l> cx [I]. l; l --) 111 call-1); 112 for (; r <cx [I]. r; r ++) 113 calc (r + 1); 114 for (; r> cx [I]. r, r --) 115 calc (r); 116 out [cx [I]. id] = ans; 117} 118 for (int I = 1; I <= cxnum; I ++) 119 printf ("% d \ n", out [I]); 120 return 0; 121}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.