您的位置:首页 > 其它

【lightoj - 1183 - Computing Fast Average(如此水题,竟然也会错?!)】

2012-08-09 17:02 344 查看
1183 - Computing Fast Average

PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 64 MB
Given an array of integers (0 indexed), you have to perform two types of queries in the array.

1 i j v - change the value of the elements from ith index to jth index to v.

2 i j - find the average value of the integers from ith index to jth index.

You can assume that initially all the values in the array are 0.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case contains two integers: n (1 ≤ n ≤ 105), q (1 ≤ q ≤ 50000), where n denotes the size of the array. Each of the next q lines will contain a query of the form:

1 i j v (0 ≤ i ≤ j < n, 0 ≤ v ≤ 10000)

2 i j (0 ≤ i ≤ j < n)

Output

For each case, print the case number first. Then for each query of the form '2 i j' print the average value of the integers from i to j. If the result is an integer, print it. Otherwise print the result in 'x/y' form, where x denotes the numerator and y denotes the denominator of the result and x and y are relatively prime.

Sample Input

Output for Sample Input

1

10 6

1 0 6 6

2 0 1

1 1 1 2

2 0 5

1 0 3 7

2 0 1

Case 1:

6

16/3

7

Note

Dataset is huge. Use faster i/o methods.

PROBLEM SETTER: JANE ALAM JAN

先贴错误代码吧。

坑爹啊。。。。。这可是线段树水题啊。。

// Project name : 1183 ( Computing Fast Average )
// File name    : main.cpp
// Author       : iCoding
// E-mail       : honi.linux@gmail.com
// Date & Time  : Thu Aug  9 15:38:43 2012

#include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;

/*************************************************************************************/
/* data */

#ifndef MAXN
#define MAXN 100100
#endif

#ifndef CHANGE
#define CHANGE 1
#endif

#ifndef QUERY
#define QUERY 2
#endif

struct Node
{
int left;
int right;
int value;
};

Node iMap[MAXN*4];

int n, k;

/*************************************************************************************/
/* procedure */

void debug()
{
printf("--debug msg--\n");
}
void iCreatMap(int iStart, int iEnd, int iID)
{
iMap[iID].left  = iStart;
iMap[iID].right = iEnd;
iMap[iID].value = 1;

if (iStart == iEnd)
{
return;
}
else
{
int iMid = (iStart + iEnd) / 2;
iCreatMap(iStart, iMid, iID*2);
iCreatMap(iMid+1, iEnd, iID*2+1);
}
}

void iChangeValue(int iStart, int iEnd, int iID, int iValue)
{
if (iMap[iID].left == iStart && iMap[iID].right == iEnd)
{
iMap[iID].value = iValue;
}
else
{
if (iMap[iID].value != -1)
{
iMap[iID*2]  .value = iMap[iID].value;
iMap[iID*2+1].value = iMap[iID].value;
iMap[iID].value     = -1;
}
int iMid = (iMap[iID].left + iMap[iID].right) / 2;
if (iMid >= iEnd)
{
iChangeValue(iStart,   iEnd, iID * 2,     iValue);
}
else if (iMid + 1 <= iStart)
{
iChangeValue(iStart,   iEnd, iID * 2 + 1, iValue);
}
else
{
iChangeValue(iStart,   iMid, iID * 2,     iValue);
iChangeValue(iMid + 1, iEnd, iID * 2 + 1, iValue);
}
}
}

int iQuery(int iStart, int iEnd, int iID)
{
if (iMap[iID].value != -1)
{
return (iEnd - iStart + 1) * iMap[iID].value;
}
else
{
int iMid = (iMap[iID].left + iMap[iID].right) / 2;
if (iMid >= iEnd)
{
return iQuery(iStart, iEnd, iID * 2);
}
else if (iMid + 1 <= iStart)
{
return iQuery(iStart, iEnd, iID * 2 + 1);
}
else
{
return iQuery(iStart, iMid, iID * 2) + iQuery(iMid + 1, iEnd, iID * 2 + 1);
}
}
}

int iGCD(int a, int b)
{
int tmp;
while (b)
{
tmp = a % b;
a   = b;
b   = tmp;
}
return a;
}

void iShowResult(int a, int b)
{
int gcd = iGCD(a, b);
a /= gcd;
b /= gcd;
if (b == 1)
{
printf("%d\n", a);
}
else
{
printf("%d/%d\n", a, b);
}
}
/*************************************************************************************/
/* main */
int main()
{
int iT;
scanf("%d", &iT);

for (int iCaseCount = 1; iCaseCount <= iT; iCaseCount++)
{
printf("Case %d:\n", iCaseCount);
scanf("%d%d", &n, &k);
iCreatMap(0, n-1, 1);

while (k--)
{
int iInstru;
scanf("%d", &iInstru);
if (iInstru == CHANGE)
{
int iStart;
int iEnd;
int iValue;
scanf("%d%d%d", &iStart, &iEnd, &iValue);
iChangeValue(iStart, iEnd, 1, iValue);
}
if (iInstru == QUERY)
{
int iStart;
int iEnd;
scanf("%d%d", &iStart, &iEnd);
int iSum = iQuery(iStart, iEnd, 1);

// undone
//printf("%d\n", iSum);
iShowResult(iSum, iEnd - iStart + 1);
}
}

}
return 0;
}

