[Codevs] 1080 line segment tree Exercise

Source: Internet
Author: User

portal:http://codevs.cn/problem/1080/


The question was a little waking up to me: The tree array is not the same as the line tree (half an hour ago the concept of the intellect is silly).

The main idea is to use a tree-like array to store the prefix and then prefix and subtract to get the interval and.

The knowledge of the line segment tree appeared in the Blue Book,,, inexplicably from the Purple book upgrade.


Mark I read that blog: http://blog.csdn.net/dcrusher/article/details/49497395

(This is also very powerful, in three ways)


#include <cstdio>
#include <iostream>
using namespace std;

int tree[999999],n,m;

int lowbit (int x)
{
	return x& (-X);
}

int add (int pos,int v)
{
	int J;
	for (j = pos;j <= n;j + = Lowbit (j))
	{
		Tree[j] + = v;
	}
	return 0;
}

int sch (int pos)
{
	int ans = 0,j;
	for (j = pos;j >= 1;j-= Lowbit (j))
		ans + = tree[j];
		
	return ans;
}

int main ()
{
	int a,b,c;
	scanf ("%d", &n);
	for (int i = 1;i <= n;i++)
	{
		scanf ("%d", &a);
		Add (i,a);
	}
	
	scanf ("%d", &m);
	for (int i = 0;i < m;i++)
	{
		scanf ("%d%d%d", &a,&b,&c);
		Switch (a)
		{case
			1:
				Add (b,c);
				break;
			Case 2:
				int s1,s2;
				S1 = Sch (b-1);
				S2 = Sch (c);
				printf ("%d\n", s2-s1);
				break;
		}
	}
	return 0;
}


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.