首页 > 其他分享 >【每日一题】Problem 443B. Kuriyama Mirai's Stones

【每日一题】Problem 443B. Kuriyama Mirai's Stones

时间:2023-06-26 23:25:19浏览次数:40  
标签:Stones std vector cout 443B int cin Kuriyama type

原题

解决思路

  1. 两个数组,一个未排序,一个排序;
  2. 使用前缀和的方式减少时间复杂度
#include <bits/stdc++.h>

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  int n; std::cin >> n;
  std::vector<int> v(n + 1, 0);
  for (int i = 1; i <= n; ++i) std::cin >> v[i];
  std::vector<int> u(v);
  std::sort(u.begin() + 1, u.end());

  std::vector<long long> vsum(n + 1, 0), usum(n + 1, 0);
  for (int i = 1; i <= n; ++i) {
    vsum[i] += vsum[i - 1] + v[i];
    usum[i] += usum[i - 1] + u[i];
  }
  
  int m; std::cin >> m;
  for (int i = 0; i < m; ++i) {
    int type, l, r;
    std::cin >> type >> l >> r;
    if (type == 1) {
      std::cout << vsum[r] - vsum[l - 1] << std::endl;
    } else {
      std::cout << usum[r] - usum[l - 1] << std::endl;
    }
  }
  return 0;
}

标签:Stones,std,vector,cout,443B,int,cin,Kuriyama,type
From: https://www.cnblogs.com/HelloEricy/p/17507422.html

相关文章

  • leetcode 771. Jewels and Stones
    You'regivenstrings J representingthetypesofstonesthatarejewels,and S representingthestonesyouhave. Eachcharacterin Sisatypeofstoneyouhave. Youwanttoknowhowmanyofthestonesyouhavearealsojewels.Thelettersin J areg......
  • 2023-04-20:有一堆石头,用整数数组 stones 表示 其中 stones[i] 表示第 i 块石头的重量
    2023-04-20:有一堆石头,用整数数组stones表示其中stones[i]表示第i块石头的重量。每一回合,从中选出任意两块石头,然后将它们一起粉碎假设石头的重量分别为x和y,且x<=y那么粉碎的可能结果如下:如果x==y,那么两块石头都会被完全粉碎;如果x!=y,那么重量为x的石头将......
  • 2023-04-20:有一堆石头,用整数数组 stones 表示 其中 stones[i] 表示第 i 块石头的重量
    2023-04-20:有一堆石头,用整数数组stones表示其中stones[i]表示第i块石头的重量。每一回合,从中选出任意两块石头,然后将它们一起粉碎假设石头的重量分别为x和y,且x<=y那么粉碎的可能结果如下:如果x==y,那么两块石头都会被完全粉碎;如果x!=y,那么重量为x的石头将......
  • [leeccode]771. Jewels and Stones
    J representingthetypesofstonesthatarejewels,and S representingthestonesyouhave. Eachcharacterin Sisatypeofstoneyouhave. Youwantto......
  • 【ABC270D】Stones
    首先很显然直接贪心是不行的,就好像背包的时候一直选价值最大的肯定会假一样。诶?背包?这题还真有点像背包。考虑像背包一样设,\(f_i\)表示剩下\(i\)个石子的先手最大获得......
  • LeetCode - 771. Jewels and Stones
    题目You’regivenstrings​​J​​​representingthetypesofstonesthatarejewels,and​​S​​representingthestonesyouhave.EachcharacterinSisa......
  • [LeetCode] 1753. Maximum Score From Removing Stones
    Youareplayingasolitairegamewith threepiles ofstonesofsizes a​​​​​​, b,​​​​​​and c​​​​​​respectively.Eachturnyouchoosetw......
  • [LeetCode] 947. Most Stones Removed with Same Row or Column
    Ona2Dplane,weplace n stonesatsomeintegercoordinatepoints.Eachcoordinatepointmayhaveatmostonestone.Astonecanberemovedifitshareseit......
  • ABC 270 D - Stones(博弈DP)
    https://atcoder.jp/contests/abc270/tasks/abc270_d题目大意:给定我们总共n个石子,我们每次拿的数量都必须是数组a中的一个,高桥先手,青木后手。问我们高桥可以拿到的最......
  • CF965D Single-use Stones
    题目传送门思路这题的二分解法明明很简单,但是所有题解都是一样的做法,没有二分题解,于是我来一篇二分的题解。容易想到若有\(x\)只青蛙时满足条件,\(x-1\)时也一定满足......