Xiao Ming (Nanyang oj51) (marking array), Nanyang oj51
Nosy James time limit: 4000 MS | memory limit: 65535 KB difficulty: 2
-
Description
-
There is a row of trees on the road with a length of L outside the gate of a school. The interval between every two adjacent trees is 1 meter. We can regard the road as a number axis, where one end of the road is 0 and the other is L. Each integer point on the number axis is 0, 1, 2 ,......, L. There is a tree.
There are some areas on the road to build a subway. These regions are expressed by their starting and ending points on the number axis. It is known that the coordinates of the starting and ending points of any region are integers, and the areas may overlap. Now we need to remove the trees (including the two trees at the region endpoint) in these regions. Smart James has done one thing after another. He thinks this is very interesting. He wants to use a computer to help construction workers count these trees. Now the task is coming, calculate the number of trees on the road after these trees are removed.
-
Input
-
The first row is an integer N indicating that N groups of test data exist (1 <= N <= 100)
The first line of each group of test data has two integers: L (1 <= L <= 10000) and M (1 <= M <= 100). L represents the length of the road, M indicates the number of regions. L and M are separated by a space. Each row of the next M line contains two different integers separated by a space, representing the coordinates of the start and end points of a region.
-
Output
-
The output includes a row. This row contains only one integer, indicating the number of remaining trees on the road.
-
Sample Input
-
1500 3150 300100 200470 471
-
Sample output
-
298
-
Source
-
Classic
-
Uploaded
Hzyqazasdf
#include<stdio.h>#include<string.h>int a[10002];int main(){int test,i,m,n,ans,t,k;scanf("%d",&test);while(test--){scanf("%d %d",&m,&n);memset(a,0,sizeof(a));while(n--){scanf("%d%d",&t,&k);for(i=t;i<=k;i++) a[i]=1;}for(i=0,ans=0;i<=m;i++){if(!a[i]) ans++; }printf("%d\n",ans);}return 0;}
-
/* This method is more common and can solve the big data problem. If it is marked with a decisive timeout. This question must start with I = 0, ans = 0, and if the value is ans = data [0]. j-data [0]. k + 1, I = 1 starts the cycle, and it will continue WA. N> = 1. The data is really weak. I am also drunk... */# Include <stdio. h> # include <algorithm> # include <string. h> using namespace std; struct st {int k; int j;} data [10010]; int cmp (st a, st B) {if (. k! = B. k) return. k <B. k; return. j <B. j;} int main () {int I, m, n, test, ans, t; scanf ("% d", & test); while (test --) {scanf ("% d", & m, & n); ans = 0; for (I = 0; I <n; I ++) scanf ("% d", & data [I]. k, & data [I]. j); sort (data, data + n, cmp); for (I = 0; I <n; I ++) {if (I = 0) {ans + = data [0]. j-data [0]. k + 1; t = data [0]. j; continue;} if (data [I]. k> t) {ans + = data [I]. j-data [I]. k + 1; t = data [I]. j;} elseif (data [I]. j> t & data [I]. k <= t) {ans = ans + data [I]. j-t; t = data [I]. j ;}} printf ("% d \ n", m-ans + 1);} return 0 ;}