CF 351D, Off-line processing + tree-like ARRAY/MO team algorithm

Source: Internet
Author: User

Decadent Decadent Decadent

The main idea: to ask you a m-interval, ask how many different numbers in the interval, and the existence of a number in a number column is a series of linear interval.

Solution: offline inquiry, Not the same number is actually the old practice, but the clever is whether the number is a linear interval. We put the query according to the right interval, we can see that the distance interval is necessarily a continuous paragraph, then from the current enumeration point to the left, the legal sequence must be a continuous paragraph, then we use the tree array +1-1 maintenance interval to extend to where, and then asked [l,i] and whether zero is to know that there is no consistent sequence of linear interval.

By the way this is the official solution is Mo team, just I also go to study Mo team, now first paste an offline Nlogn practice, recursive learn Mo team later to Add.

1#include <cstdio>2#include <string>3#include <iostream>4#include <algorithm>5#include <cmath>6#include <cstring>7#include <complex>8#include <Set>9#include <vector>Ten#include <map> one#include <queue> a#include <deque> -#include <ctime> -  the using namespacestd; -  - Const DoubleEPS = 1e-8; -  + #defineABS (x) ((x) <0? ( -(x)):(x)) - #defineSQR (x) ((x) * (x)) + #defineMIN (a) < (b) ( a):(b)) a #defineMAX (a) (a) > (b)? ( a):(b)) at  - #defineLson (x) ((x) <<1) - #defineRson (x) (((x) <<1) +1) - #defineLowbit (x) ((x) & (-(x))) - #defineMaxs 1111 - #defineMAXN 222222 in #defineVoidpoint 0 - #defineLL Long Long to #defineOO 214748364 + #defineINF 0x3f3f3f3f - #defineMP (x, y) Make_pair (x, y) the  * structtreearray{ $     inttree[maxn], n;Panax Notoginseng     voidClearintnn = maxn-Ten) { -n =nn; theMemset (tree,0,sizeof(tree[0]) * (n+Ten)); +     } a     voidAddintXintnum =1) { the         if(x <1)return ; +          while(x <=N) { -tree[x] + =num; $X + =lowbit (x); $         } -     } -     int Get(intX) { the         intres =0; -          while(x >0) {WuyiRes + =tree[x]; thex-=lowbit (x); -         } wu         returnres; -     } about  $ } color, sl; -  - structquery{ -     intl, r, id; aQueryintA =0,intb =0,intc =0): l (a), r (b), ID (c) {} +     BOOL operator< (ConstQuery &rhs)Const { the         returnR <rhs.r; -     } $ } q[maxn]; the  the inta[maxn], n, m, ans[maxn]; the intpre[maxn], f[maxn], last[maxn]; the  - intmain () { in //freopen ("test.txt", "r", stdin); the  the  about color.clear (); sl.clear (); the //memset (pre,-1, sizeof (pre)); theMemset (last,-1,sizeof(last)); the  +scanf"%d", &n); -      for(inti =1; I <= n; ++I) { the //cout << i << endl;Bayiscanf"%d", &a[i]); thepre[i] =last[a[i]]; the         if(pre[i]! =-1) { -             if(pre[pre[i]]! =-1) { -                 if(i-pre[i] = = Pre[i]-pre[pre[i]]) f[i] =f[pre[i]]; the                 Elsef[i] =pre[i]; the}Else thef[i] =f[pre[i]]; the}Else  -f[i] =-1; thelast[a[i]] =i; the     } thescanf"%d", &m);94      for(inti =0; I < m; ++I) { the         intx, y; scanf"%d%d", &x, &y); theq[i] =query (x, y, i); the     }98Sort (q, q+m); about  - 101      for(inti =1, j =0; I <= n; ++I) {102 Color.add (i);103         if(pre[i]! =-1) Color.add (pre[i],-1);104  the         if(pre[i]! =-1) {106             if(f[i] = =f[pre[i]]) {107Sl.add (pre[i],-1); Sl.add (i);108}Else {109Sl.add (f[pre[i]] = =-1? -1: pre[f[pre[i]],1); Sl.add (pre[i],-1); theSl.add (pre[pre[i]),-1); Sl.add (i,1);111             } the}Else {113 Sl.add (i); the         } the  the          while(j < m && Q[J].R = =I) {117 //cout << i << ' << q[j].l << ' << q[j].r << endl; 118             119 //cout << (sl.get (q[j].r)-sl.get (q[j].l-1)) << endl; - //cout << color.get (q[j].r) << "<< color.get (q[j].l-1) << endl;121 122ans[q[j].id] = Color.Get(q[j].r)-color.Get(q[j].l-1) + (sl.Get(q[j].r)-sl.Get(q[j].l-1) ==0);123++j;124             if(i = =- to) { thecout << i <<endl;126                  for(intK =1; K <= i; ++k) cout << Color.Get(k) <<' '; cout <<endl;127                  for(intK =1; K <= i; ++k) cout << a[k] <<' '; cout <<endl; -                  for(intK =1; K <= i; ++k) cout << sl.Get(k)-sl.Get(k-1) <<' '; cout <<endl;129             } the         }131     }  the 133      for(inti =0; I < m; ++i) printf ("%d\n", ans[i]);134 135     return 0;136}
CF 351D

CF 351D, Off-line processing + tree-like ARRAY/MO team algorithm

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.