zoukankan      html  css  js  c++  java
  • XidianOJ 1177 Counting Stars

    题目描述

       "But baby, I've been, I've been praying hard,
        Said, no more counting dollars
        We'll be counting stars"

    Grandpa Shaw loves counting stars.
    One evening he was sitting in his armchar, trying to figure out this problem.

    Consider the sky as a rectangular coordinates, each star has a coordinate (Xi,Yi).
    To make it simple, all the stars are in the first quadrant.
    Now he want to know how many stars there are in the square formed by (0,0) and (x,y).
    (including stars in edges, 4 vertices of the square is (0,0) (x,0) (x,y) (0,y))

    There are n stars in the sky, and he raised m questions.
    Because grandpa Shaw's eyesight is poor, he ask you for help.

    [img]
    in this image, yellow stars are in the square, blue ones are not.

    输入

    multiple test cases, please read until EOF.
    for each test case:
    first line contains two integers n m (0 <= n, m <= 10^5).
    following n lines each line contains two integers Xi Yi (0 <= Xi, Yi <= 10^6).
    following m lines each line contains two integers x y (0 <= x, y <= 10^6).

    输出

    for each test case:
    first line "Case #t:" t is the number of test case.
    following m lines, each line contains one integer, the number of stars in that square.

    --正文
    由于是离线的询问,并不需要二维
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    int n,m;
    int c[1000001],ans[1000001];
    
    struct StarNode {
        int x,y;
        int num;
        bool query;
    };
    struct StarNode star[2000001];
    
    bool cmp(struct StarNode s1,struct StarNode s2){
        if (s1.x == s2.x) return s1.y < s2.y;
        return s1.x < s2.x;
    }
    int lowbit(int i) {
        return i&-i;
    }
    void Update(int i,int x){
        while (i < 1000001){
            c[i] += x;
            i += lowbit(i);
        }
    }
    int Sum(int i){
        int s = 0;
        while (i > 0) {
            s += c[i];
            i -= lowbit(i);
        } 
        return s;
    } 
    
    int main(){
        int total = 0;
        while (scanf("%d %d",&n,&m) != EOF){
            memset(c,0,sizeof(c)); total ++;
            int i,j;
            for (i=1;i<=n;i++){
                int xi,yi;
                scanf("%d %d",&star[i].x,&star[i].y);
                star[i].query = false;
            }
            for (i=n+1;i<=n+m;i++){
                scanf("%d %d",&star[i].x,&star[i].y);
                star[i].query = true; star[i].num = i - n;
            }
            sort(star+1,star+1+n+m,cmp);
            printf("Case #%d:
    ",total);
            for (i=1;i<=n+m;i++){
                if (star[i].query) 
                    ans[star[i].num] = Sum(star[i].y+1);
                else 
                    Update(star[i].y+1,1);
            }
            for (i=1;i<=m;i++) 
                printf("%d
    ",ans[i]);
        } 
        return 0;
    } 
     
  • 相关阅读:
    领域驱动和MVVM应用于UWP开发的一些思考
    UWP中实现自定义标题栏
    UWP中新加的数据绑定方式x:Bind分析总结
    MVVM框架从WPF移植到UWP遇到的问题和解决方法
    UWP学习目录整理
    MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息
    MVVM模式解析和在WPF中的实现(五)View和ViewModel的通信
    MVVM设计模式和WPF中的实现(四)事件绑定
    MVVM模式解析和在WPF中的实现(三)命令绑定
    MVVM模式和在WPF中的实现(二)数据绑定
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/6151316.html
Copyright © 2011-2022 走看看