zoukankan      html  css  js  c++  java
  • vijos 清点人数

    背景

    NK中学组织同学们去五云山寨参加社会实践活动,按惯例要乘坐火车去。由于NK中学的学生很多,在火车开之前必须清点好人数。

    描述

    初始时,火车上没有学生;当同学们开始上火车时,年级主任从第一节车厢出发走到最后一节车厢,每节车厢随时都有可能有同学上下。年级主任走到第m节车厢时,他想知道第1到m这m节车厢上一共有多少学生,但是他没有调头往回走的习惯.也就是说每次当他提问时,m总会比前一次大。

    格式

    输入格式

    第一行两个整数n,k,表示火车共有n节车厢以及k个事件。接下来有k行,按时间先后给出k个事件,每行开头都有一个字母A,B或C,如果字母为A,接下来是一个数m,表示年级主任现在在第m节车厢;如果为B,接下来两个数m,p,表示在第m节车厢有p名学生上车;如果为C,接下来两个数m,p,表示在第m节车厢有p名学生下车。学生总人数不会超过100000。

    输出格式

    有多少个A就输出多少行,每行一个整数,回答年级主任提出的问题。

    样例1

    样例输入1

    10 7
    A 1
    B 1 1
    B 3 1
    B 4 1
    A 2
    A 3
    A 10
    

    样例输出1

    0
    1
    2
    3
    

    限制

    各个测试点1s

    提示

    注意:对于30%的数据,n<=10000,k<=10000 至少有3000个A
    对于100%的数据n<=500000,k<=100000. 至少有30000个A

     

    *******树状数组,若是上车就是加,如果是下车就是减。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 using namespace std;
     6 int i,j,m,p,n,k,tree[500005] = {0},ans;
     7 char c;
     8 int lowbit(int q)
     9 {
    10     return q & (-q);
    11 }
    12 void add(int q,int x)
    13 {
    14     while(q <= n)
    15     {
    16         tree[q] += x;
    17         q += lowbit(q);
    18     }
    19 }
    20 int chaxun(int q)
    21 {
    22     int res = 0;
    23     while(q > 0)
    24     {
    25         res += tree[q];
    26         q -= lowbit(q); 
    27      } 
    28      return res;
    29 }
    30 int main()
    31 {
    32     scanf("%d %d",&n,&k);
    33     for(i = 1;i <= k;i++)
    34     {
    35         scanf("
    %c",&c);
    36         if(c == 'A')
    37         {
    38             scanf("%d",&m);
    39             ans = chaxun(m);
    40             printf("%d
    ",ans);
    41         }
    42         else if(c == 'B')
    43         {
    44             scanf("%d %d",&m,&p);
    45             add(m,p);
    46         }
    47         else
    48         {
    49             scanf("%d %d",&m,&p);
    50             p = -p;
    51             add(m,p);
    52         }
    53      } 
    54      return 0;
    55 }
  • 相关阅读:
    leetcode701. Insert into a Binary Search Tree
    leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
    leetcode 110. Balanced Binary Tree
    leetcode 104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree
    二叉树
    leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
    5. Longest Palindromic Substring
    128. Longest Consecutive Sequence
    Mac OS下Android Studio的Java not found问题,androidfound
    安卓 AsyncHttpClient
  • 原文地址:https://www.cnblogs.com/rax-/p/9583816.html
Copyright © 2011-2022 走看看