An incomplete line segment tree of the HDU-1166 enemy army

Source: Internet
Author: User

This pseudo-line segment tree took an afternoon. Why is it a pseudo-line segment tree? Because this question does not actually update a certain domain, it is just a specific point, so when the program is looking for it, both the node to be updated and the path to be updated are unified "+ = Update". Come on, write a question this evening, write a full version. The summary will be written one by one in the next full version.

The Code is as follows:

#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <queue>#include <algorithm>using namespace std;int N;union Mix{int sum;int pos;};struct Node{int left, right;Mix x;}e[200005], info;void creat(  ){queue< Node >q;e[1].left= 1, e[1].right= N+ 1;info.left= 1, info.right= N+ 1, info.x.pos= 1; q.push( info ); while( !q.empty() ){Node obj= q.front();q.pop();if( obj.right- obj.left== 1 ){    continue;}int lch= obj.x.pos<< 1, rch= ( obj.x.pos<< 1 )+ 1; e[lch].left= obj.left, e[lch].right= ( obj.left+ obj.right )>> 1;if( e[lch].right- e[lch].left> 1 ){info.left= e[lch].left, info.right= e[lch].right, info.x.pos= lch;q.push( info );}e[rch].left= e[lch].right, e[rch].right= obj.right;if( e[rch].right- e[rch].left> 1 ){info.left= e[rch].left, info.right= e[rch].right, info.x.pos= rch;q.push( info );}}}int getsum( int l, int r ){int sum= 0;queue< Node >q;info.left= l, info.right= r+ 1, info.x.pos= 1;q.push( info );while( !q.empty() ){Node obj= q.front();q.pop(); if( obj.left== e[obj.x.pos].left&& obj.right== e[obj.x.pos].right ){ sum+= e[obj.x.pos].x.sum;}else{int mid= ( e[obj.x.pos].left+ e[obj.x.pos].right )>> 1;if( obj.left>= mid ){info.left= obj.left, info.right= obj.right, info.x.pos= ( obj.x.pos<< 1 )+ 1;q.push( info );}else if( obj.right<= mid ){info.left= obj.left, info.right= obj.right, info.x.pos= obj.x.pos<< 1;q.push( info );}else{info.left= obj.left, info.right= mid, info.x.pos= obj.x.pos<< 1;q.push( info );info.left= mid, info.right= obj.right, info.x.pos= ( obj.x.pos<< 1 )+ 1; q.push( info );}}}return sum;}void add( int pos, int num ){bool finish= false;int i= 1;while( !finish ){if( e[i].left== pos&& e[i].right== pos+ 1 ){e[i].x.sum+= num;finish= true;break;}e[i].x.sum+= num;if( pos>= ( ( e[i].left+ e[i].right )>> 1 ) ){i= ( i<< 1 )+ 1;}else{i= i<< 1;}}}int main(){int T;char op[10];scanf( "%d", &T );for( int t= 1; t<= T; ++t ){scanf( "%d", &N );memset( e, 0, sizeof( e ) );creat();for( int i= 1; i<= N; ++i ){int c;scanf( "%d", &c );add( i, c );}printf( "Case %d:\n", t );while( scanf( "%s", op ), op[0]!= 'E' ){int pos, num, l, r;switch( op[0] ){case 'Q': scanf( "%d %d", &l, &r );printf( "%d\n", getsum( l, r ) );break;case 'S':scanf( "%d %d", &pos, &num );add( pos, -num );break;case 'A':scanf( "%d %d", &pos, &num );add( pos, num );break;}}}} 

  

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.