本来是以十作为例子,然后发现改成八千就全都错了
看来是整个思路都有问题。。
先贴上来
#include<stdio.h>
#include <queue>
#include <algorithm>
#include<string.h>
using namespace std;
const int m = 8000;
struct xtree
{
int a;
int b;
int c;
}t[m*3];
int color[m];
void refresh(int a, int b, int i)
{
t[i].a = a;
t[i].b = b;
t[i].c = -1;
if ((b - a) > 1)
{
refresh(a, (a + b) / 2, 2 * i);
refresh((a + b) / 2, b, 2 * i + 1);
}
}
void paint(int x1, int x2, int i, int c)
{
if (i > 8000||x1==x2)
return;
if (t[i].b==x2&&t[i].a==x1 )
{
t[i].c = c;
return;
}
else
t[i].c = -2;
int m = (t[i].a + t[i].b) / 2;
if (x2 <= m)
{
paint(x1, x2, 2 * i, c);
}
else if (x1 >= m)
{
paint(x1, x2, 2 * i + 1, c);
}
else if (x1 <= m&&x2 >= m)
{
if (t[2 * i].c > 0)
t[2 * i].c = -2;
if (t[2 * i+1].c > 0)
t[2 * i+1].c = -2;
paint(x1, m, 2 * i, c);
paint(m, x2, 2 * i + 1, c);
}
}
int sor()
{
queue<int> q;
int x = 0, y = 0,p=-1;
q.push(1);
while (!q.empty())
{
x = q.front();
q.pop();
if (t[x].c >= 0)
{if(y!=0) //作用相同
{
if (t[x].c != color[y - 1])
color[y++] = t[x].c;
}
else
color[y++] = t[x].c;
}
else
{
if (t[x * 2].c != -1)
q.push(x * 2);
if (t[x * 2+1].c != -1)
q.push(x * 2+1);
}
}
sort(color,color+y);
return y;
}
int main()
{
int n = 0, x1 = 0, x2 = 0, c = 0,i=0,j=1;
while (scanf_s("%d", &n) != EOF)
{
memset(color, -1, sizeof(int));
refresh(0,8000,1);
for (i = 0; i < n; i++)
{
scanf_s(" %d %d %d", &x1, &x2, &c);
paint(x1, x2, 1, c);
}
for (i = 0; i < sor(); i++)
{
while (color[i] == color[i + 1])
{
j++;
i++;
}
printf_s("%d %d\n", color[i], j);
j = 1;
}
printf_s("\n");
}
}
今天下午又重新看了一次
感觉 思路是对的
然而是WA
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAX 6000*3
int num[6000 * 3],t=1;
struct TreeNode
{
int x1, x2;
int c=-1;
int key;
}tree[MAX];
void creattree(int key, int a, int b) //生成线段树
{
tree[key].x1 = a;
tree[key].x2 = b;
if (b - a == 1)
return;
creattree(2 * key, a, (a + b) / 2);
creattree(2 * key+1, (a + b) / 2, b);
}
void print(int key,int a, int b, int c) //给线段树上色
{
if (a == b || tree[key].x2 - tree[key].x1 == 1)
{
tree[key].c = c;
return;
}
if (tree[key].c == -1|| tree[key].c>=0) //-1表示均为上色
tree[key].c = -2; //-2表示下面有被上色
if (b <= (tree[key].x1 + tree[key].x2) / 2)
print(2 * key, a, b, c);
else if(a>= (tree[key].x1 + tree[key].x2) / 2)
print(2 * key+1, a, b, c);
else
{
print(2 * key, a, (tree[key].x1 + tree[key].x2) / 2, c);
print(2 * key+1, (tree[key].x1 + tree[key].x2) / 2, b, c);
}
}
void DFS(int key)
{
if (tree[key].c == -1)
return;
else if (tree[key].c >= 0)
{
if (tree[key].x2 - tree[key].x1 == 1)
{
num[t] = tree[key].c;
if (num[t] == num[t - 1])
t--;
t++;
}
return ;
}
else
{
DFS(2 * key);
DFS(2 * key+1);
}
}
int main()
{
int n, i, j,nn=0;
int a, b, c;
creattree(1, 0, 8000);//8000
while (cin >> n)
{
memset(num, 0, sizeof(num));
for (int j=0; j < MAX ; j++)
tree[j].c = -1;
t = 1;num[0] = -3;
for (i = 1; i <= n; i++)
{
cin >> a >> b >> c;
print(1, a, b, c);
}
DFS(1);
sort(num + 1, num + t);
for (int j = 1; j < t; j++)
{
cout << num[j] << ends;
nn = j;
while (num[nn] == num[j]&&nn<t)
{
nn++;
}
cout << nn-j << endl;
j = nn-1;
}
cout << endl;
//int mm=9;
}
//system("pause");
return 0;
}