Segment update and discretization line segment tree
This question is almost a day .... Various problems: I didn't add the queried nodes to the tree when I started to build the tree. The results showed that the information of many nodes was lost during the query. After a long time, I went to check other people's code, when building the original tree, we should add the queried nodes. When doing discretization, we should open the hash array to 10 ^ 9, which is out of memory. We can't compile it, compile it, and then change it to map, finally, I am ashamed of A. (The data in this question may be weak and can be passed without discretization)
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <cstdlib>
# Include <algorithm>
# Include <vector>
# Include <map>
Using namespace std;
# Define N 100005
Map <int, int> h;
Int x [3 * N]; // The node to be queried
Int ask [N];
Struct cam2
{
Int x;
Int y;
} Flower [N * 2];
Struct cam1
{
Int x;
Int y;
Int sum;
Int add;
} List [N * 4];
Void build (int k, int x, int y)
{
Int mid;
List [k]. add = 0;
List [k]. x = x;
List [k]. y = y;
List [k]. sum = 0;
If (list [k]. x = list [k]. y)
Return;
Mid = (x + y)/2;
Build (k <1, x, mid );
Build (k <1 | 1, mid + 1, y );
}
Void update (int k, int x, int y, int d)
{
Int mid;
If (list [k]. x = x & list [k]. y = y)
{
List [k]. sum + = (list [k]. y-list [k]. x + 1) * d;
List [k]. add + = d;
Return;
}
If (list [k]. add! = 0)
{
List [k <1]. sum + = (list [k <1]. y-list [k <1]. x + 1) * list [k]. add;
List [k <1 | 1]. sum + = (list [k <1 | 1]. y-list [k <1 | 1]. x + 1) * list [k]. add;
List [k <1]. add + = list [k]. add;
List [k <1 | 1]. add + = list [k]. add;
List [k]. add = 0;
}
Mid = (list [k]. x + list [k]. y)/2;
If (x> mid)
Update (k <1 | 1, x, y, d );
Else if (y <= mid)
Update (k <1, x, y, d );
Else
{
Update (k <1, x, mid, d );
Update (k <1 | 1, mid + 1, y, d );
}
List [k]. sum = list [k <1]. sum + list [k <1 | 1]. sum;
}
Int find (int k, int x, int y)
{
Int mid;
If (list [k]. x = x & list [k]. y = y)
Return list [k]. sum;
If (list [k]. add! = 0)
{
List [k <1]. sum + = (list [k <1]. y-list [k <1]. x + 1) * list [k]. add;
List [k <1 | 1]. sum + = (list [k <1 | 1]. y-list [k <1 | 1]. x + 1) * list [k]. add;
List [k <1]. add + = list [k]. add;
List [k <1 | 1]. add + = list [k]. add;
List [k]. add = 0;
}
Mid = (list [k]. x + list [k]. y)/2;
If (x> mid)
Return find (k <1 | 1, x, y );
Else if (y <= mid)
Return find (k <1, x, y );
Return find (k <1, x, mid) + find (k <1 | 1, mid + 1, y );
}
Int main ()
{
Int k, t, n, m, I, cnt, num;
Scanf ("% d", & t );
For (k = 1; k <= t; k ++)
{
Scanf ("% d", & n, & m );
Cnt = 0;
For (I = 0; I <n; I ++)
{
Scanf ("% d", & flower [I]. x, & flower [I]. y );
X [cnt ++] = flower [I]. x;
X [cnt ++] = flower [I]. y;
}
Printf ("Case # % d: \ n", k );
For (I = 0; I <m; I ++) // Insert the node to be queried when creating a tree. Otherwise, node information is lost during query.
{
Scanf ("% d", & ask [I]);
X [cnt ++] = ask [I];
}
Sort (x, x + cnt );
Cnt = unique (x, x + cnt)-x;
H. clear ();
For (I = 0; I <cnt; I ++) // reduce the building space
H [x [I] = I;
Build (, cnt-1 );
For (I = n-1; I> = 0; I --)
Update (1, h [flower [I]. x], h [flower [I]. y], 1 );
For (I = 0; I <m; I ++) www.2cto.com
Printf ("% d \ n", find (1, h [ask [I], h [ask [I]);
}
Return 0;
}
By Cambridgeacm