CUGB 테마 트 레이 닝 데이터 구조: B - Count Color 선분 트 리 구간 업데이트

4307 단어
B - Count Color
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit 
Status
Description
Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. 
There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board: 
1. "C A B C" Color the board from segment A to segment B with color C. 
2. "P A B" Output the number of different colors painted between segment A and segment B (including). 
In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your. 
Input
First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.
Output
Ouput results of the output operation in order, each line contains a number.
Sample Input
2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2

Sample Output
2
1

이 문 제 는 정말...................................................................................데이터 가 약 해 졌 습 니 다!만약 배열 이 비교적 강하 다 면, 나의 이런 방법 은 시간 을 초과 할 수 있다.
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d
",a) #define f(i,a,n) for(i=a;i<n;i++) #define F(i,a,n) for(i=a;i<=n;i++) #define MM 200005 #define MN 505 #define INF 10000007 using namespace std; typedef long long ll; char s[2]; int a,b,c,vis[31]; struct node { int l,r,lazy,mid; } tree[MM*4]; void build(int i,int l,int r) { tree[i].l=l; tree[i].r=r; tree[i].lazy=1; //lazy , 0 tree[i].mid=(l+r)>>1; // , if(l==r) return; build(i<<1,l,tree[i].mid); build(i<<1|1,tree[i].mid+1,r); } void insert(int i,int l,int r,int lazy) { if(tree[i].l==l&&tree[i].r==r) { tree[i].lazy=lazy; return ; } if(tree[i].lazy==lazy) return ; if(tree[i].lazy) { tree[i<<1].lazy=tree[i<<1|1].lazy=tree[i].lazy; tree[i].lazy=0; } if(l>tree[i].mid) insert(i<<1|1,l,r,lazy); else if(r<=tree[i].mid) insert(i<<1,l,r,lazy); else { insert(i<<1,l,tree[i].mid,lazy); insert(i<<1|1,tree[i].mid+1,r,lazy); } } void query(int i,int l,int r) { if(tree[i].lazy) { vis[tree[i].lazy]=1; // , return ; } if(l>tree[i].mid) query(i<<1|1,l,r); else if(r<=tree[i].mid) query(i<<1,l,r); else { query(i<<1,l,tree[i].mid); query(i<<1|1,tree[i].mid+1,r); } } int main() { int l,t,q; scanf("%d%d%d",&l,&t,&q); build(1,1,l); while(q--) { scanf("%s%d%d",s,&a,&b); if(s[0]=='C') { scanf("%d",&c); insert(1,a,b,c); } else { int sum=0; mem(vis,0); query(1,a,b); for(int i=1;i<=t;i++) if(vis[i]) sum++; // +1 pri(sum); } } return 0; }

좋은 웹페이지 즐겨찾기