Hdu3333-Turing tree-line segment tree + offline + discretization

Source: Internet
Author: User

Reference XX-I am a Qaq pig .....

I will not repeat the question.

Solution:

General achievements and summation.

Discretization: record the original array with another array sor [], sort again, de-duplicate with unique, and use sor [] array element subscript to replace the elements in the original array, then, the system searches for elements in the Sor [] array in two parts. The last [] Array records the position where the first element appeared.

Offline processing: sorts queries by right boundary from small to large, processing intervals [1, R1], [R1 + 1, R2], [r2 + 1, r3]... delete the single point update in the previous online segment tree (recorded in last) of the repeated elements, and record the query (Li, RI) value to the corresponding position. At the same time, the number of queries recorded during reading is used for subsequent output.

Complexity: O (nlogn)

AC code:

1 # include <iostream> 2 # include <cstdio> 3 # include <vector> 4 # include <cstring> 5 # include <algorithm> 6 using namespace STD; 7 # define lson L, M, RT <1 8 # define rson m + 1, R, RT <1 | 1 9 int n, m; 10 struct node 11 {12 int l, R; 13 int num; 14 bool operator <(const node that) const {15 return r <that. r; 16} 17}; 18 bool uni (int I, Int J) 19 {20 return I = J; 21} 22 vector <node> BRR; 23 ve Ctor <int> sor; 24 vector <int> arr; 25 long Sgt [1200080]; 26 int last [30005]; 27 long ans [300005]; 28 void input () 29 {30 Brr. clear (); sor. clear (); arr. clear (); 31 memset (last,-1, sizeof (last); 32 scanf ("% d", & N); 33 for (INT I = 0; I <n; I ++) {34 int A; scanf ("% d", & A); 35 arr. push_back (a); 36 sor. push_back (a); 37} 38 sort (SOR. begin (), sor. end (); 39 vector <int>: iterator it; 40 it = Unique (SOR. begin (), sor. end (), Uni); 41 sor. resize (distance (SOR. begin (), It); // discretization 42 scanf ("% d", & M); 43 for (INT I = 0; I <m; I ++) {44 node X; scanf ("% d", & X. l, & X. r); 45 x. num = I; // record the query Order 46 Brr. push_back (x); 47} 48 sort (Brr. begin (), brr. end (); 49} 50 void push_up (int rt) 51 {52 Sgt [RT] = Sgt [RT <1] + Sgt [RT <1 | 1]; 53} 54 void build (int l, int R, int RT) 55 {56 If (L = r) {57 s GT [RT] = arr L-1]; 58 return; 59} 60 int M = (L + r)> 1; 61 build (lson); 62 build (rson ); 63 push_up (RT); 64} 65 void Delete (int l, int R, int RT, int POS) 66 {67 If (L = r) {68 Sgt [RT] = 0; return; 69} 70 int M = (L + r)> 1; 71 If (Pos <= m) Delete (lson, pos); 72 else Delete (rson, POS); 73 push_up (RT); 74} 75 long query (int l, int R, int RT, int L, int R) 76 {77 If (L <= L & R <= r) {78 Return Sgt [RT]; 79} 80 long res = 0; 81 int M = (L + r)> 1; 82 If (L <= m) res + = query (lson, L, R); 83 If (M <r) RES + = query (rson, L, R); 84 return res; 85} 86 87 void work () 88 {89 build (1, n, 1); 90 int L, R; 91 for (INT I = 0; I <m; I ++) {92 r = BRR [I]. r-1; 93 if (I = 0) L = 0; 94 else l = BRR [I-1]. r; 95 for (Int J = L; j <= r; j ++) {96 int Pos = lower_bound (SOR. begin (), sor. end (), arr [J])-sor. begin (); // POS corresponds to the element subscript 97 After discretization if (last [POS] =-1) {// There are no duplicates before 98 last [POS] = J; continue; 99} 100 Delete (1, n, 1, last [POS] + 1); last [POS] = J; // Delete duplicate elements, change last to the current position of the element. 101} 102 ans [BRR [I]. num] = query (1, n, 1, BRR [I]. l, BRR [I]. r); // enter the result in the query Order 103} 104 for (INT I = 0; I <m; I ++) {105 printf ("% i64d \ n", ANS [I]); 106} 107} 108 int main () 109 {110 int t; CIN> T; 111 while (t --) {112 input (); 113 work (); 114} 115 return 0; 116}
View code

 

Hdu3333-Turing tree-line segment tree + offline + discretization

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.