Time limit:80 Sec Memory limit:512 MB Description
A sequence A that is long n. Have M
A query, ask three intervals, delete the same number in the three interval, and ask the number of the last three intervals and ask for independence.
Note that this deletion refers to a deletion, not the number equal to this value directly deleted, such as three interval is [1,2,2,3,3,3,3], [1,2,2,3,3,3,3] and [1,1,2,3,3], together threw out a 1, a 2, 2 3. Input
The first line of two digits represents N, M. The number of n in the second row represents A[i].
After M line, 6 numbers per line L1, R1, L2, R2, L3, R3 represent these three intervals. Output
For each query, output a number to represent the answer. Sample Input
5 2
1 2 2 3 3
1 2 2 3 3 4
1 5 1 5 1 5 Sample Output
3
0 HINT
N, M <= 100000, 1 <= a[i] <= 1000000000 Solution
For each enquiry, the answer we ask for is ∑i=13 (ri−li+1) −3∗∑i=1maxmin (cnt1[i],cnt2[i],cnt3[i]) ∑i = 1 3 (r i−l i + 1) −3∗∑i = 1 m A x m i n (c n T 1 [i], C n T 2 [i], C n T 3 [i]) \sum^3_{i=1} (r_i-l_i+1) -3*\sum^{max}_{i=1}min (cnt_1[i],cnt_2[i],cnt _3[i]) The first half is easy, and the difficulty is in the latter part of the calculation. A[i] A [i] a[i] can be very large, so the a[i] a [i] a[i] should be discretized.
It is a classic problem to find the number of numbers appearing in a certain interval in the MO-team algorithm. for the subject, we take the three interval of a query to solve, and finally merged. Each merge is the complexity of O (n) o (n) o (n), which is not excellent, we use Bitset b i t s e T bitset optimization, recording CNT C N T cnt. Note that numbers may recur, and Bitset b i t s e t Bitset can only record 0 and 1, so there is no way to redo the process before discretization. For example, the sample data: 1 2 2 3 3, after discretization should be: 1 2 2 4 4, in the Bitset b i t s e t bitset in the 2nd digit represents the 1th 2, the 3rd bit represents the second 2, and so on.
But this will mle, so we forced the query group, each 25,000 for a group, batch solution, solve the problem of insufficient space.
Code
#include <stdio.h> #include <algorithm> #include <iostream> #include <cstdlib> #include <
cstring> #include <bitset> #include <cmath> using namespace std;
const int maxn=1e5+5;
const int t=25000;
int N,M,L,R,TOT,SIZ,A[MAXN],B[MAXN],BL[MAXN],ANS[MAXN],CNT[MAXN];
int L1[MAXN],R1[MAXN],L2[MAXN],R2[MAXN],L3[MAXN],R3[MAXN];
BOOL Flag[t+5];
Bitset<maxn> f[t+5],temp;
struct node {int id,tl,tr;
Node () {} node (int id,int tl,int TR): ID (ID), TL (TL), tr (tr) {}}Q[MAXN];
BOOL CMP (node X,node y) {return bl[x.tl]==bl[y.tl]?x.tr<y.tr:bl[x.tl]<bl[y.tl];} void modify (int x,int y) {
X=A[X];
if (y<0) temp[x+cnt[x]-1]=0;
if (y>0) temp[x+cnt[x]]=1;
Cnt[x]+=y;
} void Solve (int x,int y) {memset (cnt,0,sizeof (CNT)), Tot=0,temp.reset ();
memset (flag,false,sizeof (flag)), l=1,r=0;
for (int i=x;i<=y;i++) {q[++tot]=node (I,l1[i],r1[i]), ans[i]+=r1[i]-l1[i]+1; Q[++tot]=node (I,l2[i],r2[i]), ans[i]+=r2[i]-l2[i]+1; Q[++tot]=node (i,l3[i],r