Just a Hook

A - Just a Hook
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. 



Now Pudge wants to do some operations on the hook. 

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks. 
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows: 

For each cupreous stick, the value is 1. 
For each silver stick, the value is 2. 
For each golden stick, the value is 3. 

Pudge wants to know the total value of the hook after performing the operations. 
You may consider the original hook is made up of cupreous sticks. 
 

Input

The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases. 
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations. 
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind. 
 

Output

For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example. 
 

Sample Input

     
     
1 10 2 1 5 2 5 9 3
 

Sample Output

     
     
Case 1: The total value of the hook is 24.

对于这种输出格式就应该复制粘贴,这个只需要更新就好了,因为最后输出的都是总值,就不需要查询了,对某点的标记pushdown后一定要记得清除。


#include <iostream>
#include<cstdio>
using namespace std;
int col[100010*4];
struct node
{
    int le,ri,sum;
}tt[100010*4];
void build(int id,int l,int r)
{
    tt[id].le=l;
    tt[id].ri=r;
    col[id]=0;
    if(l==r)
        tt[id].sum=1;
    else
    {
        int mid=(l+r)/2;
        build(id*2,l,mid);
        build(id*2+1,mid+1,r);
        tt[id].sum=tt[id*2].sum+tt[id*2+1].sum;
    }
}
void pushdown(int id)
{
    if(col[id])
    {
        col[id*2]=col[id];
        col[id*2+1]=col[id];
        int m=tt[id].ri-tt[id].le+1;
        tt[id*2].sum=(m-m/2)*col[id];
        tt[id*2+1].sum=(m/2)*col[id];
        col[id]=0;
    }
}
void update(int id,int l,int r,int c)
{
    if(l<=tt[id].le&&tt[id].ri<=r)
    {
        tt[id].sum=(tt[id].ri-tt[id].le+1)*c;
        col[id]=c;
    }
    else
    {
        pushdown(id);
        int m=(tt[id].le+tt[id].ri)/2;
        if(l<=m) update(id*2,l,r,c);
        if(r>m) update(id*2+1,l,r,c);
        tt[id].sum=tt[id*2].sum+tt[id*2+1].sum;
    }
}
int main()
{
    int t,n,q;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        scanf("%d%d",&n,&q);
        build(1,1,n);
        for(int j=0;j<q;j++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            update(1,x,y,z);
        }
        printf("Case %d: The total value of the hook is %d.\n",i,tt[1].sum);
    }
    return 0;
}

在Linux内核中,kprobes(Kernel Probe)是一种动态跟踪机制,允许你在内核函数执行前后执行自定义的代码片段。虽然kprobes主要用于调试和性能分析,理论上也可以用来间接影响ioctl的功能。但是,直接在ioctl函数上应用kprobes并不常见,因为ioctl是用户空间和内核空间交互的一个特殊接口,而kprobes主要用于内核内部。 不过,如果你想通过kprobes对ioctl的某个关键步骤进行监控或修改,一个间接的方式可能是探查包含ioctl调用的函数(如设备驱动中的相关路径)。比如,你可以设置kprobe在设备驱动的`do_ioctl()`函数入口处,然后在这个钩子里判断ioctl请求,再决定是否执行实际的ioctl操作。 以下是一个简化示例: ```c #include <linux/kprobes.h> asmlinkage long __old_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { /* kprobe hook here */ if (/* check conditions for your ioctl command */){ // Call the original do_ioctl() return __old_do_ioctl(file, cmd, arg); } else { // Custom action or log return -ENOTTY; // Just a placeholder, replace with appropriate return value } } int register_my_ioctl_hook(void) { struct probe_fn probe_func = { .fn = __old_do_ioctl, ... }; kprobe_add(&probe_func); return 0; } ``` 请注意,这仅作为一个理论示例,实际操作时需要考虑到内核安全规则,并确保不会对系统稳定性造成负面影响。同时,直接修改ioctl流程可能违反设计意图,通常应该寻找更合适的位置和方式来实现所需功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值