ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

P1494 小Z的袜子

2020-12-13 08:02:41  阅读:191  来源: 互联网

标签:ch P1494 袜子 -- add while include id


由于过于懒惰,就不写详细题解了,反正是给自己看的。

#include <cstdio>
#include <iostream>
#include <cmath> 
#include <algorithm>
#define orz cout<<"AK IOI"
#define int long long

using namespace std;
const int maxx = 50010;

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
	return x * f;
}
int n, m, a[maxx];//a为袜子的颜色 
int qn, l = 0, r = -1, ans, cs[maxx];//初始保证l r之间为空 
int dafz[maxx], dafm[maxx];
struct node{
	int l, r, id, k;
}q[maxx];
bool cmp(node a, node b)
{
	if(a.k < b.k || (a.k == b.k && a.r < b.r)) return 1;
	return 0;
}
int c(int x)
{
	if(x < 2) return 0;
	return x * (x - 1) / 2;  
}
void delet(int x)
{
    int tp = --cs[a[x]];
    ans -= c(tp + 1);
    ans += c(tp);
}
void add(int x)
{
    int tp = ++cs[a[x]];
    ans -= c(tp - 1);
    ans += c(tp);
}
int gcd(int a,int b)
{
	if(b % a == 0) return a;
    return gcd(b, a%b);
}
signed main()
{
	//freopen("P1494_7.in","r",stdin);
	//freopen("P1494.out","w",stdout); 
	n = read(); m = read();
	for(int i = 1; i <= n; i++)
	a[i] = read();
	qn = sqrt(n + 0.5);//每块的大小 和 一共有多少块
	for(int i = 1; i <= m; i++)
	{
		q[i].l = read(); q[i].r = read();
		q[i].id = i;
		q[i].k = q[i].l / qn;//在第几块 
	}
	sort(q + 1, q + m + 1, cmp);//对查询进行排序 
	for(int i = 1; i <= m; i++)
	{
		while(l < q[i].l)
		{
			delet(l);//因为当前的l值不包含在要查询的区间内,先删再减 
			l++;
		}
		while(l > q[i].l)
		{
		    l--;//因为当前的l值包含在要查询的区间内,先加再加上当前 
			add(l);
		}
		while(r < q[i].r)
		{
		    r++;
			add(r);
		}
		while(r > q[i].r)
		{
		    delet(r);
			r--;
		}
		dafz[q[i].id] = ans;                 
		dafm[q[i].id] = c(r - l + 1);
	} 
	for(int i = 1; i <= m; i++)                 
	{
		if(dafz[i] == 0)
		{
			printf("0/1\n");
			continue;
		}
		int tp = gcd(dafz[i], dafm[i]);
		printf("%lld/%lld\n",dafz[i]/tp, dafm[i]/tp);
	}
	return 0;
}

标签:ch,P1494,袜子,--,add,while,include,id
来源: https://www.cnblogs.com/yangchengcheng/p/14127069.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有