Cod sursa(job #1621253)

Utilizator qwertyuiTudor-Stefan Berbinschi qwertyui Data 29 februarie 2016 17:51:08
Problema Arbori de intervale Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin ("qxy.in");
ofstream fout ("qxy.out");

int tree[10050][1050], DP[10050][1050];
int N, M;

void update(int x , int y , int val)
{
    int y1;
    while (x <= N){
        y1 = y;
        while (y1 <= M){
            tree[x][y1] += val;
            y1 += (y1 & -y1);
        }
        x += (x & -x);
    }
}

int read(int x,int y)
{
           int sum = 0;
             while(x)
				{
                  int y1 = y;
                 while(y1){
                     sum += tree[x][y1];
                       y1 -= y1 & -y1;
                }
                x -= x & -x;
            }
          return sum;
}

int main()
{
    fin >>N;
    for (int i = 1; i <= N; ++i)
	{
        int x;
        fin >>x;
		M = max(M, x+1);
		tree[i][x] = 1;
	}
	for (int i = 1; i <= N; ++i)
		for (int j = 1; j <= 1000; ++j)
		{
			int line = 0, column = 0;
			for (int k = 0; k < j; ++k)
				line += tree[i][k];
			for (int k = 0; k < i; ++k)
				column += tree[k][j];
			DP[i][j] = DP[i-1][j-1] + tree[i][j] + line + column;
		}


	/*for (int i = 1; i <= N; ++i){
		for (int j = 1; j <= N; ++j)
			fout <<DP[i][j] <<' ';
			fout <<'\n';
	}*/
	int Q;
	fin >>Q;
	for (int i = 1; i <= Q; ++i)
	{
		int x,y,a,b;
		fin >>x >>y >>a >>b;
		fout <<DP[y][b] - DP[x-1][b] - DP[y][a-1] + DP[x-1][a];
		fout <<'\n';
	}

	//cout <<read(5,7) - read(1,4)+1;
    return 0;
}