HDU - 1698:Just a Hook
22713 단어 [기록]알고리즘 문제 풀이
출처: HDU
태그: 데이터 구조, 선분 트 리, 구간 수정
참고 자료:
비슷 한 제목:
제목.
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. Now Pudge wants to do some operations on the hook. Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks. The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows: For each cupreous stick, the value is 1. For each silver stick, the value is 2. For each golden stick, the value is 3. Pudge wants to know the total value of the hook after performing the operations. You may consider the original hook is made up of cupreous sticks.
입력
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases. For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations. Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
출력
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
입력 샘플
1 10 2 1 5 2 5 9 3
출력 샘플
Case 1: The total value of the hook is 24.
참조 코드
#include
#include
#define MAXN 100005
using namespace std;
struct SegTreeNode{
int val;
int mark;
}segTree[MAXN*4];
void build(int root, int begin, int end){
segTree[root].mark=0;
if(begin==end){
segTree[root].val=1;
}
else{
int mid=(begin+end)/2;
build(root*2+1,begin,mid);
build(root*2+2,mid+1,end);
segTree[root].val=segTree[root*2+1].val+segTree[root*2+2].val;
}
}
void pushDown(int root,int begin,int end){
if(segTree[root].mark!=0){
segTree[root*2+1].mark=segTree[root].mark;
segTree[root*2+2].mark=segTree[root].mark;
segTree[root*2+1].val=((begin+end)/2-begin+1)*segTree[root].mark;
segTree[root*2+2].val=(end-(begin+end)/2)*segTree[root].mark;
segTree[root].mark=0;
}
}
int query(int root, int begin, int end, int L, int R){
if(L>end || R<begin) return 0;
if(L<=begin && R>=end) return segTree[root].val;
pushDown(root,begin,end);
int mid=(begin+end)/2;
return query(root*2+1, begin, mid, L, R)+query(root*2+2, mid+1, end, L, R);
}
void update(int root, int begin, int end, int L, int R, int val){
if(L>end || R<begin){
return;
}
if(L<=begin && R>=end){
segTree[root].mark=val;
segTree[root].val=(end-begin+1)*val;
return;
}
pushDown(root,begin,end);
int mid=(begin+end)/2;
update(root*2+1, begin, mid, L, R, val);
update(root*2+2, mid+1, end, L, R, val);
segTree[root].val=segTree[root*2+1].val+segTree[root*2+2].val;
}
int main(){
int T;
scanf("%d",&T);
for(int t=1;t<=T;t++){
int N,Q;
scanf("%d%d",&N,&Q);
build(0,0,N-1);
int a,b,c;
for(int i=0;i<Q;i++){
scanf("%d%d%d",&a,&b,&c);
update(0,0,N-1,a-1,b-1,c);
}
printf("Case %d: The total value of the hook is %d.
",t,query(0,0,N-1,0,N-1));
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
UVALive - 3135:ArgusLikewise, queries over streams run continuously over a period of time and incrementally return new results as new data a...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.