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;}}}}