牛客练习赛28 A,B___枚举,毒瘤线段树

206 篇文章 0 订阅
35 篇文章 1 订阅

A.生日宴会:

链接:https://www.nowcoder.com/acm/contest/200/A
来源:牛客网
给出N个人的名字与出生年月日,
名字为10个字符以内的英文字母,
出生年月日为8位数字。
有M个询问,每个询问给出一个k与某一天s,输出生日在这天s的年龄第k大的人。
保证每一个朋友的出生年月日不同,且出生年月日均在19000101至19991231,保证一定有答案。
n &lt; = 1000 , m &lt; = 20 n&lt;=1000,m&lt;=20 n<=1000,m<=20

分析:

直接暴力即可。

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define N 1005

using namespace std;

struct Node { char na[N]; int x, y; }a[N];
int n, m;

bool cmp(Node aa, Node bb)
{
    if (aa.y == bb.y) return aa.x < bb.x;
	return aa.y < bb.y;	
}

int main()
{
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++)
	{
		 int x;
		 scanf("%s %d", a[i].na, &x);
		 a[i].x = x / 10000;
		 a[i].y = x % 10000;
	}
	sort(a + 1, a + n + 1, cmp);
	for (int i = 1; i <= m; i++)
	{
		 int k, s;
		 scanf("%d %d", &k, &s);
		 for (int j = 1; j <= n; j++)
		      if (a[j].y == s)
		      {
			      printf("%s\n", a[j + k - 1].na);
			      break;
			  }
	}
	return 0;	
}

B. 数据结构:

链接:https://www.nowcoder.com/acm/contest/200/B
来源:牛客网
给出一个长度为N的序列,有M个修改,每次修改允许4种操作,
1 l r 询问区间[l,r]内的元素和
2 l r 询问区间[l,r]内的元素的平方 和
3 l r x 将区间[l,r]内的每一个元素都乘上x
4 l r x 将区间[l,r]内的每一个元素都加上x

分析:

一个傻逼线段树题,
利用线段树处理区间和与区间平方和,
利用性质 ( a + b ) 2 = a 2 + 2 a b + b 2 (a+b)^2 = a^2+2ab+b^2 (a+b)2=a2+2ab+b2 ( a b ) 2 = a 2 b 2 (ab)^2=a^2b^2 (ab)2=a2b2可判断出区间平方和的变动情况,然后进行修改。
区间和的话比较容易了,
然后lazy有点烦,细心点吧,
考场打的心态炸了,lazy出了奇葩错误

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define N 100005

using namespace std;

typedef long long LL;

struct Node { LL lazy, mulzy, sum, mul;}tree[N];
LL a[10005];
int n, m;

void Build(int G, int l, int r)
{
	if (l == r) 
	{
	    tree[G].mulzy = 1, tree[G].sum = a[l], tree[G].mul = a[l] * a[l];
        return;
	}
	int mid = (l + r) >> 1;
	Build(G * 2, l, mid);
	Build(G * 2 + 1, mid + 1, r);
	tree[G].mulzy = 1;
	tree[G].sum = tree[G * 2].sum + tree[G * 2 + 1].sum;
	tree[G].mul = tree[G * 2].mul + tree[G * 2 + 1].mul;
}

void Updata(int p, int l, int r)
{
	  if (tree[p].lazy == 0 && tree[p].mulzy == 1) return;
	  int mid = (l + r) >> 1;
	  tree[p * 2].mul = tree[p * 2].mul * tree[p].mulzy * tree[p].mulzy + 2 * (tree[p].mulzy * tree[p].lazy) * tree[p * 2].sum + (mid - l + 1) * tree[p].lazy * tree[p].lazy;
      tree[p * 2 + 1].mul = tree[p * 2 + 1].mul * tree[p].mulzy * tree[p].mulzy + 2 * (tree[p].mulzy * tree[p].lazy) * tree[p * 2 + 1].sum + (r - mid) * tree[p].lazy * tree[p].lazy;
          
	  tree[p * 2].sum = tree[p * 2].sum * tree[p].mulzy + (mid - l + 1) * tree[p].lazy;
      tree[p * 2 + 1].sum = tree[p * 2 + 1].sum * tree[p].mulzy + (r - mid) * tree[p].lazy;
           
	  tree[p * 2].mulzy = tree[p * 2].mulzy * tree[p].mulzy;
      tree[p * 2 + 1].mulzy = tree[p * 2 + 1].mulzy * tree[p].mulzy;
          
      tree[p * 2].lazy = tree[p * 2].lazy * tree[p].mulzy + tree[p].lazy;
      tree[p * 2 + 1].lazy = tree[p * 2 + 1].lazy * tree[p].mulzy + tree[p].lazy;
          
	  tree[p].lazy = 0, tree[p].mulzy = 1;
}

void Insert(int p, int l, int r, int a, int b, LL x, int opt)
{
      int mid = (l + r) >> 1;
      if (l == a && r == b)
      {    
          if (opt == 3) tree[p].mul = tree[p].mul * x * x,
						tree[p].sum = tree[p].sum * x, 
						tree[p].lazy *= x,
						tree[p].mulzy *= x;
                   else tree[p].mul += 2 * x * tree[p].sum + (r - l + 1) * x * x, 
						tree[p].sum = tree[p].sum + (r - l + 1) * x, 
						tree[p].lazy += x;
          return;
	  }
      Updata(p, l, r);
      if (mid >= b) Insert(p * 2, l, mid, a, b, x, opt);
               else if (a > mid) Insert(p * 2 + 1, mid + 1, r, a, b, x, opt);
                            else {
                                    Insert(p * 2, l, mid, a, mid, x, opt);
                                    Insert(p * 2 + 1, mid + 1, r, mid + 1, b, x, opt);
                                 }
      tree[p].sum = tree[p * 2].sum + tree[p * 2 + 1].sum;
      tree[p].mul = tree[p * 2].mul + tree[p * 2 + 1].mul;
}

LL Get_sum(int p, int l, int r, int a, int b, int opt)
{
      int mid = (l + r) >> 1;
      if (l == a && r == b) 
	  {
	      if (opt == 1) return tree[p].sum;
                   else return tree[p].mul;
      }
      Updata(p, l, r);
      if (mid >= b) return Get_sum(p * 2, l, mid, a, b, opt);
               else if (a > mid) return Get_sum(p * 2 + 1, mid + 1, r, a, b, opt);
                            else return Get_sum(p * 2, l, mid, a, mid, opt) + Get_sum(p * 2 + 1, mid + 1, r, mid + 1, b, opt);                      
}

int main()
{
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
	Build(1, 1, n);
	for (int i = 1; i <= m; i++)
	{
		 int opt, l, r;
		 LL x;
		 scanf("%d", &opt);
	     if (opt == 1 || opt == 2)  
	     {
	     	 scanf("%d %d", &l, &r);
	     	 printf("%lld\n", Get_sum(1, 1, n, l, r, opt));
		 }
		 if (opt == 3 || opt == 4)
		 {
		 	 scanf("%d %d %lld", &l, &r, &x);
		 	 Insert(1, 1, n, l, r, x, opt);
		 }
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值