https://nanti.jisuanke.com/t/41399
Subject to the effect:
Lamp has n, m operations, every change in the lamp [l, r], (off - on, on - off), there are several lights lit last asked.
Stated differently: n points m intervals, each time interval the number of + 1, the last n points count is odd number of points is the answer.
At first I did not pay attention, to write directly with tree line, a super memory. . . .
This problem because the outer layer has a T, and n is too large, but also the memory card, too much.
Card data memory card, a heavy cycle each sample is timed-out. It is possible to block and to do the processing of m.
M to deal with the case, think about it, only to be operating range of the number of odd, then, this interval operation is valid, so we just left the endpoint range from small to large sums and then on the line.
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <set> 11 #include <map> 12 #include <math.h> 13 const int INF=0x3f3f3f3f; 14 typedef long long LL; 15 const int mod=1e9+7; 16 const int maxn=1e7+10; 17 using namespace std; 18 int a[4010]; 19 20 int main() 21 { 22 int T; 23 scanf("%d",&T); 24 for(int k=1;k<=T;k++) 25 { 26 int n,m; 27 int l,r; 28 scanf("%d %d",&n,&m); 29 for(int i=0;i<2*m;i++) 30 { 31 scanf("%d %d",&l,&r); 32 a[i++]=l; 33 a[i]=++r; 34 } 35 m*=2; 36 int ans=0; 37 sort(a,a+m); 38 for(int i=0;i<m;i++) 39 { 40 printf("%d ",a[i]); 41 } 42 for(int i=1;i<m;i+=2) 43 { 44 ans+=a[i]-a[i-1]; 45 } 46 printf("Case #%d: %d\n",k,ans); 47 } 48 return 0; 49 }
M to start, notes that:
After each endpoint save up to (and ordering), the interval between the bright points every two absence As regards the left, the right is not included.
Therefore, the use of differential thinking, modifications L, R corresponds to a (L) + 1, a (R + 1) -1
After the end we sorted, directly run again m
If the current point is left is covered by layers ++ (after each number means that are more of a zone coverage)
If the current is then covered with layers right point - (that is, after each number means one less coverage interval)
Opening and closing the left and right sections as long as it is an odd number (lights), it adds the value RL of the left and right open interval
#include <stdio.h> #include <string.h> #include <iostream> #include <string> #include <math.h> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <math.h> const int INF=0x3f3f3f3f; typedef long long LL; const int mod=1e9+7; const int maxn=1e7+10; using namespace std; struct node { int f; int id; bool friend operator < (node a,node b) { if(a.id==b.id) return a.f<b.f; return a.id<b.id; } }pos[2010]; int main() { int T; scanf("%d",&T); for(int k=1;k<=T;k++) { int n,m; int l,r; int cnt=0; scanf("%d %d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d %d",&l,&r); pos[++cnt].id=l; pos[cnt].f=0; pos[++cnt].id=r+1; pos[cnt].f=1; } int cot=0; int ans=0; sort(pos+1,pos+1+cnt); for(int i=1;i<=cnt-1;i++) { if(pos[i].f) cot--; else cot++; if(cot%2) ans+=pos[i+1].id-pos[i].id; } printf("Case #%d: %d\n",k,ans); } return 0; }