zoukankan      html  css  js  c++  java
  • Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新

    C. Appleman and a Sheet of Paper

     

     

    Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types:

    1. Fold the sheet of paper at position pi. After this query the leftmost part of the paper with dimensions 1 × pi must be above the rightmost part of the paper with dimensions 1 × ([current width of sheet] - pi).
    2. Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance li from the left border of the current sheet of paper and the other at distance ri from the left border of the current sheet of paper.

    Please look at the explanation of the first test example for better understanding of the problem.

    Input

    The first line contains two integers: n and q (1  ≤ n ≤ 105; 1 ≤ q ≤ 105) — the width of the paper and the number of queries.

    Each of the following q lines contains one of the described queries in the following format:

    • "pi(1 ≤ pi < [current width of sheet]) — the first type query.
    • "li ri(0 ≤ li < ri ≤ [current width of sheet]) — the second type query.
    Output

    For each query of the second type, output the answer.

    input
    7 4
    1 3
    1 2
    2 0 1
    2 1 2
    output
    4
    3
    思路: 暴力更新, 然后用FenwickTree 或者SegmentTree进行区间求和即可。
    因为每个位置上的value只会更新到别的位置一次,所以暴力的话复杂度也是O(n), 然后更新的时候分两种情况, 如果折过去的长度大于右边界 就相当于把右面的对应长度折过来, 否则就是题目中所说的从左面折了。
    我用ua, ub,维护了当前区间的左右端点, 每次查询也分两种情况。
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 1e5 + 5;
     4 namespace FenwickTree {
     5 int arr[maxn];
     6 void Modify(int x, int d) {
     7     while(x < maxn) {
     8         arr[x] += d;
     9         x += x & -x;
    10     }
    11 }
    12 void init(int n) {
    13     memset(arr, 0, sizeof arr);
    14     for(int i = 1; i <= n; i++) {
    15         Modify(i, 1);
    16     }
    17 }
    18 int query(int x) {
    19     int res = 0;
    20     while (x > 0) {
    21         res += arr[x];
    22         x -= x & -x;
    23     }
    24     return res;
    25 }
    26 }
    27 int main() {
    28 #ifndef ONLINE_JUDGE
    29     freopen("in.txt","r",stdin);
    30 #endif
    31     int n, q;
    32     while (~ scanf ("%d%d", &n, &q)) {
    33         int tot = 0, direction = 0;
    34         FenwickTree::init(n);
    35         int ub = n;
    36         for (int j = 0; j < q; j++) {
    37             int op, l, r, p;
    38             int ua = tot+1;
    39             scanf ("%d", &op);
    40             if (op == 1) {
    41                 scanf ("%d", &p);
    42                 if (!direction) {
    43                     if (2*p <= 1 + ub - ua) {
    44                         for (int i = ua; i <= ua+p-1; i++) {
    45                             FenwickTree::Modify(2*ua+2*p-i-1, FenwickTree::query(i) - FenwickTree::query(i-1));
    46                         }
    47                         tot += p;
    48                     } else {
    49                         p = ub - (ua + p-1);
    50                         for (int i = ub; i >= ub-p+1; i--) {
    51                             FenwickTree::Modify(2*ub-2*p+1-i, FenwickTree::query(i) - FenwickTree::query(i-1));
    52                         }
    53                         ub = ub - p;
    54                         direction ^= 1;
    55                     }
    56                 }else{
    57                     if (2*p > 1 + ub - ua){
    58                         p = ub - (ua + p-1);
    59                         for (int i = ua; i <= ua+p-1; i++) {
    60                             FenwickTree::Modify(2*ua+2*p-i-1, FenwickTree::query(i) - FenwickTree::query(i-1));
    61                         }
    62                         direction ^= 1;
    63                         tot += p;
    64                     }else{
    65                         for (int i = ub; i >= ub-p+1; i--) {
    66                             FenwickTree::Modify(2*ub-2*p+1-i, FenwickTree::query(i) - FenwickTree::query(i-1));
    67                         }
    68                         ub = ub - p;
    69                     }
    70                 }
    71 
    72             } else {
    73                 scanf ("%d%d", &l, &r);
    74                 if (!direction) {
    75                     printf("%d
    ", FenwickTree::query(ua+r-1)-FenwickTree::query(ua-1+l));
    76                 }else{
    77                     printf("%d
    ", FenwickTree::query(ub-l)-FenwickTree::query(ub-r));
    78                 }
    79             }
    80         }
    81     }
    82     return 0;
    83 }
     
  • 相关阅读:
    printf打印输出null问题的跟踪
    一个需求的反思
    编写可测试的代码
    编写高质量代码_改善C++程序的150个建议 读书笔记
    GetDlgItem的用法小结
    引用作为函数返回值的一点思考
    LoadRunner 使用介绍
    撰写技术文章的注意事项
    NetLimiter网速测试小坑
    需求管理和开发的一点小思考
  • 原文地址:https://www.cnblogs.com/oneshot/p/4857133.html
Copyright © 2011-2022 走看看