// end
// Code by Sublime text 2
// iCoding@CodeLab


啊,,罪过罪过。。我把和之前做的一个题目给混淆起来了。。。这里默认初始值是0 啊。。而以前做的是1,这么不小心。。。害我还不知道哪里错了。。。又浪费了不少时间啊。。。

贴个代码,认个错吧。

// Project name : 1183 ( Computing Fast Average )
// File name    : main.cpp
// Author       : iCoding
// E-mail       : honi.linux@gmail.com
// Date & Time  : Thu Aug  9 15:38:43 2012

#include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;

/*************************************************************************************/
/* data */

#ifndef MAXN
#define MAXN 100100
#endif

#ifndef CHANGE
#define CHANGE 1
#endif

#ifndef QUERY
#define QUERY 2
#endif

struct Node
{
int left;
int right;
int value;
};

Node iMap[MAXN*4];

int n, k;

/*************************************************************************************/
/* procedure */

void debug()
{
printf("--debug msg--\n");
}
void iCreatMap(int iStart, int iEnd, int iID)
{
iMap[iID].left  = iStart;
iMap[iID].right = iEnd;
iMap[iID].value = 0;

if (iStart == iEnd)
{
return;
}
else
{
int iMid = (iStart + iEnd) / 2;
iCreatMap(iStart, iMid, iID*2);
iCreatMap(iMid+1, iEnd, iID*2+1);
}
}

void iChangeValue(int iStart, int iEnd, int iID, int iValue)
{
if (iMap[iID].left == iStart && iMap[iID].right == iEnd)
{
iMap[iID].value = iValue;
}
else
{
if (iMap[iID].value != -1)
{
iMap[iID*2]  .value = iMap[iID].value;
iMap[iID*2+1].value = iMap[iID].value;
iMap[iID].value     = -1;
}
int iMid = (iMap[iID].left + iMap[iID].right) / 2;
if (iMid >= iEnd)
{
iChangeValue(iStart,   iEnd, iID * 2,     iValue);
}
else if (iMid + 1 <= iStart)
{
iChangeValue(iStart,   iEnd, iID * 2 + 1, iValue);
}
else
{
iChangeValue(iStart,   iMid, iID * 2,     iValue);
iChangeValue(iMid + 1, iEnd, iID * 2 + 1, iValue);
}
}
}

int iQuery(int iStart, int iEnd, int iID)
{
if (iMap[iID].value != -1)
{
return (iEnd - iStart + 1) * iMap[iID].value;
}
else
{
int iMid = (iMap[iID].left + iMap[iID].right) / 2;
if (iMid >= iEnd)
{
return iQuery(iStart, iEnd, iID * 2);
}
else if (iMid + 1 <= iStart)
{
return iQuery(iStart, iEnd, iID * 2 + 1);
}
else
{
return iQuery(iStart, iMid, iID * 2) + iQuery(iMid + 1, iEnd, iID * 2 + 1);
}
}
}

int iGCD(int a, int b)
{
int tmp;
while (b)
{
tmp = a % b;
a   = b;
b   = tmp;
}
return a;
}

void iShowResult(int a, int b)
{
if (a % b == 0)
{
printf("%d\n", a / b);
}
else
{
int gcd = iGCD(a, b);
a /= gcd;
b /= gcd;
printf("%d/%d\n", a, b);
}
}
/*************************************************************************************/
/* main */
int main()
{
int iT;
scanf("%d", &iT);

for (int iCaseCount = 1; iCaseCount <= iT; iCaseCount++)
{
printf("Case %d:\n", iCaseCount);
scanf("%d%d", &n, &k);
iCreatMap(0, n-1, 1);

while (k--)
{
int iInstru;
scanf("%d", &iInstru);
if (iInstru == CHANGE)
{
int iStart;
int iEnd;
int iValue;
scanf("%d%d%d", &iStart, &iEnd, &iValue);
iChangeValue(iStart, iEnd, 1, iValue);
}
if (iInstru == QUERY)
{
int iStart;
int iEnd;
scanf("%d%d", &iStart, &iEnd);
int iSum = iQuery(iStart, iEnd, 1);

// undone
//printf("%d\n", iSum);
iShowResult(iSum, iEnd - iStart + 1);
}
}

}
return 0;
}

// end
// Code by Sublime text 2
// iCoding@CodeLab
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: